Link support

This commit is contained in:
2023-09-17 10:44:49 +02:00
parent 2f8ccc7ad1
commit d262aae6b2
2 changed files with 44 additions and 6 deletions

View File

@@ -170,7 +170,6 @@ class OrderedListItem extends Symbol {
return `<li class="indent-${this.level}">${this.text} indentation level ${this.level}</li>`;
}
}
class Link extends Symbol {
/**
* @type {RegExp}
@@ -205,8 +204,9 @@ class Link extends 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);
const [linkLine, rest] = extractTokenAndRest(Link.textAndLinkRegExp, line);
lineFeed.push(rest);
const { text, link } = Link.textAndLinkRegExp.exec(linkLine)?.groups ?? {};
instance.link = link ?? "";
instance.text = text ?? "";
return instance;
@@ -459,13 +459,50 @@ class CatchAll extends Symbol {
/**
*
* @param {string} token
* @param {string|RexExp} token
* @param {string} string
* @returns {string[]}
*/
const splitStringAtToken = (token, string) => {
const index = string.indexOf(token);
return [string.slice(0, index), string.slice(index + token.length)];
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), 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)];
};
/**