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-21 15:26:51 +08:00
|
|
|
table (header, body) {
|
|
|
|
if (body) body = '<tbody class="n-table__tbody">' + body + '</tbody>'
|
2020-02-24 16:25:02 +08:00
|
|
|
return '<n-table single-column>\n' +
|
2020-02-21 15:26:51 +08:00
|
|
|
'<thead class="n-table__thead">\n' +
|
|
|
|
header +
|
|
|
|
'</thead>\n' +
|
|
|
|
body +
|
|
|
|
'</n-table>\n'
|
|
|
|
},
|
|
|
|
|
|
|
|
tablerow (content) {
|
|
|
|
return '<tr class="n-table__tr">\n' + content + '</tr>\n'
|
|
|
|
},
|
|
|
|
|
|
|
|
tablecell (content, flags) {
|
|
|
|
const type = flags.header ? 'th' : 'td'
|
|
|
|
const tag = flags.align
|
|
|
|
? '<' + type + ` class="n-table__${type}"` + ' align="' + flags.align + '">'
|
|
|
|
: '<' + type + ` class="n-table__${type}"` + '>'
|
|
|
|
return tag + content + '</' + type + '>\n'
|
|
|
|
},
|
|
|
|
|
2019-12-22 23:19:08 +08:00
|
|
|
code: (code, language) => {
|
|
|
|
const isLanguageValid = !!(language && hljs.getLanguage(language))
|
|
|
|
if (!isLanguageValid) {
|
|
|
|
throw new Error(`MdRendererError: ${language} is not valid for code`)
|
|
|
|
}
|
|
|
|
const highlighted = hljs.highlight(language, code).value
|
2020-02-24 16:25:02 +08:00
|
|
|
return `${wrapCodeWithCard ? '<n-card size="small">' : ''}<n-config-consumer abstract>
|
2019-12-22 23:19:08 +08:00
|
|
|
<template v-slot="{ theme }">
|
2019-12-23 15:01:38 +08:00
|
|
|
<pre class="n-code" :class="'n-' + theme + '-theme'"><code v-pre>${highlighted}</code></pre>
|
2019-12-22 23:19:08 +08:00
|
|
|
</template>
|
|
|
|
</n-config-consumer>${wrapCodeWithCard ? '</n-card>' : ''}`
|
|
|
|
},
|
|
|
|
heading: (text, level) => {
|
|
|
|
const id = text.replace(/ /g, '-')
|
|
|
|
return `<n-h${level} id="${id}">${text}</n-h${level}>`
|
|
|
|
},
|
|
|
|
blockquote: quote => {
|
|
|
|
return `<n-blockquote>${quote}</n-blockquote>`
|
|
|
|
},
|
|
|
|
hr: () => `<n-hr />`,
|
|
|
|
paragraph: text => {
|
|
|
|
return `<n-p>${text}</n-p>`
|
|
|
|
},
|
|
|
|
link (href, title, text) {
|
2020-01-23 17:24:48 +08:00
|
|
|
return `<n-a to="${href}" >${text}</n-a>`
|
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'
|
|
|
|
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>`
|
2019-12-22 15:31:04 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 23:19:08 +08:00
|
|
|
|
2019-12-23 21:40:45 +08:00
|
|
|
Object.keys(overrides).forEach(key => {
|
|
|
|
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
|