Skip to content

Commit 1aa5957

Browse files
committed
feat: add prismjs support
1 parent 37ad26f commit 1aa5957

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

pages/api/prismjs.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,28 @@ export default async function handler(
2929
const body = req.body
3030
let html = body.html
3131

32-
// properly parse and format all hightlighted
33-
// code blocks on the server
34-
const $ = cheerio.load(html)
35-
const $codes = $("code[class^=language]")
36-
if ($codes.length > 0)
37-
$codes.each(function () {
38-
const $code = $(this)
39-
40-
const lang = $code.attr("class")?.replace("language-", "") ?? "text"
41-
const code = decode($code.html())
42-
// default loaded languages with prisma, skip to decrease build times
43-
if (!["clike", "markup"].includes(lang)) {
44-
loadLanguages([lang])
45-
}
46-
$code.html(Prism.highlight(code, Prism.languages[lang], code))
47-
$code.addClass(`prism language-${lang}`)
48-
html = $.html()
49-
})
32+
try {
33+
// properly parse and format all hightlighted
34+
// code blocks on the server
35+
const $ = cheerio.load(html)
36+
const $codes = $("code[class^=language]")
37+
if ($codes.length > 0)
38+
$codes.each(function () {
39+
const $code = $(this)
40+
41+
const lang = $code.attr("class")?.replace("language-", "") ?? "text"
42+
const code = decode($code.html())
43+
// default loaded languages with prisma, skip to decrease build times
44+
if (!["clike", "markup"].includes(lang)) {
45+
loadLanguages([lang])
46+
}
47+
$code.html(Prism.highlight(code, Prism.languages[lang], code))
48+
$code.addClass(`prism language-${lang}`)
49+
html = $.html()
50+
})
51+
} catch (e) {
52+
console.error("prismjs parse error", e)
53+
}
5054

5155
json = {
5256
html: html,

0 commit comments

Comments
 (0)