Skip to content

Commit 02a2c45

Browse files
authored
Add languages by filename (#55)
* Add languages by filename * Remove the need for domain config * Update readme * Make fixes. * Add proper scheme (not default to https)
1 parent 5896861 commit 02a2c45

File tree

9 files changed

+44
-9
lines changed

9 files changed

+44
-9
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Easily share code and text.
1616
**Setup:**
1717
- Clone
1818
- Copy `config.template.toml` into `config.toml`
19-
- For local testing `[SERVER] > domain` can be set to `http://localhost:PORT` (Default Port `8181`)
2019
- Set Database connection DSN.
2120
- Optionally set URLs to a running Redis Instance.
2221
- ! If you haven't already: Create a Database in `postgres` (Default `mystbin`)

config.template.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[SERVER]
22
host = "localhost"
33
port = 8181
4-
domain = "https://mystb.in"
54
session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64))
65
maintenance = false
76

views/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ async def security_info(self, request: starlette_plus.Request) -> starlette_plus
302302
status_code=404,
303303
)
304304

305-
delete: str = f"{CONFIG['SERVER']['domain']}/api/security/delete/{token}"
306-
info: str = f"{CONFIG['SERVER']['domain']}/api/security/info/{token}"
305+
delete: str = f"{request.url.scheme}://{request.url.hostname}/api/security/delete/{token}"
306+
info: str = f"{request.url.scheme}://{request.url.hostname}/api/security/info/{token}"
307307
data: dict[str, str] = {
308308
"token": paste.safety,
309309
"delete": delete,

views/htmx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ async def paste_raw(self, request: starlette_plus.Request) -> starlette_plus.Res
225225

226226
htmx_url: str | None = request.headers.get("HX-Current-URL", None)
227227
if identifier == "0" and htmx_url:
228-
identifier = htmx_url.removeprefix(f'{CONFIG["SERVER"]["domain"]}/')
228+
identifier = htmx_url.removeprefix(f"{request.url.scheme}://{request.url.hostname}/")
229229

230230
headers: dict[str, str] = {"HX-Redirect": f"/raw/{identifier}"}
231231
paste = await self.app.database.fetch_paste(identifier, password=password)

web/password.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<!-- SCRIPTS -->
1919
<script src="/static/scripts/initialTheme.js?v=1"></script>
20+
<script src="/static/scripts/utils.js"></script>
2021
<script src="/static/scripts/themes.js?v=1" defer></script>
2122
<script src="/static/scripts/hidecopy.js?v=1" defer></script>
2223
<script src="/static/scripts/highlightsHTMX.js?v=5"></script>

web/paste.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<!-- SCRIPTS -->
1818
<script src="/static/scripts/initialTheme.js?v=1"></script>
19+
<script src="/static/scripts/utils.js"></script>
1920
<script src="/static/scripts/themes.js?v=1" defer></script>
2021
<script src="/static/scripts/hidecopy.js?v=1" defer></script>
2122
<script src="/static/scripts/highlights.js?v=5" defer></script>

web/static/scripts/highlights.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@ let DlCount = 0;
66

77
for (let area of HIGHLIGHT_AREAS) {
88
let code = area.querySelector("pre > code");
9+
let name = area.querySelector(".pasteHeader > div > .filenameArea");
10+
911
pasteStores.push(code.textContent);
1012

1113
// Highlight Code Block and get Language Details...
12-
let details = hljs.highlightAuto(code.textContent);
13-
let highlightedLang = details.language ? details.language : "plaintext";
14+
let nameLang = getLangByName(name.textContent);
15+
let highlightedLang;
16+
let details;
17+
18+
if (!nameLang) {
19+
details = hljs.highlightAuto(code.textContent);
20+
highlightedLang = details.language || "plaintext";
21+
} else {
22+
details = hljs.highlight(code.textContent, { "language": nameLang })
23+
highlightedLang = nameLang.toLowerCase();
24+
}
1425

1526
code.innerHTML = details.value;
1627

web/static/scripts/highlightsHTMX.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ document.addEventListener("htmx:afterRequest", function (evt) {
1414

1515
for (let area of HIGHLIGHT_AREAS) {
1616
let code = area.querySelector("pre > code");
17+
let name = area.querySelector(".pasteHeader > div > .filenameArea");
1718
pasteStores.push(code.textContent);
1819

1920
// Highlight Code Block and get Language Details...
20-
let details = hljs.highlightAuto(code.textContent);
21-
let highlightedLang = details.language ? details.language : "plaintext";
21+
let nameLang = getLangByName(name.textContent);
22+
let highlightedLang;
23+
let details;
24+
25+
if (!nameLang) {
26+
details = hljs.highlightAuto(code.textContent);
27+
highlightedLang = details.language || "plaintext";
28+
} else {
29+
details = hljs.highlight(code.textContent, { "language": nameLang })
30+
highlightedLang = nameLang.toLowerCase();
31+
}
2232

2333
code.innerHTML = details.value;
2434

web/static/scripts/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getLangByName(name) {
2+
splat = name.split(".");
3+
if (splat.length <= 1) {
4+
return null
5+
}
6+
7+
ext = splat[splat.length - 1];
8+
lang = hljs.getLanguage(ext);
9+
10+
if (!lang) {
11+
return null
12+
}
13+
return lang.name;
14+
}

0 commit comments

Comments
 (0)