Skip to content

Commit ef99c53

Browse files
committed
refactor(fetch): generate Governance docs with Capitalized Governance-<title> prefix under /contribute
1 parent 7c27a16 commit ef99c53

1 file changed

Lines changed: 29 additions & 78 deletions

File tree

src/utilities/fetch-governance.mjs

Lines changed: 29 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs';
21
import path from 'path';
32
import { mkdirp } from 'mkdirp';
43
import { writeFile } from 'fs/promises';
@@ -12,21 +11,12 @@ const __dirname = path.dirname(__filename);
1211
const owner = 'webpack';
1312
const repo = 'governance';
1413

15-
// Output directory for governance content
16-
const outputDir = path.resolve(__dirname, '../content/contribute/Governance');
14+
// Output directly under /contribute (no Governance subfolder)
15+
const outputDir = path.resolve(__dirname, '../content/contribute');
1716

18-
// Mapping GitHub files to local filenames
19-
const fileMap = {
20-
'README.md': 'index.mdx',
21-
'CHARTER.md': 'charter.mdx',
22-
'MEMBER_EXPECTATIONS.md': 'member-expectations.mdx',
23-
'MODERATION_POLICY.md': 'moderation-policy.mdx',
24-
'WORKING_GROUPS.md': 'working-groups.mdx',
25-
};
26-
27-
// Generate title for frontmatter
17+
// Generate readable title from filename
2818
function generateTitle(filename) {
29-
if (filename === 'README.md') return 'Governance';
19+
if (filename === 'README.md') return 'Governance Overview';
3020
return filename
3121
.replace('.md', '')
3222
.replace(/_/g, ' ')
@@ -35,51 +25,47 @@ function generateTitle(filename) {
3525
.replace(/\b\w/g, (c) => c.toUpperCase());
3626
}
3727

38-
// Fix internal markdown links (.md → /) for static site compatibility
28+
// Fix internal markdown links (.md → /)
3929
function fixMarkdownLinks(content) {
4030
return content.replace(/(\]\([A-Z0-9_-]+)\.md(\))/gi, '$1/$2');
4131
}
4232

43-
// Helper to get slug from filename
44-
function destSlugFromFilename(destFile) {
45-
const name = path.basename(destFile, path.extname(destFile));
46-
return name.toLowerCase() === 'index' ? '/' : `/${name}/`;
47-
}
48-
49-
// Replace related section block in index
50-
function replaceRelatedSection(indexContent, relatedBlock) {
51-
const startMarker = '{/* GOV-RELATED-START */}';
52-
const endMarker = '{/* GOV-RELATED-END */}';
53-
const regex = new RegExp(`${startMarker}[\\s\\S]*?${endMarker}`, 'm');
54-
const wrappedBlock = `${startMarker}\n${relatedBlock}\n${endMarker}`;
55-
return regex.test(indexContent)
56-
? indexContent.replace(regex, wrappedBlock)
57-
: `${indexContent}\n\n${wrappedBlock}\n`;
58-
}
59-
6033
async function fetchGovernanceDocs() {
6134
console.log('Fetching governance markdown files from webpack/governance...');
6235

6336
await mkdirp(outputDir);
6437

6538
try {
39+
// Get markdown files from governance repo
6640
const { data: files } = await api.repos.getContent({
6741
owner,
6842
repo,
6943
path: '',
7044
});
45+
7146
const markdownFiles = files.filter((file) => file.name.endsWith('.md'));
7247

7348
for (const file of markdownFiles) {
7449
const filename = file.name;
75-
const destFile = fileMap[filename];
76-
if (!destFile) continue;
7750

51+
// Create Capitalized prefixed filenames
52+
const baseName = filename
53+
.replace('.md', '')
54+
.replace(/_/g, '-')
55+
.toLowerCase();
56+
57+
const destFile =
58+
filename === 'README.md'
59+
? 'Governance-Overview.mdx'
60+
: `Governance-${baseName}.mdx`;
61+
62+
// Fetch content from GitHub and fix markdown links
7863
const response = await fetch(file.download_url);
7964
let content = await response.text();
8065
content = fixMarkdownLinks(content);
81-
const title = generateTitle(filename);
8266

67+
// Generate title and sorting order
68+
const title = generateTitle(filename);
8369
const sortOrder =
8470
{
8571
'README.md': 0,
@@ -89,61 +75,26 @@ async function fetchGovernanceDocs() {
8975
'WORKING_GROUPS.md': 4,
9076
}[filename] ?? 10;
9177

92-
// Build frontmatter object: only add group for the index (README.md)
78+
// Build YAML frontmatter
9379
const fm = {
9480
title,
81+
group: 'Contribute',
9582
sort: sortOrder,
9683
source: `https://github.com/${owner}/${repo}/blob/main/${filename}`,
9784
edit: `https://github.com/${owner}/${repo}/edit/main/${filename}`,
9885
};
9986

100-
if (filename === 'README.md') fm.group = 'Contribute';
101-
10287
const frontmatter = yamlHeadmatter(fm);
103-
await writeFile(
104-
path.join(outputDir, destFile),
105-
frontmatter + content,
106-
'utf8'
107-
);
108-
console.log(`Synced: ${filename}`);
109-
}
11088

111-
// Ensure index.mdx exists and is properly formatted
112-
const indexPath = path.resolve(outputDir, 'index.mdx');
113-
let indexContent = '';
114-
115-
try {
116-
indexContent = fs.readFileSync(indexPath, 'utf8');
117-
} catch {
118-
indexContent =
119-
'---\n' +
120-
'title: Governance\n' +
121-
'group: Contribute\n' +
122-
'directory: true\n' +
123-
'sort: 0\n' +
124-
'---\n\n' +
125-
'# Governance\n\n';
126-
console.log('Created fallback index.mdx with metadata.');
89+
// Write .mdx file
90+
const destPath = path.join(outputDir, destFile);
91+
await writeFile(destPath, frontmatter + content, 'utf8');
92+
console.log(`Synced: ${filename}${destFile}`);
12793
}
12894

129-
const relatedItems = Object.entries(fileMap)
130-
.filter(([key]) => key !== 'README.md')
131-
.map(([key, dest]) => {
132-
const title = generateTitle(key);
133-
const slug = destSlugFromFilename(dest);
134-
return `- [${title}](/contribute/Governance${slug})`;
135-
})
136-
.join('\n');
137-
138-
const relatedBlock = `## Related Documents\n\n${relatedItems}\n`;
139-
fs.writeFileSync(
140-
indexPath,
141-
replaceRelatedSection(indexContent, relatedBlock),
142-
'utf8'
95+
console.log(
96+
'\nGovernance content generated successfully with Capitalized prefix!'
14397
);
144-
console.log('Updated index.mdx with related governance links.');
145-
146-
console.log('Governance content successfully synced.');
14798
} catch (error) {
14899
console.error('Error fetching governance files:', error.message);
149100
process.exitCode = 1;

0 commit comments

Comments
 (0)