From 9f5587fc9f68af4d8eacf7956b6a73402b9e9b17 Mon Sep 17 00:00:00 2001 From: Nikhil Kumar Rajak Date: Sat, 25 Jul 2026 18:03:26 +0000 Subject: [PATCH] fix(markdown): point "Edit this page" at the upstream repo --- scripts/markdown/governance.mjs | 12 ++++++++--- scripts/markdown/readmes.mjs | 36 ++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/scripts/markdown/governance.mjs b/scripts/markdown/governance.mjs index b47a6293..9fe6a6be 100644 --- a/scripts/markdown/governance.mjs +++ b/scripts/markdown/governance.mjs @@ -3,6 +3,11 @@ import { join } from 'node:path'; import { fetchWithRetry } from '../utils/fetch.mjs'; import { rewriteRelativeLinks } from './sanitize.mjs'; +const REPO = 'webpack/governance'; +const RAW_URL = `https://raw.githubusercontent.com/${REPO}/HEAD`; +// GitHub's editor needs a real branch name, HEAD only works for reading. +const EDIT_URL = `https://github.com/${REPO}/edit/main`; + const { GH_TOKEN } = process.env; const BASE_HEADERS = { @@ -49,8 +54,9 @@ await mkdir(outputDir, { recursive: true }); const results = await Promise.all( Object.entries(FILE_MAP).map(async ([source, { output, label }]) => { - const url = `https://raw.githubusercontent.com/webpack/governance/HEAD/${source}`; - const res = await fetchWithRetry(url, { headers: BASE_HEADERS }); + const res = await fetchWithRetry(`${RAW_URL}/${source}`, { + headers: BASE_HEADERS, + }); if (!res.ok) { console.error(`Failed: ${source} -> ${res.status} ${res.statusText}`); @@ -66,7 +72,7 @@ const results = await Promise.all( // site derives the page title from — fall back to the sidebar label. if (!/^# /m.test(body)) body = `# ${label}\n\n${body}`; - const content = `---\nsource: ${url}\n---\n\n${body}`; + const content = `---\nsource: ${EDIT_URL}/${source}\n---\n\n${body}`; await writeFile(join(outputDir, `${output}.md`), content, 'utf8'); console.log(`Fetched: ${source} -> ${output}.md`); return { output, label }; diff --git a/scripts/markdown/readmes.mjs b/scripts/markdown/readmes.mjs index 8a29aebf..ec1221d3 100644 --- a/scripts/markdown/readmes.mjs +++ b/scripts/markdown/readmes.mjs @@ -24,8 +24,13 @@ const discoverRepos = async () => { for (const repo of await res.json()) { if (repo.archived) continue; - if (repo.name.endsWith('-loader')) loaders.push(repo.full_name); - else if (repo.name.endsWith('-plugin')) plugins.push(repo.full_name); + const entry = { + name: repo.name, + fullName: repo.full_name, + branch: repo.default_branch, + }; + if (repo.name.endsWith('-loader')) loaders.push(entry); + else if (repo.name.endsWith('-plugin')) plugins.push(entry); } url = parseNextLink(res.headers.get('link')); @@ -34,16 +39,16 @@ const discoverRepos = async () => { return { loaders, plugins }; }; -// Strip repo chrome, then point any relative links at the source repo on GitHub. -const cleanReadme = (content, fullName) => - cleanupMarkdown( - content, - target => `https://github.com/${fullName}/blob/HEAD/${target}` - ); +// GitHub's editor needs a real branch name, HEAD only works for reading. +const fileURL = ({ fullName, branch }, path, view = 'blob') => + `https://github.com/${fullName}/${view}/${branch}/${path}`; -const repoName = fullName => fullName.split('/')[1]; +// Strip repo chrome, point relative links and edits at the source repo. +const renderReadme = (content, repo) => + `---\nsource: ${fileURL(repo, 'README.md', 'edit')}\n---\n\n` + + cleanupMarkdown(content, target => fileURL(repo, target)).trimStart(); -const fetchReadme = async fullName => { +const fetchReadme = async ({ fullName }) => { const url = `https://raw.githubusercontent.com/${fullName}/HEAD/README.md`; const res = await fetchWithRetry(url); return res.text(); @@ -54,15 +59,14 @@ const processRepos = async (repos, { label, basePath, outputDir }) => { const fetched = ( await Promise.all( - repos.map(async fullName => { - const name = repoName(fullName); - const result = await fetchReadme(fullName); + repos.map(async repo => { + const result = await fetchReadme(repo); await writeFile( - join(outputDir, `${name}.md`), - cleanReadme(result, fullName), + join(outputDir, `${repo.name}.md`), + renderReadme(result, repo), 'utf8' ); - return name; + return repo.name; }) ) ).sort();