-
Notifications
You must be signed in to change notification settings - Fork 52
feat: automate fetching and sanitization of webpack ecosystem readmes #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryzrr
wants to merge
5
commits into
main
Choose a base branch
from
feat/automated-readme-fetcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−15
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c25ffc7
feat: automate fetching and sanitization of webpack ecosystem readmes
ryzrr cabe77f
Fixup!
ryzrr 6ac95ca
Merge branch 'main' into feat/automated-readme-fetcher
ryzrr 19f2714
Update scripts/fetch-readmes.mjs
ryzrr 445f650
style: format fetch-readmes.mjs
ryzrr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ out | |
| *.generated.* | ||
| /.cache | ||
| /pages/api | ||
| /pages/loaders/ | ||
| /pages/plugins/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import base from './site.json' with { type: 'json' }; | ||
| import loadersSite from './loaders/site.json' with { type: 'json' }; | ||
| import pluginsSite from './plugins/site.json' with { type: 'json' }; | ||
|
ryzrr marked this conversation as resolved.
|
||
|
|
||
| export const { navbar, footer } = base; | ||
|
|
||
| export const sidebar = [...loadersSite.sidebar, ...pluginsSite.sidebar]; | ||
|
ryzrr marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { mkdirSync, writeFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| const { GH_TOKEN } = process.env; | ||
|
|
||
| const BASE_HEADERS = { | ||
| ...(GH_TOKEN && { Authorization: `Bearer ${GH_TOKEN}` }), | ||
| 'X-GitHub-Api-Version': '2022-11-28', | ||
| }; | ||
|
avivkeller marked this conversation as resolved.
avivkeller marked this conversation as resolved.
|
||
|
|
||
| const parseNextLink = linkHeader => { | ||
| if (!linkHeader) return null; | ||
| const match = linkHeader.match(/<([^>]+)>;\s*rel="next"/); | ||
| return match ? match[1] : null; | ||
| }; | ||
|
|
||
| const discoverRepos = async () => { | ||
| const loaders = []; | ||
| const plugins = []; | ||
| let url = | ||
| 'https://api.github.com/orgs/webpack/repos?per_page=100&type=public'; | ||
|
|
||
| while (url) { | ||
| const res = await fetch(url, { headers: BASE_HEADERS }); | ||
| if (!res.ok) | ||
| throw new Error( | ||
| `Failed to list org repos: ${res.status} ${res.statusText}` | ||
| ); | ||
|
|
||
| const repos = await res.json(); | ||
| for (const repo of repos) { | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| url = parseNextLink(res.headers.get('link')); | ||
| } | ||
|
|
||
| return { loaders, plugins }; | ||
| }; | ||
|
|
||
| const stripLeadingDiv = content => | ||
| content.replace(/^\s*<div[\s\S]*?<\/div>\n*/i, ''); | ||
|
|
||
| // Remove badge lines - lines consisting only of [![...][ref]][ref] or [](url) links | ||
| const stripBadges = content => | ||
| content | ||
| .replace( | ||
| /^(\[!\[[^\]]*\](?:\[[^\]]*\]|\([^)]*\))\]\s*(?:\[[^\]]*\]|\([^)]*\))\s*)+$/gm, | ||
| '' | ||
| ) | ||
| .replace(/\n{3,}/g, '\n\n'); | ||
|
|
||
| // TODO: remove this allowlist once Shiki silently skips unknown languages instead of build errors. | ||
| const SUPPORTED_LANGS = new Set([ | ||
| 'bash', | ||
| 'c', | ||
| 'c++', | ||
| 'cjs', | ||
| 'coffee', | ||
| 'coffeescript', | ||
| 'console', | ||
| 'cpp', | ||
| 'diff', | ||
| 'docker', | ||
| 'dockerfile', | ||
| 'glsl', | ||
| 'gql', | ||
| 'graphql', | ||
| 'http', | ||
| 'ini', | ||
| 'java', | ||
| 'javascript', | ||
| 'js', | ||
| 'json', | ||
| 'jsx', | ||
| 'mjs', | ||
| 'powershell', | ||
| 'ps', | ||
| 'ps1', | ||
| 'regex', | ||
| 'regexp', | ||
| 'sh', | ||
| 'shell', | ||
| 'shellscript', | ||
| 'shellsession', | ||
| 'sql', | ||
| 'ts', | ||
| 'tsx', | ||
| 'typescript', | ||
| 'xml', | ||
| 'yaml', | ||
| 'yml', | ||
| 'zsh', | ||
| ]); | ||
|
|
||
| const sanitizeCodeFences = content => | ||
| content.replace(/^```([a-zA-Z0-9_+-]+)\b/gm, (match, lang) => | ||
| SUPPORTED_LANGS.has(lang.toLowerCase()) ? match : '```' | ||
| ); | ||
|
avivkeller marked this conversation as resolved.
Comment on lines
+58
to
+104
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ryzrr marked this conversation as resolved.
|
||
|
|
||
| // remark-gfm does not support GitHub alert syntax (> [!TYPE]); rewrite to bold label inside the blockquote. | ||
| const GFM_ALERT_LABELS = { | ||
| NOTE: 'Note', | ||
| TIP: 'Tip', | ||
| IMPORTANT: 'Important', | ||
| WARNING: 'Warning', | ||
| CAUTION: 'Caution', | ||
| }; | ||
| const GFM_ALERT_RE = | ||
| /^([ \t]*>[ \t]*)\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][ \t]*$/gim; | ||
|
|
||
| const transformGfmAlerts = content => | ||
| content.replace( | ||
| GFM_ALERT_RE, | ||
| (_, prefix, type) => `${prefix}**${GFM_ALERT_LABELS[type]}:**` | ||
| ); | ||
|
avivkeller marked this conversation as resolved.
Comment on lines
+107
to
+121
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ryzrr marked this conversation as resolved.
|
||
|
|
||
| const processContent = content => | ||
| transformGfmAlerts(sanitizeCodeFences(stripBadges(stripLeadingDiv(content)))); | ||
|
|
||
| const fetchReadme = async fullName => { | ||
| const url = `https://raw.githubusercontent.com/${fullName}/HEAD/README.md`; | ||
| const res = await fetch(url); | ||
| return res.ok | ||
| ? { ok: true, text: await res.text() } | ||
| : { ok: false, status: res.status }; | ||
| }; | ||
|
avivkeller marked this conversation as resolved.
|
||
|
|
||
| const processRepos = async (repos, { groupName, basePath, outputDir }) => { | ||
| mkdirSync(outputDir, { recursive: true }); | ||
| const repoName = r => r.split('/')[1]; | ||
| console.log( | ||
| `Discovered ${groupName.toLowerCase()}: ${repos.map(repoName).join(', ')}` | ||
| ); | ||
|
|
||
| const fetched = []; | ||
| for (const fullName of repos) { | ||
| const name = repoName(fullName); | ||
| const result = await fetchReadme(fullName); | ||
| if (!result.ok) { | ||
| console.log(`Failed: ${name} — ${result.status}`); | ||
| continue; | ||
| } | ||
| const content = processContent(result.text); | ||
| writeFileSync(join(outputDir, `${name}.md`), content, 'utf8'); | ||
| fetched.push(name); | ||
| console.log(`Fetched: ${name}`); | ||
| } | ||
|
ryzrr marked this conversation as resolved.
|
||
|
|
||
| const siteJson = { | ||
| sidebar: [ | ||
| { | ||
| groupName, | ||
| items: fetched | ||
| .sort() | ||
| .map(name => ({ link: `${basePath}/${name}`, label: name })), | ||
| }, | ||
| ], | ||
| }; | ||
| writeFileSync( | ||
| join(outputDir, 'site.json'), | ||
| JSON.stringify(siteJson, null, 2) + '\n', | ||
| 'utf8' | ||
| ); | ||
| console.log( | ||
| `Written: ${outputDir}/site.json (${fetched.length} ${groupName.toLowerCase()})` | ||
| ); | ||
|
avivkeller marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const args = process.argv.slice(2); | ||
| const runLoaders = args.includes('--loaders') || args.length === 0; | ||
| const runPlugins = args.includes('--plugins') || args.length === 0; | ||
|
|
||
| const root = join(import.meta.dirname, '..'); | ||
|
ryzrr marked this conversation as resolved.
|
||
| const { loaders, plugins } = await discoverRepos(); | ||
|
ryzrr marked this conversation as resolved.
|
||
|
|
||
| if (runLoaders) { | ||
| await processRepos(loaders, { | ||
| groupName: 'Loaders', | ||
| basePath: '/loaders', | ||
| outputDir: join(root, 'pages/loaders'), | ||
| }); | ||
| } | ||
|
|
||
| if (runPlugins) { | ||
| await processRepos(plugins, { | ||
| groupName: 'Plugins', | ||
| basePath: '/plugins', | ||
| outputDir: join(root, 'pages/plugins'), | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.