const hljs = require('highlight.js')
const marked = require('marked')
function createRenderer (wrapCodeWithCard = true) {
const renderer = new marked.Renderer()
const overrides = {
table (header, body) {
if (body) body = '
' + body + ''
return (
'\n' +
'\n' +
header +
'\n' +
body +
'\n' +
'
'
)
},
tablerow (content) {
return '\n' + content + '
\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'
},
code: (code, language) => {
const isLanguageValid = !!(language && hljs.getLanguage(language))
if (!isLanguageValid) {
throw new Error(
`MdRendererError: ${language} is not valid for code - ${code}`
)
}
const highlighted = hljs.highlight(code, { language }).value
const content = `${highlighted}
`
return wrapCodeWithCard
? `${content}`
: content
},
heading: (text, level) => {
const id = text.replace(/ /g, '-')
return `${text}`
},
blockquote: (quote) => {
return `${quote}`
},
hr: () => '',
paragraph: (text) => {
return `${text}`
},
link (href, title, text) {
if (/^(http:|https:)/.test(href)) {
return `${text}`
}
return `${text}`
},
list (body, ordered, start) {
const type = ordered ? 'n-ol' : 'n-ul'
const startatt = ordered && start !== 1 ? ' start="' + start + '"' : ''
return `<${type}${startatt}>\n` + body + `${type}>\n`
},
listitem (text) {
return `${text}`
},
codespan (code) {
return `${code}`
},
strong (text) {
return `${text}`
},
checkbox (checked) {
return ``
}
}
Object.keys(overrides).forEach((key) => {
renderer[key] = overrides[key]
})
return renderer
}
module.exports = createRenderer