Add code highlighting using highlight.js

This commit is contained in:
Jackson Harper
2022-02-26 14:57:59 -08:00
parent 766bc285cf
commit 84fbc9cd27
5 changed files with 37 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import { WikipediaHandler } from './wikipedia-handler'
import { SubstackHandler } from './substack-handler'
import { AxiosHandler } from './axios-handler'
import { BloombergHandler } from './bloomberg-handler'
import * as hljs from 'highlightjs'
const logger = buildLogger('utils.parse')
@ -254,6 +255,22 @@ export const parsePreparedContent = async (
const jsonLdLinkMetadata = await getJSONLdLinkMetadata(window.document)
logRecord.JSONLdParsed = jsonLdLinkMetadata
if (article?.content) {
const cWindow = new JSDOM(article?.content).window
cWindow.document.querySelectorAll('code').forEach((e) => {
console.log(e.textContent)
if (e.textContent) {
const att = hljs.highlightAuto(e.textContent)
const code = window.document.createElement('code')
const langClass = `hljs language-${att.language}` + att.second_best?.language ? ` language-${att.second_best?.language}` : ''
code.setAttribute('class', langClass)
code.innerHTML = att.value
e.replaceWith(code)
}
})
article.content = cWindow.document.body.outerHTML
}
Object.assign(article, {
content: clean,
title: article?.title || jsonLdLinkMetadata.title,