perf: 优化 head id 渲染

This commit is contained in:
xiaozzzi 2024-01-16 16:21:43 +08:00
parent c031935a44
commit 023d4e491e

View File

@ -108,13 +108,18 @@ const domParser = new DOMParser()
* TOC ,
* @param text
* @param level
* @param raw
*/
export const renderHeading = (text: any, level: number) => {
export const renderHeading = (text: string, level: number, raw: string) => {
let id: string = randomInt(1000000, 9999999).toString()
try {
let dom = domParser.parseFromString(text, 'text/html')
if (dom) {
id += dom.body.innerText
if (raw.indexOf('<') > -1 && raw.indexOf('>') > -1) {
let dom = domParser.parseFromString(raw, 'text/html')
if (dom) {
id += dom.body.innerText
} else {
id += text
}
} else {
id += text
}