summaryrefslogtreecommitdiff
path: root/src/build/to-html.mjs
blob: 5c0824274202817b865dbe01641ef2a0fc8d823d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import remarkToc from "remark-toc";
import remarkDirective from "remark-directive";
import remarkMath from "remark-math";
import remarkFrontmatter from "remark-frontmatter";
import remarkRehype from "remark-rehype";
import rehypeSlug from "rehype-slug";
import rehypePicture from "rehype-picture";
import rehypeKaTeX from "rehype-katex";
import rehypeRaw from "rehype-raw";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeStringify from "rehype-stringify";
import rehypeHighlight from "rehype-highlight";
import remarkSpecialBox from "./special_box.mjs";
import remarkRemoveFootnoteHeader from "./remove-footnote-header.mjs";
import { getTocHeading } from "./i18n.mjs";

const autoLinkOption = {
  behavior: "wrap",
  properties: {
    tabIndex: 0,
    className: "anchor",
  },
};

const pictureOptions = {
  png: { avif: "image/avif" },
};

const generator = unified()
  .use(remarkParse)
  .use(remarkGfm)
  .use(remarkToc, { heading: getTocHeading(), tight: true, ordered: true })
  .use(remarkMath)
  .use(remarkDirective)
  .use(remarkSpecialBox)
  .use(remarkFrontmatter, { type: "toml", marker: "-" })
  .use(remarkRehype, { allowDangerousHtml: true })
  .use(rehypeRaw)
  .use(remarkRemoveFootnoteHeader)
  .use(rehypePicture, pictureOptions)
  .use(rehypeKaTeX)
  .use(rehypeSlug)
  .use(rehypeHighlight)
  .use(rehypeAutolinkHeadings, autoLinkOption)
  .use(rehypeStringify);

const generatorMd = unified()
  .use(remarkParse)
  .use(remarkGfm)
  .use(remarkToc, { heading: getTocHeading(), tight: true, ordered: true })
  .use(remarkMath)
  .use(remarkDirective)
  .use(remarkSpecialBox)
  .use(remarkFrontmatter, { type: "toml", marker: "-" });

const generatorHTML = unified()
  .use(remarkRehype, { allowDangerousHtml: true })
  .use(rehypeRaw)
  .use(remarkRemoveFootnoteHeader)
  .use(rehypePicture, pictureOptions)
  .use(rehypeKaTeX)
  .use(rehypeSlug)
  .use(rehypeHighlight)
  .use(rehypeAutolinkHeadings, autoLinkOption)
  .use(rehypeStringify);

const toHtml = (content) => generator.processSync(content);

export const toMdRaw = (content) =>
  generatorMd.runSync(generatorMd.parse(content));
export const mdToHtmlRaw = (content) => generatorHTML.runSync(content);

export const toHtmlRaw = (content) =>
  generator.runSync(generator.parse(content));
export const toString = (content) => generator.stringify(content);
export default toHtml;