From a3936f2891a570f392d54863999ac8c4a1809d6a Mon Sep 17 00:00:00 2001 From: Eviee Py Date: Fri, 12 Jul 2024 17:36:18 +1000 Subject: [PATCH 1/5] 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 From 08bb196237053aa40f9d10d884c5c2a53ee3bfac Mon Sep 17 00:00:00 2001 From: Eviee Py Date: Mon, 15 Jul 2024 14:04:31 +1000 Subject: [PATCH 2/5] Remove the need for domain config --- config.template.toml | 1 - views/api.py | 4 ++-- views/htmx.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/config.template.toml b/config.template.toml index 860da18..939b932 100644 --- a/config.template.toml +++ b/config.template.toml @@ -1,7 +1,6 @@ [SERVER] host = "localhost" port = 8181 -domain = "https://mystb.in" session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64)) maintenance = false diff --git a/views/api.py b/views/api.py index 8527033..d72fec2 100644 --- a/views/api.py +++ b/views/api.py @@ -302,8 +302,8 @@ async def security_info(self, request: starlette_plus.Request) -> starlette_plus status_code=404, ) - delete: str = f"{CONFIG['SERVER']['domain']}/api/security/delete/{token}" - info: str = f"{CONFIG['SERVER']['domain']}/api/security/info/{token}" + delete: str = f"https://{request.url.hostname}/api/security/delete/{token}" + info: str = f"https://{request.url.hostname}/api/security/info/{token}" data: dict[str, str] = { "token": paste.safety, "delete": delete, diff --git a/views/htmx.py b/views/htmx.py index 336774e..f501bb4 100644 --- a/views/htmx.py +++ b/views/htmx.py @@ -225,7 +225,7 @@ async def paste_raw(self, request: starlette_plus.Request) -> starlette_plus.Res htmx_url: str | None = request.headers.get("HX-Current-URL", None) if identifier == "0" and htmx_url: - identifier = htmx_url.removeprefix(f'{CONFIG["SERVER"]["domain"]}/') + identifier = htmx_url.removeprefix(f"https://{request.url.hostname}/") headers: dict[str, str] = {"HX-Redirect": f"/raw/{identifier}"} paste = await self.app.database.fetch_paste(identifier, password=password) From 5b8751c66059c6c1ff1ef5e1801f4cbbcb6cb25f Mon Sep 17 00:00:00 2001 From: Eviee Py Date: Mon, 15 Jul 2024 14:04:46 +1000 Subject: [PATCH 3/5] Update readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 172a29b..f3c7f7b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ Easily share code and text. **Setup:** - Clone - Copy `config.template.toml` into `config.toml` - - For local testing `[SERVER] > domain` can be set to `http://localhost:PORT` (Default Port `8181`) - Set Database connection DSN. - Optionally set URLs to a running Redis Instance. - ! If you haven't already: Create a Database in `postgres` (Default `mystbin`) From 29e207c4d2f64d4e5b1741ea2898c1a7be566362 Mon Sep 17 00:00:00 2001 From: Eviee Py Date: Mon, 15 Jul 2024 14:08:53 +1000 Subject: [PATCH 4/5] Make fixes. --- web/static/scripts/highlights.js | 2 +- web/static/scripts/highlightsHTMX.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/web/static/scripts/highlights.js b/web/static/scripts/highlights.js index 8cf639a..24c4aee 100644 --- a/web/static/scripts/highlights.js +++ b/web/static/scripts/highlights.js @@ -17,7 +17,7 @@ for (let area of HIGHLIGHT_AREAS) { if (!nameLang) { details = hljs.highlightAuto(code.textContent); - highlightedLang = details.language ? details.language : "plaintext"; + highlightedLang = details.language || "plaintext"; } else { details = hljs.highlight(code.textContent, { "language": nameLang }) highlightedLang = nameLang.toLowerCase(); diff --git a/web/static/scripts/highlightsHTMX.js b/web/static/scripts/highlightsHTMX.js index 7072543..421c90c 100644 --- a/web/static/scripts/highlightsHTMX.js +++ b/web/static/scripts/highlightsHTMX.js @@ -24,7 +24,7 @@ document.addEventListener("htmx:afterRequest", function (evt) { if (!nameLang) { details = hljs.highlightAuto(code.textContent); - highlightedLang = details.language ? details.language : "plaintext"; + highlightedLang = details.language || "plaintext"; } else { details = hljs.highlight(code.textContent, { "language": nameLang }) highlightedLang = nameLang.toLowerCase(); From 68c40e72ac51372062c3e2cf44cd6f9b6964064c Mon Sep 17 00:00:00 2001 From: Eviee Py Date: Mon, 15 Jul 2024 14:12:38 +1000 Subject: [PATCH 5/5] Add proper scheme (not default to https) --- views/api.py | 4 ++-- views/htmx.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/views/api.py b/views/api.py index d72fec2..ba0d583 100644 --- a/views/api.py +++ b/views/api.py @@ -302,8 +302,8 @@ async def security_info(self, request: starlette_plus.Request) -> starlette_plus status_code=404, ) - delete: str = f"https://{request.url.hostname}/api/security/delete/{token}" - info: str = f"https://{request.url.hostname}/api/security/info/{token}" + delete: str = f"{request.url.scheme}://{request.url.hostname}/api/security/delete/{token}" + info: str = f"{request.url.scheme}://{request.url.hostname}/api/security/info/{token}" data: dict[str, str] = { "token": paste.safety, "delete": delete, diff --git a/views/htmx.py b/views/htmx.py index f501bb4..d9c42ea 100644 --- a/views/htmx.py +++ b/views/htmx.py @@ -225,7 +225,7 @@ async def paste_raw(self, request: starlette_plus.Request) -> starlette_plus.Res htmx_url: str | None = request.headers.get("HX-Current-URL", None) if identifier == "0" and htmx_url: - identifier = htmx_url.removeprefix(f"https://{request.url.hostname}/") + identifier = htmx_url.removeprefix(f"{request.url.scheme}://{request.url.hostname}/") headers: dict[str, str] = {"HX-Redirect": f"/raw/{identifier}"} paste = await self.app.database.fetch_paste(identifier, password=password)