all repos — home @ d964dd124ca0722283ef61f238b46364b2fe5a4d

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  rehypeExternalLinks,
12} from "./deps.ts";
13
14type Converter = {
15  [key in MediaType]: null | ((mdContent: string) => Promise<string>);
16};
17
18const mdToHtml = async (mdContent: string) => {
19  const vfile = await unified()
20    .use(remarkParse)
21    .use(remarkRehype, { allowDangerousHtml: true })
22    .use(rehypeRaw)
23    .use(rehypeAttr, { properties: "attr" })
24    .use(rehypeSanitize)
25    .use(rehypeExternalLinks, { rel: ["nofollow"] })
26    .use(rehypeStringify)
27    .process(mdContent);
28  return vfile.value.replaceAll(".md", ".html");
29};
30
31const mdToMd = (mdContent: string) => Promise.resolve(mdContent);
32
33const converter: Converter = {
34  "text/html": mdToHtml,
35  "text/markdown": mdToMd,
36  "text/gemini": null,
37};
38
39export default converter;