import hljs from 'highlight.js' export function highlight(code, lang) { // Check if hljs recognises the language, else assume 'plaintext'. const language = hljs.getLanguage(lang) ? lang : 'plaintext' // Highlight it with the corresponding language. return hljs.highlight( // Switch out bullet points (•) to spaces (for proper indentation). code.replace(/•/g, " "), { language }) // Replace newlines with
tags. .value.replace(/\n/g, "
") // Replace spaces with ' ' to forcefully render them. .replace(/ /g, " " ) }