Skip to content

Commit 35e2677

Browse files
committed
Fix md generation
1 parent 904bfda commit 35e2677

1 file changed

Lines changed: 45 additions & 6 deletions

File tree

scripts/generate-md.mjs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
55
const __dirname = path.dirname(fileURLToPath(import.meta.url));
66
const SRC_DOCS_DIR = path.resolve(__dirname, '../src/content/docs');
77
const REUSABLE_COMPONENTS_DIR = path.resolve(__dirname, '../src/components/reusable');
8+
const LOCALES_BASE_DIR = path.resolve(__dirname, '../src/locales');
89

910
// Get output dir from args or default to public
1011
const targetDirName = process.argv[2] || '../public';
@@ -122,19 +123,57 @@ async function processFiles(dir, reusableComponents) {
122123
}
123124
}
124125

125-
async function main() {
126-
console.log('Starting Markdown generation...');
126+
async function processLocaleFiles(locale, baseComponents) {
127+
const localeDir = path.join(LOCALES_BASE_DIR, locale);
128+
const localeOutputDir = path.join(OUTPUT_DIR, locale);
129+
await fs.mkdir(localeOutputDir, { recursive: true });
127130

128-
// Ensure output dir exists
131+
// Load locale-specific reusable component overrides
132+
const components = { ...baseComponents };
133+
const localeReusableDir = path.join(localeDir, 'reusable');
129134
try {
130-
await fs.access(OUTPUT_DIR);
131-
} catch {
132-
await fs.mkdir(OUTPUT_DIR, { recursive: true });
135+
const files = await fs.readdir(localeReusableDir);
136+
for (const file of files) {
137+
if (file.endsWith('.md') || file.endsWith('.mdx')) {
138+
const content = await fs.readFile(path.join(localeReusableDir, file), 'utf-8');
139+
const cleanedContent = content.replace(/<!---.*?--->\s*\n?/gs, '').trim();
140+
const componentName = toPascalCase(file.replace(/\.(md|mdx)$/, ''));
141+
components[componentName] = cleanedContent;
142+
}
143+
}
144+
} catch { /* no locale-specific reusable overrides */ }
145+
146+
const entries = await fs.readdir(localeDir, { withFileTypes: true });
147+
for (const entry of entries) {
148+
if (!entry.isFile() || (!entry.name.endsWith('.md') && !entry.name.endsWith('.mdx'))) continue;
149+
150+
const rawContent = await fs.readFile(path.join(localeDir, entry.name), 'utf-8');
151+
let content = cleanFrontmatter(rawContent);
152+
content = stripContent(content, components);
153+
154+
const basename = entry.name.replace(/\.(md|mdx)$/, '');
155+
await fs.writeFile(path.join(localeOutputDir, `${basename}.md`), content, 'utf-8');
133156
}
157+
}
158+
159+
async function main() {
160+
console.log('Starting Markdown generation...');
161+
162+
await fs.mkdir(OUTPUT_DIR, { recursive: true });
134163

135164
const reusableComponents = await getReusableComponents();
136165
await processFiles(SRC_DOCS_DIR, reusableComponents);
137166

167+
// Generate .md files for each locale
168+
try {
169+
const localeEntries = await fs.readdir(LOCALES_BASE_DIR, { withFileTypes: true });
170+
const locales = localeEntries.filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name);
171+
for (const locale of locales) {
172+
await processLocaleFiles(locale, reusableComponents);
173+
console.log(`Locale markdown generated: ${locale}`);
174+
}
175+
} catch { /* no locales directory */ }
176+
138177
console.log('Markdown generation complete.');
139178
}
140179

0 commit comments

Comments
 (0)