import { encode as HTMLEncode } from "he"; import markdownIt from "markdown-it"; import markdownItKatexPlugin from "./plugins/markdown-katex"; import hljs from "highlight.js"; import "./themes/github-dark.css"; import "./themes/github.css"; import { v4 } from "uuid"; const markdown = markdownIt({ html: false, typographer: true, highlight: function (code, lang) { const uuid = v4(); const theme = window.localStorage.getItem("theme") === "light" ? "github" : "github-dark"; if (lang && hljs.getLanguage(lang)) { try { return ( `
${lang || ""}
` +
          hljs.highlight(code, { language: lang, ignoreIllegals: true }).value +
          "
" ); } catch (__) {} } return ( `
` +
      HTMLEncode(code) +
      "
" ); }, }); // Add custom renderer for strong tags to handle theme colors markdown.renderer.rules.strong_open = () => ''; markdown.renderer.rules.strong_close = () => ""; markdown.renderer.rules.link_open = (tokens, idx) => { const token = tokens[idx]; const href = token.attrs.find((attr) => attr[0] === "href"); return ``; }; // Custom renderer for responsive images rendered in markdown markdown.renderer.rules.image = function (tokens, idx) { const token = tokens[idx]; const srcIndex = token.attrIndex("src"); const src = token.attrs[srcIndex][1]; const alt = token.content || ""; return `
${alt}
`; }; markdown.use(markdownItKatexPlugin); export default function renderMarkdown(text = "") { return markdown.render(text); }