Dough Git

commit

Repair MD code

commit
a54bc24dbbc8ee09ebc272a4a1cceef30720dae4
author
Clove <[email protected]>
date
2026-11-24 14:33

diff --git a/src/lib/server/markdown.ts b/src/lib/server/markdown.ts
index c57004c..73f032f 100644
--- a/src/lib/server/markdown.ts
+++ b/src/lib/server/markdown.ts
@@ -43,6 +43,26 @@ function safeUrl(raw: string): string | null {
   return url;
 }
 
+type ResolveLink = (href: string) => string;
+
+export function repositoryLinks(owner: string, repo: string, revisionQuery = ""): ResolveLink {
+  const repoPath = `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`;
+  const revision = escapeHtml(revisionQuery);
+
+  return (href) => {
+    if (/^[/\\?#]/.test(href) || ABSOLUTE.test(href)) return href;
+
+    const [, relativePath, search, fragment] = /^([^?#]*)(\?[^#]*)?(#.*)?$/.exec(href) ?? [];
+    if (!relativePath) return href;
+
+    const { pathname } = new URL(`./${relativePath}`, "https://repository.invalid/");
+    const query = revision && search
+      ? `${revision}&amp;${search.slice(1)}`
+      : revision || search || "";
+    return `${repoPath}/blob${pathname}${query}${fragment ?? ""}`;
+  };
+}
+
 function linkAttrs(href: string): string {
   return ABSOLUTE.test(href) ? ` rel="nofollow noopener noreferrer"` : "";
 }
@@ -209,7 +229,7 @@ function attrValue(name: string, raw: string | undefined): string | null {
   }
 }
 
-function rebuildTag(closing: string, rawName: string, rawAttrs: string): string {
+function rebuildTag(closing: string, rawName: string, rawAttrs: string, resolveLink?: ResolveLink): string {
   const name = rawName.toLowerCase();
   const allowed = HTML_ATTRS[name];
   if (!allowed) return "";
@@ -227,7 +247,8 @@ function rebuildTag(closing: string, rawName: string, rawAttrs: string): string
     }
     const attr = m[1]!.toLowerCase();
     if (!allowed.includes(attr) || seen.has(attr)) continue;
-    const value = attrValue(attr, m[2] ?? m[3] ?? m[4]);
+    const safeValue = attrValue(attr, m[2] ?? m[3] ?? m[4]);
+    const value = attr === "href" && safeValue && resolveLink ? resolveLink(safeValue) : safeValue;
     if (value === null) continue;
     seen.add(attr);
     if (attr === "href") href = value;
@@ -303,7 +324,7 @@ function LINK_RE(bang: string): RegExp {
   );
 }
 
-function inline(src: string): string {
+function inline(src: string, resolveLink?: ResolveLink): string {
   const s: Stash = [];
   let text = escapeHtml(src.replace(/\x00/g, ""));
 
@@ -316,7 +337,7 @@ function inline(src: string): string {
 
   HTML_TAG.lastIndex = 0;
   text = text.replace(HTML_TAG, (_m, closing: string, name: string, attrs: string) => {
-    const tag = rebuildTag(closing, name, attrs);
+    const tag = rebuildTag(closing, name, attrs, resolveLink);
     return tag ? park(s, tag) : "";
   });
 
@@ -334,8 +355,9 @@ function inline(src: string): string {
     LINK_RE(""),
     (_m, label: string, url: string, title?: string) => {
       const inner = emphasis(label);
-      const href = safeUrl(url);
-      if (!href) return inner;
+      const safeHref = safeUrl(url);
+      if (!safeHref) return inner;
+      const href = resolveLink ? resolveLink(safeHref) : safeHref;
       const t = title ? ` title="${title}"` : "";
       return park(s, `<a href="${href}"${t}${linkAttrs(href)}>${inner}</a>`);
     },
@@ -355,11 +377,11 @@ function inline(src: string): string {
   return unpark(emphasis(text), s);
 }
 
-function paragraph(lines: string[]): string {
+function paragraph(lines: string[], resolveLink?: ResolveLink): string {
   return lines
     .map((line, i) => {
       const hard = i < lines.length - 1 && /(?: {2,}|\\)$/.test(line);
-      return inline(line.replace(/(?: +|\\)$/, "")) + (hard ? "<br>" : "");
+      return inline(line.replace(/(?: +|\\)$/, ""), resolveLink) + (hard ? "<br>" : "");
     })
     .join("\n");
 }
@@ -391,12 +413,12 @@ const ALERT_ICON: Record<string, string> = {
   aside: '<path fill="currentColor" d="M14.5 17c0 1.65-1.35 3-3 3s-3-1.35-3-3h2c0 .55.45 1 1 1s1-.45 1-1s-.45-1-1-1H2v-2h9.5c1.65 0 3 1.35 3 3M19 6.5C19 4.57 17.43 3 15.5 3S12 4.57 12 6.5h2c0-.83.67-1.5 1.5-1.5s1.5.67 1.5 1.5S16.33 8 15.5 8H2v2h13.5c1.93 0 3.5-1.57 3.5-3.5m-.5 4.5H2v2h16.5c.83 0 1.5.67 1.5 1.5s-.67 1.5-1.5 1.5v2c1.93 0 3.5-1.57 3.5-3.5S20.43 11 18.5 11"/>',
 };
 
-function alertBlock(kind: string, body: string[], depth: number): string {
+function alertBlock(kind: string, body: string[], depth: number, resolveLink?: ResolveLink): string {
   const key = kind.toLowerCase();
   const label = key.charAt(0).toUpperCase() + key.slice(1);
   const icon = ALERT_ICON[key] ?? ALERT_ICON.note;
   const glyph = `<svg class="icon" width="16" height="16" viewBox="0 0 24 24" aria-hidden="true">${icon}</svg>`;
-  const inner = blocks(body, depth + 1);
+  const inner = blocks(body, resolveLink, depth + 1);
   const title = `<p class="md-alert-title">${glyph}${label}</p>`;
   return `<div class="md-alert md-alert-${key}">\n${title}\n${inner}\n</div>`;
 }
@@ -458,7 +480,7 @@ function alignOf(cell: string): string {
   return "";
 }
 
-function table(lines: string[], start: number): [string, number] {
+function table(lines: string[], start: number, resolveLink?: ResolveLink): [string, number] {
   const heads = splitRow(at(lines, start));
   const aligns = splitRow(at(lines, start + 1)).map(alignOf);
   let i = start + 2;
@@ -468,14 +490,14 @@ function table(lines: string[], start: number): [string, number] {
     rows.push(
       `<tr>${heads
         .map(
-          (_h, n) => `<td${aligns[n] ?? ""}>${inline(cells[n] ?? "")}</td>`,
+          (_h, n) => `<td${aligns[n] ?? ""}>${inline(cells[n] ?? "", resolveLink)}</td>`,
         )
         .join("")}</tr>`,
     );
     i++;
   }
   const head = heads
-    .map((h, n) => `<th${aligns[n] ?? ""}>${inline(h)}</th>`)
+    .map((h, n) => `<th${aligns[n] ?? ""}>${inline(h, resolveLink)}</th>`)
     .join("");
   return [
     `<table><thead><tr>${head}</tr></thead><tbody>${rows.join("")}</tbody></table>`,
@@ -483,7 +505,7 @@ function table(lines: string[], start: number): [string, number] {
   ];
 }
 
-function list(lines: string[], start: number, depth: number): [string, number] {
+function list(lines: string[], start: number, depth: number, resolveLink?: ResolveLink): [string, number] {
   const first = LIST_ITEM.exec(at(lines, start));
   if (!first) return ["", start + 1];
   const bullet = first[2] ?? "-";
@@ -535,7 +557,7 @@ function list(lines: string[], start: number, depth: number): [string, number] {
       const checkbox = task
         ? `<input type="checkbox" disabled${task[1] === " " ? "" : " checked"}> `
         : "";
-      const rendered = blocks(body, depth + 1).replace(/^<p>([\s\S]*?)<\/p>(?=\n<|$)/, "$1");
+      const rendered = blocks(body, resolveLink, depth + 1).replace(/^<p>([\s\S]*?)<\/p>(?=\n<|$)/, "$1");
       return `<li>${checkbox}${rendered}</li>`;
     })
     .join("\n");
@@ -548,10 +570,10 @@ function list(lines: string[], start: number, depth: number): [string, number] {
   return [`<${tag}${startAttr}>\n${html}\n</${tag}>`, i];
 }
 
-function blocks(lines: string[], depth = 0): string {
+function blocks(lines: string[], resolveLink?: ResolveLink, depth = 0): string {
   if (depth > MAX_NEST) {
     const text = lines.filter((l) => l.trim()).join("\n");
-    return text ? `<p>${paragraph(text.split("\n"))}</p>` : "";
+    return text ? `<p>${paragraph(text.split("\n"), resolveLink)}</p>` : "";
   }
 
   const out: string[] = [];
@@ -592,7 +614,7 @@ function blocks(lines: string[], depth = 0): string {
     const atx = ATX.exec(line);
     if (atx) {
       const level = (atx[1] ?? "#").length;
-      out.push(`<h${level}>${inline(atx[2] ?? "")}</h${level}>`);
+      out.push(`<h${level}>${inline(atx[2] ?? "", resolveLink)}</h${level}>`);
       i++;
       continue;
     }
@@ -600,12 +622,12 @@ function blocks(lines: string[], depth = 0): string {
     const under = at(lines, i + 1);
     if (!isBlockStart(line)) {
       if (/^\s{0,3}=+\s*$/.test(under)) {
-        out.push(`<h1>${inline(line.trim())}</h1>`);
+        out.push(`<h1>${inline(line.trim(), resolveLink)}</h1>`);
         i += 2;
         continue;
       }
       if (/^\s{0,3}-+\s*$/.test(under)) {
-        out.push(`<h2>${inline(line.trim())}</h2>`);
+        out.push(`<h2>${inline(line.trim(), resolveLink)}</h2>`);
         i += 2;
         continue;
       }
@@ -620,23 +642,23 @@ function blocks(lines: string[], depth = 0): string {
 
       const marker = ALERT.exec(buf[0] ?? "");
       if (marker) {
-        out.push(alertBlock(marker[1] ?? "", buf.slice(1), depth));
+        out.push(alertBlock(marker[1] ?? "", buf.slice(1), depth, resolveLink));
         continue;
       }
 
-      out.push(`<blockquote>\n${blocks(buf, depth + 1)}\n</blockquote>`);
+      out.push(`<blockquote>\n${blocks(buf, resolveLink, depth + 1)}\n</blockquote>`);
       continue;
     }
 
     if (line.includes("|") && isDelimiterRow(under)) {
-      const [html, next] = table(lines, i);
+      const [html, next] = table(lines, i, resolveLink);
       out.push(html);
       i = next;
       continue;
     }
 
     if (LIST_ITEM.test(line)) {
-      const [html, next] = list(lines, i, depth);
+      const [html, next] = list(lines, i, depth, resolveLink);
       out.push(html);
       i = next;
       continue;
@@ -665,7 +687,7 @@ function blocks(lines: string[], depth = 0): string {
     if (isHtmlOnly(line)) {
       const buf: string[] = [];
       while (i < lines.length && at(lines, i).trim()) {
-        buf.push(inline(at(lines, i).trim()));
+        buf.push(inline(at(lines, i).trim(), resolveLink));
         i++;
       }
       const html = buf.join("\n");
@@ -682,13 +704,13 @@ function blocks(lines: string[], depth = 0): string {
       buf.push(line);
       i++;
     }
-    out.push(`<p>${paragraph(buf)}</p>`);
+    out.push(`<p>${paragraph(buf, resolveLink)}</p>`);
   }
 
   return out.join("\n");
 }
 
-export function renderMarkdown(src: string): string {
+export function renderMarkdown(src: string, resolveLink?: ResolveLink): string {
   const oversize = src.length > MAX_RENDER_CHARS;
   const lines = (oversize ? src.slice(0, MAX_RENDER_CHARS) : src)
     .replace(/\r\n?/g, "\n")
@@ -697,7 +719,7 @@ export function renderMarkdown(src: string): string {
 
   if (oversize) lines.pop();
 
-  const html = balanceHtml(blocks(lines));
+  const html = balanceHtml(blocks(lines, resolveLink));
   return oversize
     ? `${html}\n<p><em>This document was truncated for display.</em></p>`
     : html;
diff --git a/src/lib/server/owner.ts b/src/lib/server/owner.ts
index 99b9b9e..eaac006 100644
--- a/src/lib/server/owner.ts
+++ b/src/lib/server/owner.ts
@@ -5,7 +5,7 @@
 import { canRead } from "./access.ts";
 import { findUserBySlug } from "./users.ts";
 import { siteRole } from "./roles.ts";
-import { renderMarkdown, plainSummary } from "./markdown.ts";
+import { renderMarkdown, plainSummary, repositoryLinks } from "./markdown.ts";
 import {
   PROFILE_REPO,
   headReadme,
@@ -74,7 +74,7 @@ export async function ownerPage(owner: string, viewer: string | null, withReadme
     readme: readme
       ? {
           path: readme.path,
-          html: renderMarkdown(readme.text),
+          html: renderMarkdown(readme.text, repositoryLinks(owner, PROFILE_REPO)),
           summary: plainSummary(readme.text),
         }
       : null,
diff --git a/src/routes/[owner]/[name=repo]/+page.server.ts b/src/routes/[owner]/[name=repo]/+page.server.ts
index 9326a01..95c5634 100644
--- a/src/routes/[owner]/[name=repo]/+page.server.ts
+++ b/src/routes/[owner]/[name=repo]/+page.server.ts
@@ -13,7 +13,7 @@ import { removeRepoImage, saveRepoImage } from "#lib/server/avatars.ts";
 import { getSettings } from "#lib/server/settings.ts";
 import { clearRepoMetadata } from "#lib/server/repometa.ts";
 import { notifyRepoDeleted } from "#lib/server/notify.ts";
-import { renderMarkdown, plainSummary } from "#lib/server/markdown.ts";
+import { renderMarkdown, plainSummary, repositoryLinks } from "#lib/server/markdown.ts";
 import { describeMirror } from "#lib/server/mirrorview.ts";
 import {
   isLevel,
@@ -95,6 +95,7 @@ export const load: PageServerLoad = async (event) => {
   }
 
   const nowSec = Math.floor(Date.now() / 1000);
+  const query = revQuery(rev, refs.head);
   const summary = readmeFile ? plainSummary(readmeFile.text) : "";
 
   return {
@@ -104,7 +105,7 @@ export const load: PageServerLoad = async (event) => {
     empty: refs.branches.length === 0,
     refs,
     rev,
-    query: revQuery(rev, refs.head),
+    query,
     cloneUrl: `${config.baseUrl}/${ref.owner}/${ref.name}.git`,
     pusher: viewer ?? ref.owner,
     rawDescription,
@@ -113,7 +114,10 @@ export const load: PageServerLoad = async (event) => {
     readme: readmeFile
       ? {
           path: readmeFile.path,
-          html: renderMarkdown(readmeFile.text),
+          html: renderMarkdown(
+            readmeFile.text,
+            repositoryLinks(ref.owner, ref.name, query),
+          ),
         }
       : null,
     canPush,
diff --git a/test/markdown.test.mjs b/test/markdown.test.mjs
index 874ad49..018a328 100644
--- a/test/markdown.test.mjs
+++ b/test/markdown.test.mjs
@@ -2,7 +2,7 @@
  * LICENCED DASL-1.0 (c) Clove Twilight
  */
 
-import { renderMarkdown, plainSummary } from "../src/lib/server/markdown.ts";
+import { renderMarkdown, plainSummary, repositoryLinks } from "../src/lib/server/markdown.ts";
 
 let failures = 0;
 function check(label, cond) {
@@ -13,6 +13,31 @@ function check(label, cond) {
 const has = (md, needle) => renderMarkdown(md).includes(needle);
 const lacks = (md, needle) => !renderMarkdown(md).includes(needle);
 
+const readmeLinks = repositoryLinks("user", "repo");
+for (const href of ["licence.md", "./licence.md"]) {
+  check(
+    `README link ${href} opens the repository file`,
+    renderMarkdown(`[Licence](${href})`, readmeLinks)
+      .includes('href="/user/repo/blob/licence.md"'),
+  );
+}
+check(
+  "README links keep the selected revision, query and fragment",
+  renderMarkdown(
+    '> <a href="./docs/../licence.md?view=full#terms">Licence</a>',
+    repositoryLinks("user", "repo", "?h=release%2Fnext"),
+  ).includes('href="/user/repo/blob/licence.md?h=release%2Fnext&amp;view=full#terms"'),
+);
+check(
+  "README links preserve fragment-only, site-root and external destinations",
+  ['href="#terms"', 'href="/help"', 'href="https://example.com"'].every(
+    (attribute) => renderMarkdown(
+      "[Terms](#terms) [Help](/help) [External](https://example.com)",
+      readmeLinks,
+    ).includes(attribute),
+  ),
+);
+
 check(
   "script tags are never emitted",
   lacks("<script>alert(1)</script>", "<script"),