优化移动设备体验

This commit is contained in:
tianyaxiang 2025-01-30 01:18:32 +08:00
parent ae9ecdfa4b
commit 6f0ac58505

View File

@ -65,9 +65,6 @@ marked.setOptions({
}) })
export function convertToWechat(markdown: string, options: RendererOptions = {}): string { export function convertToWechat(markdown: string, options: RendererOptions = {}): string {
// 预处理 markdown
markdown = preprocessMarkdown(markdown)
// 创建渲染器 // 创建渲染器
const customRenderer = new marked.Renderer() const customRenderer = new marked.Renderer()
@ -83,7 +80,9 @@ export function convertToWechat(markdown: string, options: RendererOptions = {})
customRenderer.paragraph = function({ text }: Tokens.Paragraph) { customRenderer.paragraph = function({ text }: Tokens.Paragraph) {
const style = options.block?.p const style = options.block?.p
const styleStr = cssPropertiesToString(style) const styleStr = cssPropertiesToString(style)
return `<p${styleStr ? ` style="${styleStr}"` : ''}>${text}</p>` const tokens = marked.Lexer.lexInline(text)
const content = marked.Parser.parseInline(tokens, { renderer: customRenderer })
return `<p${styleStr ? ` style="${styleStr}"` : ''}>${content}</p>`
} }
customRenderer.blockquote = function({ text }: Tokens.Blockquote) { customRenderer.blockquote = function({ text }: Tokens.Blockquote) {
@ -133,13 +132,17 @@ export function convertToWechat(markdown: string, options: RendererOptions = {})
const tag = ordered ? 'ol' : 'ul' const tag = ordered ? 'ol' : 'ul'
const style = options.block?.[ordered ? 'ol' : 'ul'] const style = options.block?.[ordered ? 'ol' : 'ul']
const styleStr = cssPropertiesToString(style) const styleStr = cssPropertiesToString(style)
return `<${tag}${styleStr ? ` style="${styleStr}"` : ''}>${body.raw}</${tag}>` const tokens = marked.Lexer.lexInline(body.raw)
const content = marked.Parser.parseInline(tokens, { renderer: customRenderer })
return `<${tag}${styleStr ? ` style="${styleStr}"` : ''}>${content}</${tag}>`
} }
customRenderer.listitem = function(item: Tokens.ListItem) { customRenderer.listitem = function(item: Tokens.ListItem) {
const style = options.inline?.listitem const style = options.inline?.listitem
const styleStr = cssPropertiesToString(style) const styleStr = cssPropertiesToString(style)
return `<li${styleStr ? ` style="${styleStr}"` : ''}>${item.text}</li>` const tokens = marked.Lexer.lexInline(item.text)
const content = marked.Parser.parseInline(tokens, { renderer: customRenderer })
return `<li${styleStr ? ` style="${styleStr}"` : ''}>${content}</li>`
} }
// Convert Markdown to HTML using the custom renderer // Convert Markdown to HTML using the custom renderer