/g, ``)
return this.styledContent(`blockquote`, text)
}
code({ text, lang }) {
if (lang.startsWith(`mermaid`)) {
setTimeout(() => {
window.mermaid?.run()
}, 0)
return `
${text}
`
}
const langText = lang.split(` `)[0]
const language = hljs.getLanguage(langText) ? langText : `plaintext`
let highlighted = hljs.highlight(text, { language }).value
highlighted = highlighted
.replace(/\r\n/g, `
`)
.replace(/\n/g, `
`)
.replace(/(>[^<]+)|(^[^<]+)/g, str => str.replace(/\s/g, ` `))
return `${highlighted}
`
}
codespan({ text }) {
return this.styledContent(`codespan`, text, `code`)
}
listitem(tokens, prefix) {
return `${prefix}${this.parser.parseInline(tokens)}`
}
list({ ordered, items }) {
const listItems = []
for (let i = 0; i < items.length; i++) {
const { tokens } = items[i]
const prefix = ordered ? `${i + 1}. ` : `• `
listItems.push(this.listitem(tokens, prefix))
}
const label = ordered ? `ol` : `ul`
return this.styledContent(label, listItems.join(``))
}
image({ href, title, text }) {
const createSubText = s =>
s ? `${s}` : ``
const transform
= {
'alt': () => text,
'title': () => title,
'alt-title': () => text || title,
'title-alt': () => title || text,
}[this.opts.legend] || (() => ``)
const subText = createSubText(transform())
const figureStyles = this.getStyles(`figure`)
const imgStyles = this.getStyles(`image`)
return `
${subText}`
}
link({ href, title, text }) {
if (href.startsWith(`https://mp.weixin.qq.com`)) {
return `${text}`
}
if (href === text) {
return text
}
if (this.opts.status) {
const ref = this.addFootnote(title || text, href)
return `${text}[${ref}]`
}
return this.styledContent(`link`, text, `span`)
}
strong({ text }) {
return this.styledContent(`strong`, text)
}
em({ text }) {
return `${text}`
}
table({ header, rows }) {
const headerRow = header.map(cell => this.styledContent(`td`, cell.text)).join(``)
const body = rows.map((row) => {
const rowContent = row.map(cell => this.styledContent(`td`, cell.text)).join(``)
return this.styledContent(`tr`, rowContent)
}).join(``)
return `
`
}
tablecell({ text }) {
return this.styledContent(`td`, text)
}
hr(_) {
return this.styledContent(`hr`, ``)
}
}
export default WxRenderer