diff --git a/apps/logicsrc-web/contract/doc-links.contract.test.ts b/apps/logicsrc-web/contract/doc-links.contract.test.ts new file mode 100644 index 00000000..24445464 --- /dev/null +++ b/apps/logicsrc-web/contract/doc-links.contract.test.ts @@ -0,0 +1,47 @@ +import { existsSync } from "node:fs"; +import { resolve } from "node:path"; +import { describe, expect, it } from "vitest"; +import { marked } from "marked"; + +import { DOC_SLUGS, readDoc, resolveDocLinks } from "@/lib/docs"; +import { allSpecs } from "@/lib/specs"; + +// A crawler found /docs/opencreds linking /docs/opencreds/spec.md and +// /docs/credential-sharing.md (both 404) and the sidebar linking /openi18n and +// /openl10n, which have no landing page. +describe("doc links", () => { + it("maps a served doc to /docs/ and anything else to the repository", () => { + const html = resolveDocLinks( + 'ab' + + 'cde' + ); + expect(html).toContain('href="/docs/credential-sharing"'); + expect(html).toContain('href="https://github.com/profullstack/logicsrc/blob/master/docs/opencreds/spec.md#x"'); + expect(html).toContain('href="https://github.com/profullstack/logicsrc/blob/master/prd/0001-x.md"'); + expect(html).toContain('href="https://example.com/x.md"'); + expect(html).toContain('href="#top"'); + }); + + it("leaves no rendered doc with a relative .md link, and every repository target exists", async () => { + const repo = resolve(process.cwd(), "../.."); + for (const slug of DOC_SLUGS) { + const md = readDoc(slug); + if (!md) continue; + const html = resolveDocLinks(await marked.parse(md)); + for (const [, href] of html.matchAll(/href="([^"]+)"/g)) { + expect(/^(?!https?:|\/|#|mailto:).*\.md(#|$)/.test(href), `${slug}: ${href}`).toBe(false); + const blob = /^https:\/\/github\.com\/profullstack\/logicsrc\/blob\/master\/([^#]+)/.exec(href); + if (blob) expect(existsSync(resolve(repo, blob[1])), `${slug}: ${href}`).toBe(true); + } + } + }); + + it("gives every spec landing a page", () => { + const catchAll = new Set(["openspec", "credential-sharing", "agentbyte"]); + for (const spec of allSpecs()) { + if (!spec.landing) continue; + const seg = spec.landing.replace(/^\//, ""); + expect(existsSync(resolve(process.cwd(), "src/app", seg)) || catchAll.has(seg), spec.landing).toBe(true); + } + }); +}); diff --git a/apps/logicsrc-web/next.config.ts b/apps/logicsrc-web/next.config.ts index 93affae3..297505c6 100644 --- a/apps/logicsrc-web/next.config.ts +++ b/apps/logicsrc-web/next.config.ts @@ -1,4 +1,5 @@ import type { NextConfig } from "next"; +import { docSlugs } from "./src/lib/specs"; // The CommandBoard API (boards, tasks, plugins, /health) runs as its own // service. In the old custom server.js it was mounted in-process; here we proxy @@ -82,10 +83,22 @@ export function buildRewrites( * the home page; the spec it promised is OpenFleet, which has its own landing * page. Permanent, so the old URL in the sitemap and in links keeps working. */ +const REPO_DOCS = "https://github.com/profullstack/logicsrc/blob/master/docs"; + export function buildRedirects() { return [ { source: "/agent-swarm", destination: "/openfleet", permanent: true }, - { source: "/openprofile/skills", destination: "/openskill", permanent: true } + { source: "/openprofile/skills", destination: "/openskill", permanent: true }, + // OpenI18n and OpenL10n have a specification text and no landing page. + { source: "/openi18n", destination: "/docs/openi18n", permanent: true }, + { source: "/openl10n", destination: "/docs/openl10n", permanent: true }, + // The docs link each other as Markdown files (`./credential-sharing.md`, + // `./opencreds/spec.md`), which resolved under /docs/ and 404ed. The pages + // now rewrite those links; these keep the URLs already crawled working. + // A doc the site serves goes to its page, anything else to the file in + // the public repository. + { source: `/docs/:slug(${docSlugs().join("|")})\\.md`, destination: "/docs/:slug", permanent: true }, + { source: "/docs/:path([a-z0-9][a-z0-9/-]*)\\.md", destination: `${REPO_DOCS}/:path.md`, permanent: true } ]; } diff --git a/apps/logicsrc-web/src/app/docs/[slug]/page.tsx b/apps/logicsrc-web/src/app/docs/[slug]/page.tsx index 399b21ae..bced39fe 100644 --- a/apps/logicsrc-web/src/app/docs/[slug]/page.tsx +++ b/apps/logicsrc-web/src/app/docs/[slug]/page.tsx @@ -3,7 +3,7 @@ import { notFound } from "next/navigation"; import type { ReactNode } from "react"; import type { Metadata } from "next"; import { marked } from "marked"; -import { DOC_SLUGS, docExcerpt, docSubtitle, docTitle, readDoc } from "@/lib/docs"; +import { DOC_SLUGS, docExcerpt, docSubtitle, docTitle, readDoc, resolveDocLinks } from "@/lib/docs"; import { composeTitle, contentMetadata, notFoundMetadata } from "@/lib/page-meta"; import { SiteShell } from "@/components/site-shell"; import { sanitizeRenderedHtml } from "@/lib/html"; @@ -46,7 +46,7 @@ export default async function DocPage({ if (!md) notFound(); const rawHtml = await marked.parse(md); - const html = sanitizeRenderedHtml(rawHtml); + const html = resolveDocLinks(sanitizeRenderedHtml(rawHtml)); return ( diff --git a/apps/logicsrc-web/src/lib/docs.ts b/apps/logicsrc-web/src/lib/docs.ts index 03c2c968..f6d3ec44 100644 --- a/apps/logicsrc-web/src/lib/docs.ts +++ b/apps/logicsrc-web/src/lib/docs.ts @@ -123,3 +123,34 @@ export function listDocs(): DocSummary[] { } return out; } + +const REPO_BLOB = "https://github.com/profullstack/logicsrc/blob/master"; + +/** + * Point a doc's relative Markdown links somewhere that exists. + * + * The docs link each other the way they read on GitHub: `./opencreds/spec.md`, + * `credential-sharing.md`, `../prd/0001-....md`. Rendered at /docs/ those + * resolve to /docs/credential-sharing.md and /docs/opencreds/spec.md, which the + * site never serves, so every one was a 404. A link to a doc the site serves + * becomes /docs/; anything else (sub-documents, PRDs) goes to the file in + * the public repository, which is where it is published. + */ +export function resolveDocLinks(html: string): string { + return html.replace(/href="([^"]+)"/g, (whole, href: string) => { + if (/^[a-z][a-z0-9+.-]*:/i.test(href) || href.startsWith("/") || href.startsWith("#")) return whole; + const [path, fragment] = href.split("#", 2); + if (!path.endsWith(".md")) return whole; + const parts: string[] = ["docs"]; + for (const seg of path.split("/")) { + if (seg === "" || seg === ".") continue; + if (seg === "..") parts.pop(); + else parts.push(seg); + } + const hash = fragment ? `#${fragment}` : ""; + const repoPath = parts.join("/"); + const served = /^docs\/([^/]+)\.md$/.exec(repoPath); + if (served && isDocSlug(served[1])) return `href="/docs/${served[1]}${hash}"`; + return `href="${REPO_BLOB}/${repoPath}${hash}"`; + }); +} diff --git a/apps/logicsrc-web/src/lib/specs.ts b/apps/logicsrc-web/src/lib/specs.ts index 4fc6b743..a860704c 100644 --- a/apps/logicsrc-web/src/lib/specs.ts +++ b/apps/logicsrc-web/src/lib/specs.ts @@ -109,8 +109,8 @@ export const FAMILIES: Family[] = [ s("openstack", "OpenStack.md", "One Markdown file a project serves about what it is built on: every interface and its layers, the rules, what it must never depend on, inherited by the next project through Extends"), s("openwebring", "OpenWebring", "A webring that says who made it: one file a ring serves about its members, one a member serves about itself, and made_by on every member"), s("openwiki", "OpenWiki", "A wiki that is a folder of Markdown files: [[Page]] links, a small front matter, a descriptor and a page index, every page and revision readable as Markdown, and made_by on every revision"), - s("openl10n", "OpenL10n", "The record of what a file says, in any language: one transcript per media per language, kept once, translations beside the original", { parent: "openfile" }), - s("openi18n", "OpenI18n", "One file a service serves about the languages it speaks, which it can turn into which, how to ask for one, and where texts are translated") + s("openl10n", "OpenL10n", "The record of what a file says, in any language: one transcript per media per language, kept once, translations beside the original", { parent: "openfile", landing: undefined }), + s("openi18n", "OpenI18n", "One file a service serves about the languages it speaks, which it can turn into which, how to ask for one, and where texts are translated", { landing: undefined }) ] }, { diff --git a/apps/pwa/src/server.mjs b/apps/pwa/src/server.mjs index 14f85ac4..f221df44 100644 --- a/apps/pwa/src/server.mjs +++ b/apps/pwa/src/server.mjs @@ -29,13 +29,20 @@ app.use(cookieParser()); // static app.use(express.static(path.join(config.root, "public"), { maxAge: "1h" })); -// the @simplewebauthn/browser UMD bundle, served from node_modules (no CDN) -app.get("/vendor/simplewebauthn-browser.umd.js", (_req, res) => - res.sendFile(path.join(config.root, "node_modules/@simplewebauthn/browser/dist/bundle/index.umd.min.js"))); +const requireHere = createRequire(import.meta.url); +// the @simplewebauthn/browser UMD bundle, served from node_modules (no CDN). +// Resolved, not joined onto apps/pwa/node_modules: the workspace install hoists +// it to the repo root, and the joined path 500ed (ENOENT) on every sign-in page. +// The package exports only ".", so find its root from the resolved entry +// (script/index.js) rather than resolving the bundle subpath. +const webauthnBundle = path.join( + path.dirname(path.dirname(requireHere.resolve("@simplewebauthn/browser"))), + "dist/bundle/index.umd.min.js", +); +app.get("/vendor/simplewebauthn-browser.umd.js", (_req, res) => res.sendFile(webauthnBundle)); // libsodium for in-browser vault decryption (public/vault.js). Same two files // the CLI loads; resolved, not path-joined, so a hoisted workspace install works. // Load order on the page: libsodium.js (window.libsodium) then the wrappers (window.sodium). -const requireHere = createRequire(import.meta.url); const sodiumWrappers = requireHere.resolve("libsodium-wrappers"); const sodiumCore = createRequire(sodiumWrappers).resolve("libsodium"); app.get("/vendor/libsodium.js", (_req, res) => res.sendFile(sodiumCore));