tabletop-commander/lib/tcmd/tokenizeBlock.ts

45 lines
1.0 KiB
TypeScript

export const tokenizeBlock = (paragraph: string) => {
for (const block of blockTokens) {
const openTest = block.rx.test(paragraph),
closeTest = block.closeRx.test(paragraph);
if (closeTest) return block.create(paragraph).type;
if (!openTest) continue;
return block.create(paragraph);
}
};
const blockTokens: {
rx: RegExp;
closeRx: RegExp;
create: (line: string) => BlockToken;
}[] = [
// this indicates that this is a grid block, all paragraphs within this block will be placed in a number of columns that match the number of sets of brackets are in this line
{
rx: /^(\[\]){2,}/g,
closeRx: /\/\[\]/,
create(line) {
return {
type: "grid",
metadata: {
columns: line.match(/\[\]/g)?.length,
},
children: [],
closed: false,
};
},
},
{
rx: /^(\[\[)/,
closeRx: /\]\]/,
create() {
return {
type: "card",
metadata: {},
children: [],
closed: false,
};
},
},
];