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 + '\n' }, 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 ? '' : ''} ${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 + `\n` }, listitem (text) { return `${text}` }, codespan (code) { return `${code}` }, strong (text) { return `${text}` } } Object.keys(overrides).forEach(key => { renderer[key] = overrides[key] }) return renderer } module.exports = createRenderer