给 KaTeX 加一层包装
This commit is contained in:
@ -29,6 +29,35 @@ const allowed_langs = [
|
|||||||
'mermaid',
|
'mermaid',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* KaTeX 渲染结果修改。
|
||||||
|
*
|
||||||
|
* 让 KaTeX 的公式可复制。
|
||||||
|
*/
|
||||||
|
|
||||||
|
function setupKatexCopy(md: MarkdownIt) {
|
||||||
|
const defaultInline = md.renderer.rules.math_inline!
|
||||||
|
const defaultBlock = md.renderer.rules.math_block!
|
||||||
|
|
||||||
|
md.renderer.rules.math_inline = (tokens, idx, options, env, self) => {
|
||||||
|
const content = tokens[idx].content
|
||||||
|
const rendered = defaultInline(tokens, idx, options, env, self)
|
||||||
|
|
||||||
|
return `<span class="katex-wrapper inline" data-code="${encodeURIComponent(content)}">${rendered}</span>`
|
||||||
|
}
|
||||||
|
|
||||||
|
md.renderer.rules.math_block = (tokens, idx, options, env, self) => {
|
||||||
|
const content = tokens[idx].content
|
||||||
|
const rendered = defaultBlock(tokens, idx, options, env, self)
|
||||||
|
return `
|
||||||
|
<div class="katex-wrapper block" data-code="${encodeURIComponent(content)}">${rendered}
|
||||||
|
<button class="copy-katex" title="Copy LaTeX">
|
||||||
|
复制
|
||||||
|
</button>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Markdown 渲染函数。
|
* Markdown 渲染函数。
|
||||||
*
|
*
|
||||||
@ -59,6 +88,7 @@ export async function renderMarkdown(content: string): Promise<string> {
|
|||||||
})
|
})
|
||||||
|
|
||||||
md.use(mdKatex)
|
md.use(mdKatex)
|
||||||
|
setupKatexCopy(md)
|
||||||
|
|
||||||
return md.render(content)
|
return md.render(content)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user