Skip to content

Commit

Permalink
Add languages by filename
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Jul 12, 2024
1 parent 5896861 commit a3936f2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions web/password.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!-- SCRIPTS -->
<script src="/static/scripts/initialTheme.js?v=1"></script>
<script src="/static/scripts/utils.js"></script>
<script src="/static/scripts/themes.js?v=1" defer></script>
<script src="/static/scripts/hidecopy.js?v=1" defer></script>
<script src="/static/scripts/highlightsHTMX.js?v=5"></script>
Expand Down
1 change: 1 addition & 0 deletions web/paste.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<!-- SCRIPTS -->
<script src="/static/scripts/initialTheme.js?v=1"></script>
<script src="/static/scripts/utils.js"></script>
<script src="/static/scripts/themes.js?v=1" defer></script>
<script src="/static/scripts/hidecopy.js?v=1" defer></script>
<script src="/static/scripts/highlights.js?v=5" defer></script>
Expand Down
15 changes: 13 additions & 2 deletions web/static/scripts/highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 12 additions & 2 deletions web/static/scripts/highlightsHTMX.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions web/static/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit a3936f2

Please sign in to comment.