71 lines
1.4 KiB
TypeScript
71 lines
1.4 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,
|
|
};
|
|
};
|
|
|
|
export const singleLineTokens: SingleLineCfg[] = [
|
|
{
|
|
rx: /^#\s/,
|
|
create(line) {
|
|
return ({ type: "h1", line, raw: line, cfg: this });
|
|
},
|
|
replaceRx: /^#\s/,
|
|
},
|
|
{
|
|
rx: /^##\s/,
|
|
create(line) {
|
|
return ({ type: "h2", line, raw: line, cfg: this });
|
|
},
|
|
replaceRx: /^##\s/,
|
|
},
|
|
{
|
|
rx: /^###\s/,
|
|
create(line) {
|
|
return ({ type: "h3", line, raw: line, cfg: this });
|
|
},
|
|
replaceRx: /^###\s/,
|
|
},
|
|
{
|
|
rx: /^-\s/,
|
|
create(line) {
|
|
return ({ type: "list1", line, raw: line, mends: true, cfg: this });
|
|
},
|
|
replaceRx: /^-\s/,
|
|
shouldMendNextLine: true,
|
|
},
|
|
{
|
|
rx: /^[\t\s]{2}-\s/,
|
|
create(line) {
|
|
return ({ type: "list2", line, raw: line, mends: true, cfg: this });
|
|
},
|
|
replaceRx: /^[\t\s]{2}-\s/,
|
|
shouldMendNextLine: true,
|
|
},
|
|
];
|