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('.table-code').forEach((el) => { diff --git a/public/style.css b/public/style.css index 2794a6e..e59be3d 100644 --- a/public/style.css +++ b/public/style.css @@ -99,8 +99,8 @@ pre { max-height: 337px; } -.hljs { - background: none !important; +.hljs{ + color: #c9d1d9; } .line-code.selected { @@ -134,4 +134,4 @@ table.csv-table thead tr th { table.csv-table tbody td { @apply border py-1.5 px-1 border-slate-800; -} \ No newline at end of file +}