优化 代码块主题
This commit is contained in:
parent
a6f8d32ae0
commit
4d9dddd8b5
@ -145,7 +145,6 @@ function getTokenStyles(theme: CodeThemeId, tokenType: string): string {
|
||||
|
||||
const tokenColor = themeConfig.theme[tokenType as keyof typeof themeConfig.theme]
|
||||
if (!tokenColor) return ''
|
||||
|
||||
return `color: ${tokenColor};`
|
||||
}
|
||||
|
||||
@ -197,28 +196,34 @@ export function convertToWechat(markdown: string, options: RendererOptions = def
|
||||
|
||||
// 重写 code 方法
|
||||
renderer.code = function({ text, lang }: Tokens.Code) {
|
||||
|
||||
const codeStyle = (mergedOptions.block?.code_pre || {}) as StyleOptions
|
||||
const style: StyleOptions = {
|
||||
...codeStyle,
|
||||
...getCodeThemeStyles(mergedOptions.codeTheme)
|
||||
}
|
||||
const styleStr = cssPropertiesToString(style)
|
||||
|
||||
// 代码高亮处理
|
||||
let highlighted = text
|
||||
if (lang && Prism.languages[lang]) {
|
||||
try {
|
||||
const grammar = Prism.languages[lang]
|
||||
const tokens = Prism.tokenize(text, grammar)
|
||||
|
||||
highlighted = tokens.map(token => {
|
||||
// Helper function to recursively process tokens
|
||||
const processToken = (token: string | Prism.Token): string => {
|
||||
if (typeof token === 'string') {
|
||||
return token
|
||||
}
|
||||
|
||||
const tokenStyle = getTokenStyles(mergedOptions.codeTheme, token.type)
|
||||
return `<span style="${tokenStyle}">${token.content}</span>`
|
||||
}).join('')
|
||||
const content = Array.isArray(token.content)
|
||||
? token.content.map(t => processToken(t)).join('')
|
||||
: processToken(token.content)
|
||||
|
||||
return `<span style="${tokenStyle}">${content}</span>`
|
||||
}
|
||||
|
||||
try {
|
||||
const grammar = Prism.languages[lang]
|
||||
const tokens = Prism.tokenize(text, grammar)
|
||||
highlighted = tokens.map(processToken).join('')
|
||||
} catch (error) {
|
||||
console.error(`Error highlighting code: ${error}`)
|
||||
}
|
||||
@ -229,6 +234,7 @@ export function convertToWechat(markdown: string, options: RendererOptions = def
|
||||
|
||||
// 重写 codespan 方法
|
||||
renderer.codespan = function({ text }: Tokens.Codespan) {
|
||||
|
||||
const codespanStyle = (mergedOptions.inline?.codespan || {}) as StyleOptions
|
||||
const style: StyleOptions = {
|
||||
...codespanStyle
|
||||
|
Loading…
Reference in New Issue
Block a user