From 089d3218981b12695ee79f9f85326a3a9a4bdbf5 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Sun, 7 May 2023 18:54:09 +0200 Subject: [PATCH] Syntax highlighting + fix escaping in Markdown code (#29) --- public/main.ts | 19 +++++++++++++++++-- public/style.css | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/public/main.ts b/public/main.ts index 2056ae3..a4b997e 100644 --- a/public/main.ts +++ b/public/main.ts @@ -1,7 +1,7 @@ +import 'highlight.js/styles/tokyo-night-dark.css'; import './style.css'; import './markdown.css'; import './favicon.svg'; -import 'highlight.js/styles/tokyo-night-dark.css'; import moment from 'moment'; import md from 'markdown-it'; import hljs from 'highlight.js'; @@ -31,7 +31,22 @@ document.addEventListener('DOMContentLoaded', () => { } document.querySelectorAll('.markdown').forEach((e: HTMLElement) => { - e.innerHTML = md().render(e.innerHTML); + e.innerHTML = md({ + html: true, + highlight: function (str, lang) { + if (lang && hljs.getLanguage(lang)) { + try { + console.log(str) + console.log(hljs.highlight(str, {language: lang, ignoreIllegals: false}, false)); + return '
' +
+ hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
+ '
';
+ } catch (__) {}
+ }
+
+ return '' + md().utils.escapeHtml(str) + '
';
+ }
+ }).render(e.textContent);
});
document.querySelectorAll