Link support
This commit is contained in:
49
markdown.js
49
markdown.js
@@ -170,7 +170,6 @@ class OrderedListItem extends Symbol {
|
|||||||
return `<li class="indent-${this.level}">${this.text} indentation level ${this.level}</li>`;
|
return `<li class="indent-${this.level}">${this.text} indentation level ${this.level}</li>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Link extends Symbol {
|
class Link extends Symbol {
|
||||||
/**
|
/**
|
||||||
* @type {RegExp}
|
* @type {RegExp}
|
||||||
@@ -205,8 +204,9 @@ class Link extends Symbol {
|
|||||||
static create(lineFeed) {
|
static create(lineFeed) {
|
||||||
const instance = new Link();
|
const instance = new Link();
|
||||||
const line = lineFeed.claim().trim();
|
const line = lineFeed.claim().trim();
|
||||||
const { text, link } = Link.textAndLinkRegExp.exec(line)?.groups ?? {};
|
const [linkLine, rest] = extractTokenAndRest(Link.textAndLinkRegExp, line);
|
||||||
console.log("test:", text, link);
|
lineFeed.push(rest);
|
||||||
|
const { text, link } = Link.textAndLinkRegExp.exec(linkLine)?.groups ?? {};
|
||||||
instance.link = link ?? "";
|
instance.link = link ?? "";
|
||||||
instance.text = text ?? "";
|
instance.text = text ?? "";
|
||||||
return instance;
|
return instance;
|
||||||
@@ -459,13 +459,50 @@ class CatchAll extends Symbol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {string} token
|
* @param {string|RexExp} token
|
||||||
* @param {string} string
|
* @param {string} string
|
||||||
* @returns {string[]}
|
* @returns {string[]}
|
||||||
*/
|
*/
|
||||||
const splitStringAtToken = (token, string) => {
|
const splitStringAtToken = (token, string) => {
|
||||||
const index = string.indexOf(token);
|
let index = 0;
|
||||||
return [string.slice(0, index), string.slice(index + token.length)];
|
let length = 0;
|
||||||
|
if (typeof token?.exec === "function") {
|
||||||
|
const exp = new RegExp(token);
|
||||||
|
const match = exp.exec(string);
|
||||||
|
index = match?.index ?? -1;
|
||||||
|
length = match?.[0].length ?? 0;
|
||||||
|
} else {
|
||||||
|
index = string.indexOf(token);
|
||||||
|
length = token.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
const splitTokenNotFound = index === -1;
|
||||||
|
if (splitTokenNotFound) return [string, ""];
|
||||||
|
return [string.slice(0, index), string.slice(index + length)];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string|RexExp} token
|
||||||
|
* @param {string} string
|
||||||
|
* @returns {string[]}
|
||||||
|
*/
|
||||||
|
const extractTokenAndRest = (token, string) => {
|
||||||
|
let index = 0;
|
||||||
|
let length = 0;
|
||||||
|
if (typeof token?.exec === "function") {
|
||||||
|
const exp = new RegExp(token);
|
||||||
|
const match = exp.exec(string);
|
||||||
|
index = match?.index ?? -1;
|
||||||
|
length = match?.[0].length ?? 0;
|
||||||
|
} else {
|
||||||
|
index = string.indexOf(token);
|
||||||
|
length = token.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
const splitTokenNotFound = index === -1;
|
||||||
|
if (splitTokenNotFound) return [string, ""];
|
||||||
|
return [string.slice(0, index + length), string.slice(index + length)];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -71,5 +71,6 @@ const y = () => {
|
|||||||
1. ordered list item
|
1. ordered list item
|
||||||
|
|
||||||
[link to the index](https://blog.zacke.dev)
|
[link to the index](https://blog.zacke.dev)
|
||||||
|
[link to the index](https://blog.zacke.dev) with text after it
|
||||||
|
|
||||||

|

|
||||||
|
|||||||
Reference in New Issue
Block a user