tabletop-commander/lib/tcmd/tokenizeLine.ts

104 lines
1.8 KiB
TypeScript

import { tokenizeInline } from "./tokenizeInline";
export const tokenizeLine = (
line: string,
previous?: SingleLineToken,
): SingleLineToken => {
for (const token of singleLineTokens) {
if (!token.rx.test(line)) continue;
const t = token.create(line);
if (t.type === "h2") {
}
t.line = tokenizeInline(line.replace(token.replaceRx, ""));
return t;
}
if (previous?.mends) {
previous.raw += " " + line;
previous.line = tokenizeInline(previous.raw.replace(previous.cfg!.rx, ""));
return previous;
}
return {
line: tokenizeInline(line),
type: "text",
raw: line,
uuid: crypto.randomUUID(),
};
};
export const singleLineTokens: SingleLineCfg[] = [
{
rx: /^#\s/,
create(line) {
return ({
type: "h1",
line,
raw: line,
cfg: this,
uuid: crypto.randomUUID(),
});
},
replaceRx: /^#\s/,
},
{
rx: /^##\s/,
create(line) {
return ({
type: "h2",
line,
raw: line,
cfg: this,
uuid: crypto.randomUUID(),
});
},
replaceRx: /^##\s/,
},
{
rx: /^###\s/,
create(line) {
return ({
type: "h3",
line,
raw: line,
cfg: this,
uuid: crypto.randomUUID(),
});
},
replaceRx: /^###\s/,
},
{
rx: /^-\s/,
create(line) {
return ({
type: "list1",
line,
raw: line,
mends: true,
cfg: this,
uuid: crypto.randomUUID(),
});
},
replaceRx: /^-\s/,
shouldMendNextLine: true,
},
{
rx: /^[\t\s]{2}-\s/,
create(line) {
return ({
type: "list2",
line,
raw: line,
mends: true,
cfg: this,
uuid: crypto.randomUUID(),
});
},
replaceRx: /^[\t\s]{2}-\s/,
shouldMendNextLine: true,
},
];