import MarkdownIt from "markdown-it"; import hljs from "highlight.js"; import mila from "markdown-it-link-attributes"; import mdKatex from "@traptitech/markdown-it-katex"; /** * Markdown */ const MarkdownUtils = { mdi: null, formatMsg: (text) => { const array = text.match(/]*?>/g); if (array) { array.some(res => { text = text.replace(res, `
${res}
`); }) } return text }, highlightBlock: (str, lang = '') => { return `
${lang}${$A.L('复制代码')}
${str}
` }, } export function MarkdownConver(text) { if (text === '...') { return '

 

' } if (MarkdownUtils.mdi === null) { MarkdownUtils.mdi = new MarkdownIt({ linkify: true, highlight(code, language) { const validLang = !!(language && hljs.getLanguage(language)) if (validLang) { const lang = language ?? '' return MarkdownUtils.highlightBlock(hljs.highlight(code, {language: lang}).value, lang) } return MarkdownUtils.highlightBlock(hljs.highlightAuto(code).value, '') }, }) MarkdownUtils.mdi.use(mila, {attrs: {target: '_blank', rel: 'noopener'}}) MarkdownUtils.mdi.use(mdKatex, {blockClass: 'katexmath-block rounded-md p-[10px]', errorColor: ' #cc0000'}) } return MarkdownUtils.formatMsg(MarkdownUtils.mdi.render(text)) }