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