tabletop-commander/lib/tcmd/tokenizeParagraph.ts

45 lines
1.0 KiB
TypeScript

export const tokenizeParagraph = (paragraph: string) => {
for (const block of blockTokens) {
const openTest = block.rx.test(paragraph),
closeTest = block.closeRx.test(paragraph);
if (openTest && closeTest) {
const p = block.create(paragraph);
p.closed = true;
return p;
}
if (closeTest) return block.create(paragraph).content;
if (openTest) {
return block.create(paragraph);
}
}
};
const blockTokens: {
rx: RegExp;
closeRx: RegExp;
create: (line: string) => ParagraphToken;
}[] = [
{
rx: /^```/g,
closeRx: /\n```/g,
create(line) {
return {
type: "code",
metadata: {
language: line.split("\n").at(0)!.replace(this.rx, ""),
},
closed: false,
content: [{
line: line.replace(/```.*?\n/g, "").replace(/\n```/, ""),
type: "text",
raw: line,
uuid: crypto.randomUUID(),
}],
allowsInline: false,
uuid: crypto.randomUUID(),
};
},
},
];