1
0
silverbullet/plugs/markdown/util.ts

25 lines
728 B
TypeScript
Raw Normal View History

import { renderToText, replaceNodesMatching } from "../../common/tree";
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
export function encodePageUrl(name: string): string {
return name.replaceAll(" ", "_");
}
export async function cleanMarkdown(text: string): Promise<string> {
let mdTree = await parseMarkdown(text);
replaceNodesMatching(mdTree, (n) => {
if (n.type === "WikiLink") {
const page = n.children![1].children![0].text!;
return {
// HACK
text: `[${page}](/${encodePageUrl(page)})`,
};
}
// Simply get rid of these
if (n.type === "CommentBlock" || n.type === "Comment") {
return null;
}
});
return renderToText(mdTree);
}