WIP Links
This commit is contained in:
141
markdown.js
141
markdown.js
@@ -99,6 +99,124 @@ class Heading extends Symbol {
|
||||
}
|
||||
}
|
||||
|
||||
class UnorderedListItem extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
level = 0;
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
return line.trim().startsWith("- ");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {LineFeed} lineFeed
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new UnorderedListItem();
|
||||
const line = lineFeed.claim();
|
||||
instance.text = line.replaceAll("-", "").trim();
|
||||
instance.level = getAmountOfTokenInBeginningOfFile(" ", line);
|
||||
return instance;
|
||||
}
|
||||
|
||||
render() {
|
||||
return `<li class="indent-${this.level}">${this.text} indentation level ${this.level}</li>`;
|
||||
}
|
||||
}
|
||||
|
||||
class OrderedListItem extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
level = 0;
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
return new RegExp(/^\d\. /).test(line.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {LineFeed} lineFeed
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new UnorderedListItem();
|
||||
const line = lineFeed.claim();
|
||||
instance.text = line.trim().replace(/^\d\. /, "");
|
||||
instance.level = getAmountOfTokenInBeginningOfFile(" ", line);
|
||||
return instance;
|
||||
}
|
||||
|
||||
render() {
|
||||
return `<li class="indent-${this.level}">${this.text} indentation level ${this.level}</li>`;
|
||||
}
|
||||
}
|
||||
|
||||
class Link extends Symbol {
|
||||
/**
|
||||
* @type {RegExp}
|
||||
*/
|
||||
static canParseRegExp = new RegExp(/^\[.*\]\(.*\)/);
|
||||
/**
|
||||
* @type {RegExp}
|
||||
*/
|
||||
static textAndLinkRegExp = new RegExp(/\[(?<text>.*)\]\((?<link>.*)\)/);
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
link = "";
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
return Link.canParseRegExp.test(line.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {LineFeed} lineFeed
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new Link();
|
||||
const line = lineFeed.claim().trim();
|
||||
const { text, link } = Link.textAndLinkRegExp.exec(line)?.groups ?? {};
|
||||
console.log("test:", text, link);
|
||||
instance.link = link ?? "";
|
||||
instance.text = text ?? "";
|
||||
return instance;
|
||||
}
|
||||
|
||||
render() {
|
||||
return `<a href="${this.link}">${this.text}</a>`;
|
||||
}
|
||||
}
|
||||
|
||||
class Italic extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
@@ -350,10 +468,33 @@ const splitStringAtToken = (token, string) => {
|
||||
return [string.slice(0, index), string.slice(index + token.length)];
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} token
|
||||
* @param {string} string
|
||||
*/
|
||||
const getAmountOfTokenInBeginningOfFile = (token, string) => {
|
||||
let count = 0;
|
||||
let searchIndex = 0;
|
||||
while (true) {
|
||||
if (searchIndex + (token.length - 1) > string.length) return count;
|
||||
|
||||
const index = string.slice(searchIndex).indexOf(token);
|
||||
const indexNotAtStartOfString = index !== 0;
|
||||
if (indexNotAtStartOfString) return count;
|
||||
|
||||
count++;
|
||||
searchIndex += token.length;
|
||||
}
|
||||
};
|
||||
|
||||
const Factories = [
|
||||
Heading,
|
||||
UnorderedListItem,
|
||||
OrderedListItem,
|
||||
Bold,
|
||||
Italic,
|
||||
Link,
|
||||
MultiLineCode,
|
||||
SingleLineCode,
|
||||
CatchAll,
|
||||
|
||||
@@ -62,10 +62,12 @@ const y = () => {
|
||||
|
||||
- bullet
|
||||
- bullet
|
||||
- bullet
|
||||
- bullet
|
||||
|
||||
1. ordered list item
|
||||
1. ordered list item
|
||||
1. ordered list item
|
||||
1. ordered list item
|
||||
|
||||
[link to the index](https://blog.zacke.dev)
|
||||
|
||||
Reference in New Issue
Block a user