diff options
| author | Bobby <[email protected]> | 2022-03-20 00:34:17 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-03-20 00:34:17 -0400 |
| commit | cbac553ec36b33e4036469cb3643bb4c8872c60d (patch) | |
| tree | c83420a2f597fa9f9f695ff021fa8ee07620f8a9 | |
| parent | a0253ed2ce318c52944363396cbd181ba6ca05d4 (diff) | |
| download | luciferreeves.github.io-cbac553ec36b33e4036469cb3643bb4c8872c60d.tar.xz luciferreeves.github.io-cbac553ec36b33e4036469cb3643bb4c8872c60d.zip | |
fix code highlight
| -rw-r--r-- | routes/posts.js | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/routes/posts.js b/routes/posts.js index 583d38d..00b092f 100644 --- a/routes/posts.js +++ b/routes/posts.js @@ -1,10 +1,10 @@ const firebase = require("../firebase"); const fs = require("fs"); const cheerio = require("cheerio"); -const marked = require("marked"); -const hljs = require("highlight.js"); const express = require("express"); const router = express.Router(); +const marked = require("marked"); +const hljs = require("highlight.js"); router.get("/posts/:id", function (req, res) { const id = req.params.id; @@ -27,16 +27,12 @@ router.get("/posts/:id", function (req, res) { // convert content from base64 to utf8 const content = Buffer.from(post.content, "base64").toString("utf8"); // Parse the markdown - const markdown = marked.parse(content); - const markdownWithHighlight = markdown.replace( - /<pre><code class="(.*?)">(.*?)<\/code><\/pre>/g, - (match, p1, p2) => { - return `<pre><code class="hljs ${p1}">${ - hljs.highlight(p1, p2).value - }</code></pre>`; - } - ); - $("#content").html(markdownWithHighlight); + const parsed = marked.parse(content, { + highlight: function (code) { + return hljs.highlightAuto(code).value; + }, + }); + $("#content").html(parsed); $("#publishDate").text(post.publishDate); post.tags.forEach((tag) => { $("#tags").append( |
