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 remarkCaptions from "remark-captions"; 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"; import i18n from "./i18n.mjs"; // Support for more highlight syntaxes import arduino from "highlight.js/lib/languages/arduino"; import bash from "highlight.js/lib/languages/bash"; import c from "highlight.js/lib/languages/c"; import cpp from "highlight.js/lib/languages/cpp"; import css from "highlight.js/lib/languages/css"; import diff from "highlight.js/lib/languages/diff"; import go from "highlight.js/lib/languages/go"; import graphql from "highlight.js/lib/languages/graphql"; import haskell from "highlight.js/lib/languages/haskell"; import ini from "highlight.js/lib/languages/ini"; import javascript from "highlight.js/lib/languages/javascript"; import json from "highlight.js/lib/languages/json"; import less from "highlight.js/lib/languages/less"; import lua from "highlight.js/lib/languages/lua"; import makefile from "highlight.js/lib/languages/makefile"; import markdown from "highlight.js/lib/languages/markdown"; import nginx from "highlight.js/lib/languages/nginx"; import plaintext from "highlight.js/lib/languages/plaintext"; import python from "highlight.js/lib/languages/python"; import pythonRepl from "highlight.js/lib/languages/python-repl"; import r from "highlight.js/lib/languages/r"; import rust from "highlight.js/lib/languages/rust"; import scss from "highlight.js/lib/languages/scss"; import shell from "highlight.js/lib/languages/shell"; import sql from "highlight.js/lib/languages/sql"; import typescript from "highlight.js/lib/languages/typescript"; import wasm from "highlight.js/lib/languages/wasm"; import xml from "highlight.js/lib/languages/xml"; const highlightLanguages = { arduino, bash, c, cpp, css, diff, go, graphql, haskell, ini, javascript, json, less, lua, makefile, markdown, nginx, plaintext, python, pythonRepl, r, rust, scss, shell, sql, typescript, wasm, xml, }; const autoLinkOption = { behavior: "wrap", properties: { tabIndex: 0, className: "anchor", }, }; const pictureOptions = { png: { avif: "image/avif" }, jpg: { avif: "image/avif" }, }; const captionsOptions = { external: { table: "Table:", code: "Code:", math: "Equation:", }, internal: { image: "Caption:", }, }; const generator = unified() .use(remarkParse) .use(remarkGfm) .use(remarkToc, { heading: getTocHeading(), tight: true, ordered: true }) .use(remarkMath) .use(remarkDirective) .use(remarkSpecialBox) .use(remarkCaptions, captionsOptions) .use(remarkFrontmatter, { type: "toml", marker: "-" }) .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) .use(remarkRemoveFootnoteHeader) .use(rehypePicture, pictureOptions) .use(rehypeKaTeX) .use(rehypeSlug) .use(rehypeHighlight, { languages: highlightLanguages }) .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(remarkCaptions, captionsOptions) .use(remarkFrontmatter, { type: "toml", marker: "-" }); const toHtml = (content) => generator.processSync(content); export const toMdRaw = (content) => generatorMd.runSync(generatorMd.parse(content)); export const mdToHtmlRaw = (content, lang) => { const generatorHTML = unified() .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) .use(remarkRemoveFootnoteHeader, { title: i18n[lang]["footnotes_title"] }) .use(rehypePicture, pictureOptions) .use(rehypeKaTeX) .use(rehypeSlug) .use(rehypeHighlight, { languages: highlightLanguages }) .use(rehypeAutolinkHeadings, autoLinkOption) .use(rehypeStringify); return generatorHTML.runSync(content); }; export const toHtmlRaw = (content) => generator.runSync(generator.parse(content)); export const toString = (content) => generator.stringify(content); export default toHtml;