2022-04-21 11:57:45 +00:00
|
|
|
import { findNodeOfType, renderToText, replaceNodesMatching } from "@silverbulletmd/common/tree";
|
|
|
|
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
2022-04-13 12:46:52 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
if (n.type === "FencedCode") {
|
|
|
|
let codeInfoNode = findNodeOfType(n, "CodeInfo");
|
|
|
|
if (!codeInfoNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (codeInfoNode.children![0].text === "meta") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 12:46:52 +00:00
|
|
|
});
|
|
|
|
return renderToText(mdTree);
|
|
|
|
}
|