From 3a0af4ace27d217146a0a1ab3a7a51560dba88c2 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 10 Jun 2024 16:32:52 -0400 Subject: [PATCH] Adding syntax highlights --- app.js | 19 +++++++++++++++++-- index.html | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 6c3dbf7..de456ba 100644 --- a/app.js +++ b/app.js @@ -162,6 +162,11 @@ async function initialize() { swarm.on('close', () => { console.log('Swarm closed'); }); + + // Initialize highlight.js once the DOM is fully loaded + document.addEventListener("DOMContentLoaded", (event) => { + hljs.highlightAll(); + }); } function registerUser(e) { @@ -408,7 +413,17 @@ function onMessageAdded(from, message, avatar) { const $text = document.createElement('div'); $text.classList.add('message-text'); - const md = window.markdownit(); + const md = window.markdownit({ + highlight: function (str, lang) { + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(str, { language: lang }).value; + } catch (__) {} + } + return ''; // use external default escaping + } + }); + const markdownContent = md.render(message); $text.innerHTML = markdownContent; @@ -462,4 +477,4 @@ function updatePortInUrl(url) { return urlObject.toString(); } -initialize(); \ No newline at end of file +initialize(); diff --git a/index.html b/index.html index ffe7718..f1b1e9e 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,8 @@ + +