naive-ui/build/loaders/md-renderer.js

95 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-12-22 15:31:04 +08:00
const hljs = require('highlight.js')
const marked = require('marked')
2019-12-22 23:19:08 +08:00
function createRenderer (wrapCodeWithCard = true) {
const renderer = new marked.Renderer()
const overrides = {
2020-02-20 16:26:34 +08:00
table (header, body) {
if (body) body = '<tbody>' + body + '</tbody>'
2020-12-24 00:29:04 +08:00
return (
2021-04-05 14:38:39 +08:00
'<div class="md-table-wrapper"><n-table single-column class="md-table">\n' +
'<thead>\n' +
2020-02-20 16:26:34 +08:00
header +
'</thead>\n' +
body +
2021-04-05 14:38:39 +08:00
'</n-table>\n' +
'</div>'
2020-12-24 00:29:04 +08:00
)
2020-02-20 16:26:34 +08:00
},
tablerow (content) {
return '<tr>\n' + content + '</tr>\n'
2020-02-20 16:26:34 +08:00
},
tablecell (content, flags) {
const type = flags.header ? 'th' : 'td'
const tag = flags.align
? '<' + type + ' align="' + flags.align + '">'
: '<' + type + '>'
2020-02-20 16:26:34 +08:00
return tag + content + '</' + type + '>\n'
},
2019-12-22 23:19:08 +08:00
code: (code, language) => {
2021-04-21 14:47:24 +08:00
if (language.startsWith('__')) {
language = language.replace('__', '')
}
2019-12-22 23:19:08 +08:00
const isLanguageValid = !!(language && hljs.getLanguage(language))
if (!isLanguageValid) {
2020-12-24 00:29:04 +08:00
throw new Error(
`MdRendererError: ${language} is not valid for code - ${code}`
)
2019-12-22 23:19:08 +08:00
}
const highlighted = hljs.highlight(code, { language }).value
const content = `<n-code><pre v-pre>${highlighted}</pre></n-code>`
2021-01-04 01:03:09 +08:00
return wrapCodeWithCard
? `<n-card size="small" class="md-card" content-style="padding: 0;">
<n-scrollbar x-scrollable content-style="padding: 12px; 16px;">
${content}
</n-scrollbar>
</n-card>`
2021-01-04 01:03:09 +08:00
: content
2019-12-22 23:19:08 +08:00
},
heading: (text, level) => {
const id = text.replace(/ /g, '-')
return `<n-h${level} id="${id}">${text}</n-h${level}>`
},
2020-12-24 00:29:04 +08:00
blockquote: (quote) => {
2019-12-22 23:19:08 +08:00
return `<n-blockquote>${quote}</n-blockquote>`
},
2020-11-03 15:10:29 +08:00
hr: () => '<n-hr />',
2020-12-24 00:29:04 +08:00
paragraph: (text) => {
2019-12-22 23:19:08 +08:00
return `<n-p>${text}</n-p>`
},
link (href, title, text) {
2021-01-02 16:33:23 +08:00
if (/^(http:|https:)/.test(href)) {
return `<n-a href="${href}" target="_blank">${text}</n-a>`
}
2021-06-16 23:06:21 +08:00
return `<router-link to="${href}" #="{ navigate, href }" custom><n-a :href="href" @click="navigate">${text}</n-a></router-link>`
2019-12-22 23:19:08 +08:00
},
list (body, ordered, start) {
2019-12-23 21:40:45 +08:00
const type = ordered ? 'n-ol' : 'n-ul'
2020-12-24 00:29:04 +08:00
const startatt = ordered && start !== 1 ? ' start="' + start + '"' : ''
2019-12-22 23:19:08 +08:00
return `<${type}${startatt}>\n` + body + `</${type}>\n`
},
listitem (text) {
return `<n-li>${text}</n-li>`
},
codespan (code) {
return `<n-text code>${code}</n-text>`
2020-01-30 23:35:57 +08:00
},
strong (text) {
return `<n-text strong>${text}</n-text>`
2020-11-16 02:12:03 +08:00
},
checkbox (checked) {
return `<n-checkbox :checked="${checked}" style="vertical-align: -2px; margin-right: 8px;" />`
2019-12-22 15:31:04 +08:00
}
}
2019-12-22 23:19:08 +08:00
2020-12-24 00:29:04 +08:00
Object.keys(overrides).forEach((key) => {
2019-12-23 21:40:45 +08:00
renderer[key] = overrides[key]
})
2019-12-22 23:19:08 +08:00
return renderer
2019-12-22 15:31:04 +08:00
}
2019-12-23 21:40:45 +08:00
module.exports = createRenderer