From b2e08063441f73ea5196e28910ff21047cdbbf8c Mon Sep 17 00:00:00 2001 From: Bobby Date: Mon, 21 Mar 2022 05:33:41 -0400 Subject: fix posts code rendering --- public/views/editPost.html | 4 +--- public/views/post.html | 7 +++++++ routes/posts.js | 23 +++++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/public/views/editPost.html b/public/views/editPost.html index 371b455..16c2bf4 100644 --- a/public/views/editPost.html +++ b/public/views/editPost.html @@ -129,9 +129,7 @@ - + \ No newline at end of file diff --git a/public/views/post.html b/public/views/post.html index e2f9bdc..7a799bb 100644 --- a/public/views/post.html +++ b/public/views/post.html @@ -36,6 +36,11 @@ pre { padding: 10px; } + img { + width: 80%; + display: block; + margin: 20px 0px; + } @@ -74,6 +79,8 @@ + + \ No newline at end of file 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( -- cgit v1.2.3