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
12 changes: 9 additions & 3 deletions scripts/markdown/governance.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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}`);
Expand All @@ -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 };
Expand Down
36 changes: 20 additions & 16 deletions scripts/markdown/readmes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Loading