Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/scripts/not-found-history.test.cjs
Original file line number Diff line number Diff line change
@@ -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);
}
});
58 changes: 58 additions & 0 deletions .github/scripts/remark-archived-assets.test.cjs
Original file line number Diff line number Diff line change
@@ -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");
});
25 changes: 25 additions & 0 deletions .github/workflows/documentation-tests.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions docusaurus.config.ts
Original file line number Diff line number Diff line change
@@ -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.',
Expand All @@ -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',
Expand Down
25 changes: 25 additions & 0 deletions scripts/remark-archived-assets.cjs
Original file line number Diff line number Diff line change
@@ -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;
39 changes: 39 additions & 0 deletions src/theme/NotFound/Content/history.cjs
Original file line number Diff line number Diff line change
@@ -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};
67 changes: 67 additions & 0 deletions src/theme/NotFound/Content/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<BrowserOnly
fallback={
<p>
<a href={repositoryHistoryUrl}>Browse the repository's Git history</a>{' '}
to investigate old file paths.
</p>
}>
{() => {
const pathname = window.location.pathname;
const historyUrl = historyUrlForMissingPath(pathname);

return (
<>
<p>
Missing URL: <code>{pathname}</code>
</p>
<p>
<a href={historyUrl}>
{historyUrl === repositoryHistoryUrl
? "Browse the repository's Git history"
: 'Investigate this path in GitHub history'}
</a>
{' '}to find previous versions or see when a path changed.
</p>
</>
);
}}
</BrowserOnly>
);
}

export default function NotFoundContent({className}: Props): ReactNode {
return (
<main className={clsx('container margin-vert--xl', className)}>
<div className="row">
<div className="col col--8 col--offset-2">
<Heading as="h1" className="hero__title">Page Not Found</Heading>
<p>The requested page or file is not available at this URL.</p>
<MissingUrlHistory />
<div className="alert alert--info" role="note">
<strong>Archived versions:</strong> 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.
</div>
<p className="margin-top--md">
For currently published files, see the <Link to="/docs/records">Records page</Link>.
</p>
</div>
</div>
</main>
);
}
Loading