all repos — home @ 0093ce5abab2fe3dbbb2ae31e3987e577b584131

generator/converter.ts (view raw)

 1// @deno-types="./types.d.ts"
 2
 3import {
 4  remarkParse,
 5  unified,
 6  rehypeStringify,
 7  remarkRehype,
 8  rehypeRaw,
 9  rehypeSanitize,
10  rehypeAttr,
11} from "./deps.ts";
12
13type Converter = {
14  [key in MediaType]: null | ((mdContent: string) => Promise<string>);
15};
16
17const mdToHtml = async (mdContent: string) => {
18  const vfile = await unified()
19    .use(remarkParse)
20    .use(remarkRehype, { allowDangerousHtml: true })
21    .use(rehypeRaw)
22    .use(rehypeAttr, { properties: "attr" })
23    .use(rehypeSanitize)
24    .use(rehypeStringify)
25    .process(mdContent);
26  return vfile.value.replaceAll('.md', '.html');
27};
28
29const mdToMd = (mdContent: string) => Promise.resolve(mdContent);
30
31const converter: Converter = {
32  "text/html": mdToHtml,
33  "text/markdown": mdToMd,
34  "text/gemini": null,
35};
36
37export default converter;