const hljs = require('highlight.js')
const marked = require('marked')
function createRenderer (wrapCodeWithCard = true) {
const renderer = new marked.Renderer()
const overrides = {
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
return `${wrapCodeWithCard ? '' : ''}
${highlighted}
${wrapCodeWithCard ? '' : ''}`
},
heading: (text, level) => {
const id = text.replace(/ /g, '-')
return `${text}`
},
blockquote: quote => {
return `${quote}`
},
hr: () => ``,
paragraph: text => {
return `${text}`
},
link (href, title, 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}`
}
}
Object.keys(overrides).forEach(key => {
renderer[key] = overrides[key]
})
return renderer
}
module.exports = createRenderer