Files

50 lines
1.9 KiB
JavaScript

// Generates the site.json configuration file for the React documentation.
// The sidebars come from scripts/nav.json, which import-react.mjs writes from react.dev's
// src/sidebar*.json (English titles) and zh-hans.react.dev's (Chinese titles, the `zh` field).
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const SITE_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const NAV_FILE = path.join(SITE_DIR, 'scripts', 'nav.json');
const GENERATED_DIR = path.join(SITE_DIR, '.vitepress', 'generated');
// Sidebar items keep `zh` (the Chinese title); .vitepress/config.mts picks the text per language.
function toSidebarItems(items, depth) {
return items.map(item => {
const result = { text: item.text };
if (item.zh) result.zh = item.zh;
if (item.link) result.link = item.link;
if (item.version) result.version = item.version;
if (item.items) {
result.items = toSidebarItems(item.items, depth + 1);
// Top-level groups stay open; nested pages with sub-pages start collapsed.
result.collapsed = depth > 0 || !!item.link;
}
return result;
});
}
function main() {
if (!fs.existsSync(NAV_FILE))
throw new Error('scripts/nav.json is missing. Run "node scripts/import-react.mjs <react.dev-repo> <zh-hans.react.dev-repo>" first.');
const nav = JSON.parse(fs.readFileSync(NAV_FILE, 'utf8'));
fs.mkdirSync(GENERATED_DIR, { recursive: true });
const site = {
locales: {
root: {
label: 'React',
base: '/',
sections: nav.map(section => ({ text: section.text, zh: section.zh, base: section.base })),
sidebar: Object.fromEntries(nav.map(section => [section.base, toSidebarItems(section.items, 0)])),
},
},
};
fs.writeFileSync(path.join(GENERATED_DIR, 'site.json'), JSON.stringify(site, null, 2));
console.log('[generate] Generated site.json');
}
main();