diff --git a/.github/scripts/not-found-history.test.cjs b/.github/scripts/not-found-history.test.cjs new file mode 100644 index 0000000..51b3320 --- /dev/null +++ b/.github/scripts/not-found-history.test.cjs @@ -0,0 +1,33 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const {test} = require("node:test"); +const { + historyUrlForMissingPath, + repositoryHistoryUrl +} = require("../../src/theme/NotFound/Content/history.cjs"); + +test("old static file URLs point to the exact source-file history", () => { + assert.equal(historyUrlForMissingPath("/files/taxes/2024.pdf"), + repositoryHistoryUrl + "?path=static%2Ffiles%2Ftaxes%2F2024.pdf"); + assert.equal(historyUrlForMissingPath("/ref/financials/2025/Balance%20Sheet.xlsx"), + repositoryHistoryUrl + "?path=static%2Fref%2Ffinancials%2F2025%2FBalance%20Sheet.xlsx"); + assert.equal(historyUrlForMissingPath("/img/old-logo.png"), + repositoryHistoryUrl + "?path=static%2Fimg%2Fold-logo.png"); + assert.equal(historyUrlForMissingPath("/favicon.ico"), + repositoryHistoryUrl + "?path=static%2Ffavicon.ico"); +}); + +test("documentation links select current or archived source history", () => { + assert.equal(historyUrlForMissingPath("/docs/records"), + repositoryHistoryUrl + "?path=docs%2Frecords.md"); + assert.equal(historyUrlForMissingPath("/docs/0.2.0/taxes/index"), + repositoryHistoryUrl + "?path=versions%2Farchive-0.2.0%2Ftaxes%2Findex.md"); +}); + +test("unknown, generated, or unsafe URLs fall back to repository history", () => { + for (const pathname of ["/", "/unrecognized/path", "/docs/category/operations", + "/files/../private.pdf", "/files/%ZZ", "/docs/../../secrets"]) { + assert.equal(historyUrlForMissingPath(pathname), repositoryHistoryUrl); + } +}); diff --git a/.github/scripts/remark-archived-assets.test.cjs b/.github/scripts/remark-archived-assets.test.cjs new file mode 100644 index 0000000..be593c7 --- /dev/null +++ b/.github/scripts/remark-archived-assets.test.cjs @@ -0,0 +1,58 @@ +"use strict"; + +const assert = require("node:assert/strict"); +const {test} = require("node:test"); +const remarkArchivedAssets = require("../../scripts/remark-archived-assets.cjs"); + +function run(file, nodes) { + const tree = {type: "root", children: nodes}; + remarkArchivedAssets()(tree, {path: file}); + return tree.children; +} + +test("archived static file links bypass Webpack resolution", () => { + const nodes = [ + {type: "paragraph", children: [ + {type: "link", url: "/files/taxes/2024.pdf", children: [{type: "text", value: "2024"}]}, + {type: "link", url: "/files/financials/2025/Balance%20Sheet.xlsx", children: []}, + {type: "link", url: "/files/financials/2025/xlm-deposits.csv#data", children: []}, + {type: "image", url: "/img/old.png", alt: "Old"}, + {type: "link", url: "pathname:///files/already.pdf", children: []}, + {type: "link", url: "/docs/records", children: []}, + {type: "link", url: "../other.md", children: []}, + {type: "link", url: "https://example.com/file.pdf", children: []}, + {type: "link", url: "//example.com/file.pdf", children: []} + ]}, + {type: "definition", identifier: "file", url: "/files/taxes/2025.pdf"}, + {type: "code", value: "[unprocessed](/files/taxes/2025.pdf)"} + ]; + const result = run("/repo/versioned_docs/version-0.2.0/records.md", nodes); + assert.deepEqual(result[0].children.map(x => x.url), [ + "pathname:///files/taxes/2024.pdf", + "pathname:///files/financials/2025/Balance%20Sheet.xlsx", + "pathname:///files/financials/2025/xlm-deposits.csv#data", + "pathname:///img/old.png", + "pathname:///files/already.pdf", + "/docs/records", + "../other.md", + "https://example.com/file.pdf", + "//example.com/file.pdf" + ]); + assert.equal(result[1].url, "pathname:///files/taxes/2025.pdf"); + assert.match(result[2].value, /\/files\/taxes/); +}); + +test("current documents retain strict static-file validation", () => { + const result = run("/repo/docs/records.md", [ + {type: "link", url: "/files/missing.pdf", children: []} + ]); + assert.equal(result[0].url, "/files/missing.pdf"); +}); + +test("archive detection supports Windows paths but not similar paths", () => { + const node = () => ({type: "link", url: "/ref/files/missing.pdf", children: []}); + assert.equal(run("C:\\repo\\versioned_docs\\version-0.1.0\\taxes\\index.md", [node()])[0].url, + "pathname:///ref/files/missing.pdf"); + assert.equal(run("/repo/my-versioned_docs/version-1/test.md", [node()])[0].url, + "/ref/files/missing.pdf"); +}); diff --git a/.github/workflows/documentation-tests.yml b/.github/workflows/documentation-tests.yml new file mode 100644 index 0000000..cdad3f8 --- /dev/null +++ b/.github/workflows/documentation-tests.yml @@ -0,0 +1,25 @@ +name: Documentation regression tests + +on: + pull_request: + branches: [main] + push: + branches: [main] + +permissions: + contents: read + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '22' + - name: Test version archive generation + run: node --test scripts/archive-versions.test.cjs + - name: Test historical asset link handling + run: node --test .github/scripts/remark-archived-assets.test.cjs + - name: Test 404 history links + run: node --test .github/scripts/not-found-history.test.cjs diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 2aeba82..106d68b 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -1,6 +1,8 @@ import type {Config} from '@docusaurus/types'; import type * as Preset from '@docusaurus/preset-classic'; +const remarkArchivedAssets = require('./scripts/remark-archived-assets.cjs'); + const config: Config = { title: 'BlockTransfer Org Docs', tagline: 'Operational documents and records.', @@ -25,6 +27,7 @@ const config: Config = { 'classic', { docs: { + beforeDefaultRemarkPlugins: [remarkArchivedAssets], sidebarPath: './sidebars.ts', editUrl: 'https://github.com/blocktransfer/org-docs/tree/main/', lastVersion: 'current', diff --git a/scripts/remark-archived-assets.cjs b/scripts/remark-archived-assets.cjs new file mode 100644 index 0000000..0fd1d78 --- /dev/null +++ b/scripts/remark-archived-assets.cjs @@ -0,0 +1,25 @@ +"use strict"; + +const archivePathPattern = /(?:^|[\\/])versioned_docs[\\/]version-[^\\/]+(?:[\\/]|$)/; +const assetUrlPattern = /^\/(?!\/)[^?#]+\.(?:pdf|csv|xlsx?|docx?|pptx?|zip|txt|png|jpe?g|gif|svg|webp|avif)(?:[?#].*)?$/i; + +// Keep archived asset URLs as ordinary links, not Webpack imports. A missing +// historical file may return 404 without preventing newer docs from building. +function remarkArchivedAssets() { + return (tree, file) => { + if (!archivePathPattern.test(file?.path ?? "")) return; + + function visit(node) { + if (!node || typeof node !== "object") return; + if (["link", "image", "definition"].includes(node.type) && + typeof node.url === "string" && assetUrlPattern.test(node.url)) { + node.url = "pathname://" + node.url; + } + if (Array.isArray(node.children)) node.children.forEach(visit); + } + + visit(tree); + }; +} + +module.exports = remarkArchivedAssets; diff --git a/src/theme/NotFound/Content/history.cjs b/src/theme/NotFound/Content/history.cjs new file mode 100644 index 0000000..ef9ea00 --- /dev/null +++ b/src/theme/NotFound/Content/history.cjs @@ -0,0 +1,39 @@ +"use strict"; + +const repositoryHistoryUrl = "https://github.com/blocktransfer/org-docs/commits/main"; + +function historyUrlForMissingPath(pathname) { + if (typeof pathname !== "string") return repositoryHistoryUrl; + + let decoded; + try { + decoded = decodeURIComponent(pathname); + } catch { + return repositoryHistoryUrl; + } + + const parts = decoded.replace(/^\/+|\/+$/g, "").split("/"); + if (parts.some(part => !part || part === "." || part === "..")) { + return repositoryHistoryUrl; + } + + let sourcePath; + if (parts[0] === "docs" && parts.length > 1) { + if (parts[1] === "category") return repositoryHistoryUrl; + if (/^\d+\.\d+\.\d+$/.test(parts[1]) && parts.length > 2) { + sourcePath = "versions/archive-" + parts[1] + "/" + parts.slice(2).join("/"); + } else { + sourcePath = "docs/" + parts.slice(1).join("/"); + } + if (!/\.(?:md|mdx)$/i.test(sourcePath)) sourcePath += ".md"; + } else if (["files", "ref", "img"].includes(parts[0]) || + (parts.length === 1 && /\.[A-Za-z0-9]+$/.test(parts[0]))) { + sourcePath = "static/" + parts.join("/"); + } else { + return repositoryHistoryUrl; + } + + return repositoryHistoryUrl + "?path=" + encodeURIComponent(sourcePath); +} + +module.exports = {historyUrlForMissingPath, repositoryHistoryUrl}; diff --git a/src/theme/NotFound/Content/index.tsx b/src/theme/NotFound/Content/index.tsx new file mode 100644 index 0000000..d6f9edc --- /dev/null +++ b/src/theme/NotFound/Content/index.tsx @@ -0,0 +1,67 @@ +import type {ReactNode} from 'react'; +import BrowserOnly from '@docusaurus/BrowserOnly'; +import Link from '@docusaurus/Link'; +import Heading from '@theme/Heading'; +import clsx from 'clsx'; + +const {historyUrlForMissingPath, repositoryHistoryUrl} = require('./history.cjs'); + +interface Props { + readonly className?: string; +} + +function MissingUrlHistory(): ReactNode { + return ( + + Browse the repository's Git history{' '} + to investigate old file paths. +

+ }> + {() => { + const pathname = window.location.pathname; + const historyUrl = historyUrlForMissingPath(pathname); + + return ( + <> +

+ Missing URL: {pathname} +

+

+ + {historyUrl === repositoryHistoryUrl + ? "Browse the repository's Git history" + : 'Investigate this path in GitHub history'} + + {' '}to find previous versions or see when a path changed. +

+ + ); + }} +
+ ); +} + +export default function NotFoundContent({className}: Props): ReactNode { + return ( +
+
+
+ Page Not Found +

The requested page or file is not available at this URL.

+ +
+ Archived versions: File locations can change over time. + Historical versions may contain links to files that have been moved, + renamed, or removed. Those original paths are not guaranteed to remain + available. +
+

+ For currently published files, see the Records page. +

+
+
+
+ ); +}