Identifies level 1 headings and selects everything as text for it.

This commit is contained in:
2023-09-16 12:53:24 +02:00
parent 4be04f7115
commit ffb4778824
3 changed files with 50 additions and 23 deletions

View File

@@ -1,19 +1,39 @@
const toHtml = () => {
return `<div>markdown</div>`;
export const toHtml = (markdown) => {
const symbols = toSymbols(markdown);
const html = [];
symbols.forEach((s) =>
html.push(renderers.find((r) => r.key === s.key).render(s))
);
return `<div>${html.join("\r\n")}</div>`;
};
const singleSymbols = [
"#",
"##",
"###",
"####",
"#####",
"######",
"-",
"- []",
"- [ ]",
const toSymbols = (markdown) => {
const symbols = [];
if (heading.symbols.some((s) => markdown.includes(s))) {
const symbol = Object.assign({}, heading);
symbol.text = markdown;
symbols.push(symbol);
}
return symbols;
};
const heading = {
key: "heading",
level: 1,
multiLine: false,
symbols: ["#", "##", "###", "####", "#####", "######"],
text: "",
children: [],
};
const renderers = [
{
key: "heading",
render: (symbol) => {
return `<h1>${symbol.text}</h1>`;
},
},
];
const doubleSymbols = ["*", "**", "_", "__", "`", "```"];
const symbols = {};