Files
blog/markdown.js

40 lines
766 B
JavaScript

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 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>`;
},
},
];