From a3936f2891a570f392d54863999ac8c4a1809d6a Mon Sep 17 00:00:00 2001 From: Eviee Py Date: Fri, 12 Jul 2024 17:36:18 +1000 Subject: [PATCH] Add languages by filename --- web/password.html | 1 + web/paste.html | 1 + web/static/scripts/highlights.js | 15 +++++++++++++-- web/static/scripts/highlightsHTMX.js | 14 ++++++++++++-- web/static/scripts/utils.js | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 web/static/scripts/utils.js diff --git a/web/password.html b/web/password.html index 4268b5e..f5e69c9 100644 --- a/web/password.html +++ b/web/password.html @@ -17,6 +17,7 @@ + diff --git a/web/paste.html b/web/paste.html index 64ee369..fe9f7bd 100644 --- a/web/paste.html +++ b/web/paste.html @@ -16,6 +16,7 @@ + diff --git a/web/static/scripts/highlights.js b/web/static/scripts/highlights.js index 6aa5517..8cf639a 100644 --- a/web/static/scripts/highlights.js +++ b/web/static/scripts/highlights.js @@ -6,11 +6,22 @@ let DlCount = 0; for (let area of HIGHLIGHT_AREAS) { let code = area.querySelector("pre > code"); + let name = area.querySelector(".pasteHeader > div > .filenameArea"); + pasteStores.push(code.textContent); // Highlight Code Block and get Language Details... - let details = hljs.highlightAuto(code.textContent); - let highlightedLang = details.language ? details.language : "plaintext"; + let nameLang = getLangByName(name.textContent); + let highlightedLang; + let details; + + if (!nameLang) { + details = hljs.highlightAuto(code.textContent); + highlightedLang = details.language ? details.language : "plaintext"; + } else { + details = hljs.highlight(code.textContent, { "language": nameLang }) + highlightedLang = nameLang.toLowerCase(); + } code.innerHTML = details.value; diff --git a/web/static/scripts/highlightsHTMX.js b/web/static/scripts/highlightsHTMX.js index a8e12fc..7072543 100644 --- a/web/static/scripts/highlightsHTMX.js +++ b/web/static/scripts/highlightsHTMX.js @@ -14,11 +14,21 @@ document.addEventListener("htmx:afterRequest", function (evt) { for (let area of HIGHLIGHT_AREAS) { let code = area.querySelector("pre > code"); + let name = area.querySelector(".pasteHeader > div > .filenameArea"); pasteStores.push(code.textContent); // Highlight Code Block and get Language Details... - let details = hljs.highlightAuto(code.textContent); - let highlightedLang = details.language ? details.language : "plaintext"; + let nameLang = getLangByName(name.textContent); + let highlightedLang; + let details; + + if (!nameLang) { + details = hljs.highlightAuto(code.textContent); + highlightedLang = details.language ? details.language : "plaintext"; + } else { + details = hljs.highlight(code.textContent, { "language": nameLang }) + highlightedLang = nameLang.toLowerCase(); + } code.innerHTML = details.value; diff --git a/web/static/scripts/utils.js b/web/static/scripts/utils.js new file mode 100644 index 0000000..0ac8b22 --- /dev/null +++ b/web/static/scripts/utils.js @@ -0,0 +1,14 @@ +function getLangByName(name) { + splat = name.split("."); + if (splat.length <= 1) { + return null + } + + ext = splat[splat.length - 1]; + lang = hljs.getLanguage(ext); + + if (!lang) { + return null + } + return lang.name; +} \ No newline at end of file