Skip to content

Commit a6eebc0

Browse files
committed
fix(config): fall back to main for refs GitHub can't edit
Signed-off-by: vedchaudhari <vedc2853@gmail.com>
1 parent cc487f1 commit a6eebc0

5 files changed

Lines changed: 128 additions & 16 deletions

File tree

‎.changeset/edit-url-main.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
'@doc-kit/core': patch
3+
'@doc-kit/generator-react': patch
34
---
45

5-
Point "edit this page" links at the `main` branch instead of the git ref. GitHub can't edit files at `HEAD` or a tag, so those links returned a 404.
6+
Fall back to `main` in "edit this page" links when the git ref isn't a branch (`HEAD`, a commit SHA or a tag). GitHub can only edit files on a branch, so those links returned a 404.

‎packages/core/src/utils/configuration/__tests__/templates.test.mjs‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import assert from 'node:assert/strict';
44
import { describe, it } from 'node:test';
55

6-
import { populate } from '../templates.mjs';
6+
import {
7+
GITHUB_EDIT_URL,
8+
getEditableRef,
9+
populate,
10+
populateEditURL,
11+
} from '../templates.mjs';
712

813
describe('populate', () => {
914
it('should substitute template variables from config', () => {
@@ -24,3 +29,77 @@ describe('populate', () => {
2429
assert.strictEqual(result, '{missing}');
2530
});
2631
});
32+
33+
describe('getEditableRef', () => {
34+
it('should keep a branch name', () => {
35+
for (const branch of ['main', 'v24.x', 'v24.x-staging', 'feat/edit-url']) {
36+
assert.strictEqual(getEditableRef(branch), branch);
37+
}
38+
});
39+
40+
it('should strip the refs/heads/ prefix from a branch', () => {
41+
assert.strictEqual(getEditableRef('refs/heads/v24.x'), 'v24.x');
42+
});
43+
44+
it('should fall back to main when there is no ref', () => {
45+
assert.strictEqual(getEditableRef(undefined), 'main');
46+
assert.strictEqual(getEditableRef(''), 'main');
47+
});
48+
49+
it('should fall back to main for HEAD and revisions relative to it', () => {
50+
for (const ref of ['HEAD', 'HEAD~1', 'HEAD^', 'HEAD~2^1']) {
51+
assert.strictEqual(getEditableRef(ref), 'main', ref);
52+
}
53+
});
54+
55+
it('should fall back to main for commit SHAs', () => {
56+
for (const ref of [
57+
'933cb11',
58+
'933cb117c91',
59+
'933cb117c91e0c1f2a2b7e3c3f5d1a8b9c0d1e2f',
60+
'933cb117c91e0c1f2a2b7e3c3f5d1a8b9c0d1e2f933cb117c91e0c1f2a2b7e3c',
61+
]) {
62+
assert.strictEqual(getEditableRef(ref), 'main', ref);
63+
}
64+
});
65+
66+
it('should fall back to main for tags', () => {
67+
for (const ref of [
68+
'v24.0.0',
69+
'24.0.0',
70+
'v27.0.0-alpha.1',
71+
'refs/tags/v24.0.0',
72+
'refs/tags/some-tag',
73+
]) {
74+
assert.strictEqual(getEditableRef(ref), 'main', ref);
75+
}
76+
});
77+
});
78+
79+
describe('populateEditURL', () => {
80+
const template = `${GITHUB_EDIT_URL}/doc/api{path}.md`;
81+
82+
it('should not link to HEAD, which GitHub cannot edit', () => {
83+
const result = populateEditURL(template, {
84+
repository: 'nodejs/node',
85+
ref: 'HEAD',
86+
path: '/buffer',
87+
});
88+
assert.strictEqual(
89+
result,
90+
'https://github.com/nodejs/node/edit/main/doc/api/buffer.md'
91+
);
92+
});
93+
94+
it('should keep a configured branch', () => {
95+
const result = populateEditURL(template, {
96+
repository: 'nodejs/node',
97+
ref: 'v24.x',
98+
path: '/buffer',
99+
});
100+
assert.strictEqual(
101+
result,
102+
'https://github.com/nodejs/node/edit/v24.x/doc/api/buffer.md'
103+
);
104+
});
105+
});

‎packages/core/src/utils/configuration/templates.mjs‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ export const CHANGELOG_URL =
55
// This is the Base URL for viewing a file within GitHub UI
66
export const GITHUB_BLOB_URL = 'https://github.com/{repository}/blob/{ref}/';
77

8-
// This is the API docs base URL for editing a file on GitHub UI.
9-
// GitHub can only edit files on a branch, so this can't use `{ref}`
10-
// (`HEAD` or a tag would 404).
11-
export const GITHUB_EDIT_URL = 'https://github.com/{repository}/edit/main';
8+
// This is the API docs base URL for editing a file on GitHub UI
9+
export const GITHUB_EDIT_URL = 'https://github.com/{repository}/edit/{ref}';
10+
11+
// The branch "edit this page" links use when `ref` can't be edited
12+
export const DEFAULT_EDIT_BRANCH = 'main';
1213

1314
/**
1415
* Populate a template string based on a configuration
@@ -21,3 +22,39 @@ export const populate = (template, config) =>
2122
/\{(\w+)(?:\|(\w+))?\}/g,
2223
(match, path, fallback) => config[path] ?? fallback ?? match
2324
);
25+
26+
// Refs GitHub's web editor can't open, as it only edits files on a branch:
27+
const NON_BRANCH_REFS = [
28+
// `HEAD`, and revisions relative to it (`HEAD~1`, `HEAD^`)
29+
/^HEAD([~^]\d*)*$/,
30+
// Commit SHAs (SHA-1 or SHA-256, abbreviated or not)
31+
/^[0-9a-f]{7,64}$/i,
32+
// Tags, given in full or as a release version (`v24.0.0`, `1.2.3-rc.1`)
33+
/^refs\/tags\//,
34+
/^v?\d+\.\d+\.\d+([-+].*)?$/,
35+
];
36+
37+
/**
38+
* Returns a ref GitHub's web editor can open. A branch is kept (without its
39+
* `refs/heads/` prefix), and anything else, which would 404, falls back to
40+
* {@link DEFAULT_EDIT_BRANCH}.
41+
*
42+
* @param {string} [ref] The configured git ref
43+
* @returns {string}
44+
*/
45+
export const getEditableRef = ref => {
46+
if (!ref || NON_BRANCH_REFS.some(pattern => pattern.test(ref))) {
47+
return DEFAULT_EDIT_BRANCH;
48+
}
49+
50+
return ref.replace(/^refs\/heads\//, '');
51+
};
52+
53+
/**
54+
* Populate an "edit this page" URL template, using a ref GitHub can edit
55+
* @param {string} template The template string
56+
* @param {import("./types").GlobalConfiguration} config The configuration
57+
* @returns {string} the populated string
58+
*/
59+
export const populateEditURL = (template, config) =>
60+
populate(template, { ...config, ref: getEditableRef(config.ref) });

‎packages/node/src/config/__tests__/index.test.mjs‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ describe('Node.js preset', () => {
1919
assert.ok(preset.html.editURL.includes('/doc/api{path}.md'));
2020
});
2121

22-
it('should link "edit this page" to a branch, not the git ref', () => {
23-
assert.ok(
24-
preset.html.editURL.startsWith(
25-
'https://github.com/{repository}/edit/main/'
26-
)
27-
);
28-
});
29-
3022
it('should ship the Node.js llms.txt template', () => {
3123
assert.ok(existsSync(preset['llms-txt'].templatePath));
3224
});

‎packages/react/src/html/utils/config.mjs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import { getHighlighter } from '@doc-kit/core/plugins/shiki/highlighter.mjs';
44
import getConfig from '@doc-kit/core/utils/configuration/index.mjs';
5-
import { populate } from '@doc-kit/core/utils/configuration/templates.mjs';
5+
import {
6+
populate,
7+
populateEditURL,
8+
} from '@doc-kit/core/utils/configuration/templates.mjs';
69
import {
710
getEntryDescription,
811
getVersionFromSemVer,
@@ -179,7 +182,7 @@ export default function createConfigSource(input, server = false) {
179182

180183
const editURL =
181184
config.editURL &&
182-
populate(config.editURL, {
185+
populateEditURL(config.editURL, {
183186
...config,
184187
version: `v${configVersion.version}`,
185188
});

0 commit comments

Comments
 (0)