fix: 操作ldrs

This commit is contained in:
Tim
2025-08-07 20:07:37 +08:00
parent cfdd257b9a
commit 73b9dcf0cd
9 changed files with 158 additions and 36 deletions

View File

@@ -1,5 +1,26 @@
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css'
const md = new MarkdownIt({
html: false,
linkify: true,
breaks: true,
highlight: (str, lang) => {
let code = ''
if (lang && hljs.getLanguage(lang)) {
code = hljs.highlight(str, { language: lang, ignoreIllegals: true }).value
} else {
code = hljs.highlightAuto(str).value
}
return `<pre class="code-block"><button class="copy-code-btn">Copy</button><code class="hljs language-${lang || ''}">${code}</code></pre>`
}
})
// todo: 简单用正则操作一下,后续体验不佳可以采用 striptags
export function stripMarkdown(text) {
return text ? text.replace(/[#_*`>\-\[\]\(\)!]/g, '') : ''
const html = md.render(text)
return html.replace(/<\/?[^>]+>/g, '')
}
export function stripMarkdownLength(text, length) {