Dough Git

commit

Better handling

commit
aa2a67d43830cebdceee8d7822a190be0d047f16
author
Clove <[email protected]>
date
2026-11-25 00:55

diff --git a/src/lib/server/highlight.ts b/src/lib/server/highlight.ts
index c01ea99..20fc201 100644
--- a/src/lib/server/highlight.ts
+++ b/src/lib/server/highlight.ts
@@ -9,8 +9,6 @@ const MAX_HIGHLIGHT_LENGTH = 128 * 1024;
 
 hljs.registerLanguage("jpcl", jpcl);
 
-export const highlightLanguages = hljs.listLanguages().sort();
-
 const FILE_LANGUAGES = new Map([
   [".editorconfig", "ini"],
   [".gitconfig", "ini"],
@@ -38,12 +36,69 @@ const EXTENSION_LANGUAGES = new Map([
   ["zsh", "bash"],
 ]);
 
-export function fileLanguage(path: string): string | undefined {
+const INTERPRETER_LANGUAGES = new Map([
+  ["awk", "awk"],
+  ["bash", "bash"],
+  ["bun", "javascript"],
+  ["dash", "bash"],
+  ["gawk", "awk"],
+  ["groovy", "groovy"],
+  ["ksh", "bash"],
+  ["lua", "lua"],
+  ["luajit", "lua"],
+  ["mawk", "awk"],
+  ["node", "javascript"],
+  ["nodejs", "javascript"],
+  ["perl", "perl"],
+  ["php", "php"],
+  ["pwsh", "powershell"],
+  ["pypy", "python"],
+  ["python", "python"],
+  ["Rscript", "r"],
+  ["ruby", "ruby"],
+  ["sh", "bash"],
+  ["tclsh", "tcl"],
+  ["zsh", "bash"],
+]);
+
+function shebangLanguage(text: string): string | undefined {
+  if (!text.startsWith("#!")) return undefined;
+
+  const firstLine = text.slice(2, 512).split(/\r?\n/, 1)[0] ?? "";
+  const words = firstLine.match(/[^ \t]+/g) ?? [];
+  let command = words[0]?.split("/").pop();
+
+  if (command === "env") {
+    let index = 1;
+    while (index < words.length) {
+      const word = words[index++];
+      if (word === "--") break;
+      if (word === "-S" || word === "--split-string" || word === "-i" || word === "--ignore-environment") {
+        continue;
+      }
+      if (word === "-u" || word === "--unset") {
+        index++;
+        continue;
+      }
+      if (/^[A-Za-z_]\w*=/.test(word ?? "")) continue;
+      index--;
+      break;
+    }
+    command = words[index]?.split("/").pop();
+  }
+
+  return command && INTERPRETER_LANGUAGES.get(command.replace(/\d+(?:\.\d+)*$/, ""));
+}
+
+export function fileLanguage(path: string, text: string): string | undefined {
   const filename = (path.split("/").pop() ?? "").toLowerCase();
   const extension = /\.([^.]+)$/.exec(filename)?.[1];
 
-  return FILE_LANGUAGES.get(filename)
+  const namedLanguage = FILE_LANGUAGES.get(filename)
     ?? (extension && (EXTENSION_LANGUAGES.get(extension) ?? extension));
+  if (namedLanguage && hljs.getLanguage(namedLanguage)) return namedLanguage;
+
+  return shebangLanguage(text);
 }
 
 export function highlightCode(text: string, language?: string): string | null {
diff --git a/src/routes/[owner]/[name=repo]/blob/[...path]/+page.server.ts b/src/routes/[owner]/[name=repo]/blob/[...path]/+page.server.ts
index 065c9e5..7d08479 100644
--- a/src/routes/[owner]/[name=repo]/blob/[...path]/+page.server.ts
+++ b/src/routes/[owner]/[name=repo]/blob/[...path]/+page.server.ts
@@ -4,7 +4,7 @@
 
 import type { PageServerLoad } from "./$types";
 import { blob } from "#lib/server/git.ts";
-import { fileLanguage, highlightCode, highlightLanguages } from "#lib/server/highlight.ts";
+import { fileLanguage, highlightCode } from "#lib/server/highlight.ts";
 import { renderMarkdown, repositoryLinks } from "#lib/server/markdown.ts";
 import { notFound, revQuery, viewable } from "#lib/server/web.ts";
 
@@ -16,8 +16,6 @@ export const load: PageServerLoad = async (event) => {
   if (!file) notFound();
 
   const query = revQuery(rev, refs.head);
-  const requestedLanguage = event.url.searchParams.get("language") ?? "";
-  const language = highlightLanguages.includes(requestedLanguage) ? requestedLanguage : "";
   const isMarkdown = !file.binary && /\.(?:md|markdown|mdown|mkd|mkdn)$/i.test(path);
   const showSource = event.url.searchParams.get("view") === "source";
   const preview = isMarkdown && !showSource
@@ -30,13 +28,10 @@ export const load: PageServerLoad = async (event) => {
     canManage: access === "write",
     path,
     query,
-    revision: query ? rev : "",
     isMarkdown,
     preview,
-    language,
-    languages: !file.binary && preview === null ? highlightLanguages : [],
     highlighted: !file.binary && preview === null
-      ? highlightCode(file.text, language || fileLanguage(path))
+      ? highlightCode(file.text, fileLanguage(path, file.text))
       : null,
     ...file,
   };
diff --git a/src/routes/[owner]/[name=repo]/blob/[...path]/+page.svelte b/src/routes/[owner]/[name=repo]/blob/[...path]/+page.svelte
index 4434da4..d80e80c 100644
--- a/src/routes/[owner]/[name=repo]/blob/[...path]/+page.svelte
+++ b/src/routes/[owner]/[name=repo]/blob/[...path]/+page.svelte
@@ -61,25 +61,6 @@
       <Tabs active={data.preview === null ? "source" : "preview"} items={views} />
     {/if}
 
-    {#if data.languages.length}
-      <form class="language-picker" method="GET">
-        {#if data.revision}
-          <input type="hidden" name="h" value={data.revision} />
-        {/if}
-        {#if data.isMarkdown}
-          <input type="hidden" name="view" value="source" />
-        {/if}
-        <label for="source-language">Language</label>
-        <select id="source-language" name="language" value={data.language}>
-          <option value="">Automatic</option>
-          {#each data.languages as language (language)}
-            <option value={language}>{language}</option>
-          {/each}
-        </select>
-        <button class="btn small" type="submit">Apply</button>
-      </form>
-    {/if}
-
     {#if data.preview !== null}
       <article class="readme">{@html data.preview}</article>
     {:else if data.highlighted !== null}
@@ -99,11 +80,4 @@
   p {
     margin: 0;
   }
-
-  .language-picker {
-    display: flex;
-    align-items: center;
-    flex-wrap: wrap;
-    gap: 0.5rem;
-  }
 </style>
diff --git a/test/highlight.test.mjs b/test/highlight.test.mjs
new file mode 100644
index 0000000..f51408e
--- /dev/null
+++ b/test/highlight.test.mjs
@@ -0,0 +1,18 @@
+/* test/highlight.test.mjs
+ * LICENCED DASL-1.0 (c) Clove Twilight
+ */
+
+import assert from "node:assert/strict";
+import { fileLanguage, highlightCode } from "../src/lib/server/highlight.ts";
+
+assert.equal(fileLanguage("bin/start", "#!/bin/sh\nexit 0"), "bash");
+assert.equal(fileLanguage("bin/task", "#!/usr/bin/env node\n"), "javascript");
+assert.equal(fileLanguage("tool.unknown", "#!/usr/bin/env -S python3.14 -u\r\n"), "python");
+assert.equal(fileLanguage("tool", "#!/usr/bin/env -S MODE=dev /opt/bin/lua5.4\n"), "lua");
+assert.equal(fileLanguage("tool", "#!/usr/bin/env -S -u MODE -- ruby\n"), "ruby");
+assert.equal(fileLanguage("config.jp", "#!/usr/bin/env node\n"), "jpcl");
+assert.equal(fileLanguage("tool", "#!/usr/bin/env unsupported\n"), undefined);
+assert.equal(fileLanguage("tool", "plain text\n#!/bin/sh\n"), undefined);
+
+const python = "#!/usr/bin/python3\nif True:\n    print('hello')\n";
+assert.match(highlightCode(python, fileLanguage("bin/hello", python)), /hljs-keyword/);