diff options
| author | Bobby <[email protected]> | 2022-03-21 05:33:41 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-03-21 05:33:41 -0400 |
| commit | b2e08063441f73ea5196e28910ff21047cdbbf8c (patch) | |
| tree | 84ec0273b79b66961417d737a2e2f8444ce70528 /routes/posts.js | |
| parent | d8363271eef45971d835d9abe9553d9894664f2a (diff) | |
| download | luciferreeves.github.io-b2e08063441f73ea5196e28910ff21047cdbbf8c.tar.xz luciferreeves.github.io-b2e08063441f73ea5196e28910ff21047cdbbf8c.zip | |
fix posts code rendering
Diffstat (limited to 'routes/posts.js')
| -rw-r--r-- | routes/posts.js | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/routes/posts.js b/routes/posts.js index 2d00202..601b09c 100644 --- a/routes/posts.js +++ b/routes/posts.js @@ -4,7 +4,6 @@ const cheerio = require("cheerio"); 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; @@ -26,13 +25,25 @@ router.get("/posts/:id", function (req, res) { $("#title").text(post.title); // convert content from base64 to utf8 const content = Buffer.from(post.content, "base64").toString("utf8"); - // Parse the markdown - const parsed = marked.parse(content, { - highlight: function (code) { - return hljs.highlightAuto(code).value; + // Parse the markdown and highlight the code + const renderPreview = $("#content"); + marked.setOptions({ + renderer: new marked.Renderer(), + highlight: function(code, lang) { + const hljs = require('highlight.js'); + const language = hljs.getLanguage(lang) ? lang : 'plaintext'; + return hljs.highlight(code, { language }).value; }, + langPrefix: 'hljs language-', + pedantic: false, + gfm: true, + breaks: false, + sanitize: false, + smartLists: true, + smartypants: false, + xhtml: false, }); - $("#content").html(parsed); + renderPreview.html(marked.parse(content)); $("#publishDate").text(post.publishDate); post.tags.forEach((tag) => { $("#tags").append( |
