tabletop-commander/lib/tcmd/tokenizeParagraph.ts

45 lines
1.0 KiB
TypeScript

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