Skip to content

Commit 4dc4be6

Browse files
committed
fix(docs): sync Governance section generation with webpack.js.org content model
1 parent a9bd90b commit 4dc4be6

File tree

1 file changed

+57
-69
lines changed

1 file changed

+57
-69
lines changed

src/utilities/fetch-governance.mjs

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ const __dirname = path.dirname(__filename);
1212
const owner = 'webpack';
1313
const repo = 'governance';
1414

15-
// Local output directory inside webpack.js.org
16-
const outputDir = path.resolve(__dirname, '../content/contribute/governance');
15+
// Output directory for governance content
16+
const outputDir = path.resolve(__dirname, '../content/contribute/Governance');
1717

18-
// Map source files to destination filenames
18+
// Mapping GitHub files to local filenames
1919
const fileMap = {
2020
'README.md': 'index.mdx',
2121
'CHARTER.md': 'charter.mdx',
@@ -24,9 +24,9 @@ const fileMap = {
2424
'WORKING_GROUPS.md': 'working-groups.mdx',
2525
};
2626

27-
// Generate frontmatter titles automatically
27+
// Generate title for frontmatter
2828
function generateTitle(filename) {
29-
if (filename === 'README.md') return 'Governance Overview';
29+
if (filename === 'README.md') return 'Governance';
3030
return filename
3131
.replace('.md', '')
3232
.replace(/_/g, ' ')
@@ -35,61 +35,45 @@ function generateTitle(filename) {
3535
.replace(/\b\w/g, (c) => c.toUpperCase());
3636
}
3737

38-
// --- Helpers for index link generation ---
39-
38+
// Helper to get slug from filename
4039
function destSlugFromFilename(destFile) {
4140
const name = path.basename(destFile, path.extname(destFile));
42-
if (name.toLowerCase() === 'index') return '/';
43-
return `/${name}/`;
41+
return name.toLowerCase() === 'index' ? '/' : `/${name}/`;
4442
}
4543

44+
// Replace related section block in index
4645
function replaceRelatedSection(indexContent, relatedBlock) {
4746
const startMarker = '{/* GOV-RELATED-START */}';
4847
const endMarker = '{/* GOV-RELATED-END */}';
4948
const regex = new RegExp(`${startMarker}[\\s\\S]*?${endMarker}`, 'm');
5049
const wrappedBlock = `${startMarker}\n${relatedBlock}\n${endMarker}`;
51-
52-
if (regex.test(indexContent)) {
53-
return indexContent.replace(regex, wrappedBlock);
54-
} else {
55-
return `${indexContent}\n\n${wrappedBlock}\n`;
56-
}
50+
return regex.test(indexContent)
51+
? indexContent.replace(regex, wrappedBlock)
52+
: `${indexContent}\n\n${wrappedBlock}\n`;
5753
}
5854

5955
async function fetchGovernanceDocs() {
60-
console.log(
61-
'Fetching governance markdown files from webpack/governance...\n'
62-
);
56+
console.log('Fetching governance markdown files from webpack/governance...');
6357

6458
await mkdirp(outputDir);
6559

6660
try {
67-
// Get list of files in the governance repo
6861
const { data: files } = await api.repos.getContent({
6962
owner,
7063
repo,
7164
path: '',
7265
});
73-
74-
// Filter markdown files
7566
const markdownFiles = files.filter((file) => file.name.endsWith('.md'));
7667

7768
for (const file of markdownFiles) {
7869
const filename = file.name;
7970
const destFile = fileMap[filename];
71+
if (!destFile) continue;
8072

81-
if (!destFile) {
82-
console.log(`Skipping ${filename} — not mapped`);
83-
continue;
84-
}
85-
86-
const rawUrl = file.download_url;
87-
const response = await fetch(rawUrl);
73+
const response = await fetch(file.download_url);
8874
const content = await response.text();
89-
// Add YAML frontmatter for better sidebar integration
9075
const title = generateTitle(filename);
9176

92-
// Optional ordering logic for sidebar
9377
const sortOrder =
9478
{
9579
'README.md': 0,
@@ -99,57 +83,61 @@ async function fetchGovernanceDocs() {
9983
'WORKING_GROUPS.md': 4,
10084
}[filename] ?? 10;
10185

102-
const frontmatter = yamlHeadmatter({
86+
// Build frontmatter object: only add group for the index (README.md)
87+
const fm = {
10388
title,
104-
group: 'Governance',
10589
sort: sortOrder,
10690
source: `https://github.com/${owner}/${repo}/blob/main/${filename}`,
10791
edit: `https://github.com/${owner}/${repo}/edit/main/${filename}`,
108-
});
109-
110-
const finalContent = frontmatter + content;
92+
};
11193

112-
const destPath = path.join(outputDir, destFile);
113-
await writeFile(destPath, finalContent, 'utf8');
94+
if (filename === 'README.md') fm.group = 'Contribute';
11495

115-
console.log(
116-
`Synced: ${filename}${path.relative(process.cwd(), destPath)}`
96+
const frontmatter = yamlHeadmatter(fm);
97+
await writeFile(
98+
path.join(outputDir, destFile),
99+
frontmatter + content,
100+
'utf8'
117101
);
102+
console.log(`Synced: ${filename}`);
118103
}
119104

120-
// After all files are written: update index with related document links
105+
// Ensure index.mdx exists and is properly formatted
106+
const indexPath = path.resolve(outputDir, 'index.mdx');
107+
let indexContent = '';
108+
121109
try {
122-
const indexPath = path.resolve(outputDir, 'index.mdx');
123-
let indexContent = '';
124-
125-
try {
126-
indexContent = fs.readFileSync(indexPath, 'utf8');
127-
} catch (err) {
128-
indexContent =
129-
'---\ntitle: Governance Overview\n---\n\n# Governance Overview\n\n';
130-
console.log(err);
131-
}
132-
133-
const relatedItems = Object.entries(fileMap)
134-
.filter(([key]) => key !== 'README.md')
135-
.map(([key, dest]) => {
136-
const title = generateTitle(key);
137-
const slug = destSlugFromFilename(dest);
138-
const url = `/contribute/governance${slug}`;
139-
return `- [${title}](${url})`;
140-
})
141-
.join('\n');
142-
143-
const relatedBlock = `## Related Documents\n\n${relatedItems}\n`;
144-
const newIndex = replaceRelatedSection(indexContent, relatedBlock);
145-
146-
fs.writeFileSync(indexPath, newIndex, 'utf8');
147-
console.log('Updated index.mdx with related governance links.');
148-
} catch (err) {
149-
console.error('Failed to update index links:', err);
110+
indexContent = fs.readFileSync(indexPath, 'utf8');
111+
} catch {
112+
indexContent =
113+
'---\n' +
114+
'title: Governance\n' +
115+
'group: Contribute\n' +
116+
'directory: true\n' +
117+
'sort: 0\n' +
118+
'---\n\n' +
119+
'# Governance\n\n';
120+
console.log('Created fallback index.mdx with metadata.');
150121
}
151122

152-
console.log('\nGovernance content successfully synced!');
123+
const relatedItems = Object.entries(fileMap)
124+
.filter(([key]) => key !== 'README.md')
125+
.map(([key, dest]) => {
126+
const title = generateTitle(key);
127+
const slug = destSlugFromFilename(dest);
128+
return `- [${title}](/contribute/Governance${slug})`;
129+
})
130+
.join('\n');
131+
132+
const relatedBlock = `## Related Documents\n\n${relatedItems}\n`;
133+
fs.writeFileSync(
134+
indexPath,
135+
replaceRelatedSection(indexContent, relatedBlock),
136+
'utf8'
137+
);
138+
console.log('Updated index.mdx with related governance links.');
139+
140+
console.log('Governance content successfully synced.');
153141
} catch (error) {
154142
console.error('Error fetching governance files:', error.message);
155143
process.exitCode = 1;

0 commit comments

Comments
 (0)