-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathsync-contributing.cjs
More file actions
111 lines (82 loc) · 3.43 KB
/
Copy pathsync-contributing.cjs
File metadata and controls
111 lines (82 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env node
/**
* Syncs the contributing.mdx file from docs/pages/contribute to the root CONTRIBUTING.md
* This script is run as part of the pre-build process.
*/
const fs = require('fs');
const path = require('path');
const MDX_SOURCE = path.join(__dirname, '..', 'docs', 'pages', 'contribute', 'contributing.mdx');
const MD_TARGET = path.join(__dirname, '..', 'CONTRIBUTING.md');
const SYNC_COMMENT = `<!--
This file is automatically synced from docs/pages/contribute/contributing.mdx during the build process.
⚠️ DO NOT EDIT THIS FILE DIRECTLY.
Instead, make changes to docs/pages/contribute/contributing.mdx and make this be synced just by running the build command.
Both files contain the same content: the docs file is there to render the content on the website, while this copy is needed to make it highlighted in the repository dashboard.
-->
`;
function transformMdxToMd(content) {
// Remove frontmatter (everything between the first --- and second ---)
content = content.replace(/^---\n[\s\S]*?\n---\n\n?/, '');
// Remove JSX comments {/* ... */}
content = content.replace(/\{\s*\/\*[\s\S]*?\*\/\s*\}/g, '');
// Remove import statements
content = content.replace(/^import .+$/gm, '');
// Remove opening tags: <TagProvider>, <TagFilter />
content = content.replace(/<TagProvider>\s*/g, '');
content = content.replace(/<TagFilter\s*\/>\s*/g, '');
// Remove closing tags: </TagProvider>, <ContributeFooter />
content = content.replace(/\s*<\/TagProvider>/g, '');
content = content.replace(/\s*<ContributeFooter\s*\/>/g, '');
// Convert MermaidRenderer component to markdown code block
content = content.replace(
/<MermaidRenderer\s+[^>]*code=\{`([^`]+)`\}\s*\/>/g,
'```mermaid\n$1\n```'
);
// Convert internal MDX links to MD paths
// /contribute/... -> /docs/pages/contribute/...mdx
// Keep any #anchor after the .mdx extension instead of inside it
content = content.replace(
/\[([^\]]+)\]\(\/contribute\/([^\)#]+)(#[^\)]*)?\)/g,
(_m, text, page, hash) => `[${text}](/docs/pages/contribute/${page}.mdx${hash || ''})`
);
// Remove horizontal rules (standalone --- lines, but not in code blocks)
const lines = content.split('\n');
const filteredLines = [];
let inCodeBlock = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Track code blocks
if (line.trim().startsWith('```')) {
inCodeBlock = !inCodeBlock;
filteredLines.push(line);
continue;
}
// Skip standalone --- lines outside code blocks
if (!inCodeBlock && line.trim() === '---') {
continue;
}
filteredLines.push(line);
}
content = filteredLines.join('\n');
// Trim trailing whitespace
content = content.trim();
return content;
}
function main() {
try {
console.log('Syncing CONTRIBUTING.md from contributing.mdx...');
// Read the source MDX file
const mdxContent = fs.readFileSync(MDX_SOURCE, 'utf8');
// Transform the content
const transformedContent = transformMdxToMd(mdxContent);
// Add the sync comment at the beginning
const finalContent = SYNC_COMMENT + transformedContent + '\n';
// Write to the target MD file
fs.writeFileSync(MD_TARGET, finalContent, 'utf8');
console.log('✅ Successfully synced CONTRIBUTING.md');
} catch (error) {
console.error('❌ Error syncing CONTRIBUTING.md:', error.message);
process.exit(1);
}
}
main();