|
| 1 | +import { readdirSync, readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'; |
| 2 | +import { resolve, join, dirname } from 'path'; |
| 3 | +import { defineConfig } from 'vitepress' |
| 4 | + |
| 5 | +// Paths |
| 6 | +const docsDir = resolve(__dirname, '..', 'docs'); |
| 7 | + |
| 8 | +// Marker used in generated proxy files so we can detect and skip them when auto-generating the sidebar |
| 9 | +const PROXY_MARKER = '<!-- generated-proxy -->'; |
| 10 | + |
| 11 | +// Helper: recursively list files in a directory (non-hidden entries) |
| 12 | +function listMarkdownFiles(dir) { |
| 13 | + const entries = readdirSync(dir, { withFileTypes: true }); |
| 14 | + let files = []; |
| 15 | + for (const e of entries) { |
| 16 | + if (e.name.startsWith('.')) continue; |
| 17 | + const full = join(dir, e.name); |
| 18 | + if (e.isDirectory()) { |
| 19 | + files = files.concat(listMarkdownFiles(full)); |
| 20 | + } else if (e.isFile() && e.name.endsWith('.md')) { |
| 21 | + files.push(full); |
| 22 | + } |
| 23 | + } |
| 24 | + return files; |
| 25 | +} |
| 26 | + |
| 27 | +// Generate lightweight proxy .md files for any *.hidden.md so they are accessible at the normal path. |
| 28 | +// The proxy imports the hidden md as a Vue component and renders it, preserving the normal URL. |
| 29 | +function generateHiddenProxies() { |
| 30 | + const allMd = listMarkdownFiles(docsDir); |
| 31 | + for (const path of allMd) { |
| 32 | + if (path.endsWith('.hidden.md')) { |
| 33 | + const target = path.replace(/\.hidden\.md$/, '.md'); |
| 34 | + // Do not overwrite existing non-proxy files |
| 35 | + if (existsSync(target)) { |
| 36 | + try { |
| 37 | + const content = readFileSync(target, 'utf8'); |
| 38 | + if (content.includes(PROXY_MARKER)) continue; |
| 39 | + continue; |
| 40 | + } catch (e) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Ensure directory exists (should already) |
| 46 | + const td = dirname(target); |
| 47 | + if (!existsSync(td)) mkdirSync(td, { recursive: true }); |
| 48 | + |
| 49 | + // Compute relative import path from target to hidden file |
| 50 | + const importPath = './' + path.slice(td.length + 1).replace(/\\/g, '/'); |
| 51 | + // Build proxy content |
| 52 | + const proxyContent = `${PROXY_MARKER}\n<script setup>\nimport Content from '${importPath}'\n</script>\n\n<Content/>\n`; |
| 53 | + writeFileSync(target, proxyContent, 'utf8'); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Run generator synchronously so proxies exist before VitePress reads files |
| 59 | +try { |
| 60 | + generateHiddenProxies(); |
| 61 | +} catch (e) { |
| 62 | + // Fail silently — generation is best-effort |
| 63 | + console.error('generateHiddenProxies error:', e); |
| 64 | +} |
| 65 | + |
| 66 | +// Build the sidebar auto-dynamically by top-level folders in `docs` |
| 67 | +function buildSidebar() { |
| 68 | + const entries = readdirSync(docsDir, { withFileTypes: true }); |
| 69 | + const sidebar = []; |
| 70 | + |
| 71 | + // Include top-level pages (root .md files other than index.md) |
| 72 | + const rootFiles = entries |
| 73 | + .filter(e => e.isFile() && e.name.endsWith('.md')) |
| 74 | + .map(e => join(docsDir, e.name)); |
| 75 | + |
| 76 | + const rootItems = rootFiles |
| 77 | + .map(full => { |
| 78 | + // Exclude .hidden.md and generated proxies |
| 79 | + if (full.endsWith('.hidden.md')) return null; |
| 80 | + try { |
| 81 | + const content = readFileSync(full, 'utf8'); |
| 82 | + if (content.includes(PROXY_MARKER)) return null; |
| 83 | + } catch (e) {} |
| 84 | + const name = basenameWithoutExt(full); |
| 85 | + // Skip index.md (home) unless it's desired |
| 86 | + if (name === 'index') return null; |
| 87 | + return { text: name, link: `/${name}` }; |
| 88 | + }) |
| 89 | + .filter(Boolean); |
| 90 | + |
| 91 | + if (rootItems.length) { |
| 92 | + sidebar.push({ text: 'root', items: rootItems }); |
| 93 | + } |
| 94 | + |
| 95 | + // For each top-level folder, collect its markdown files (non-hidden, non-proxy) |
| 96 | + for (const e of entries) { |
| 97 | + if (!e.isDirectory()) continue; |
| 98 | + const folderPath = join(docsDir, e.name); |
| 99 | + const files = readdirSync(folderPath, { withFileTypes: true }) |
| 100 | + .filter(f => f.isFile() && f.name.endsWith('.md')) |
| 101 | + .map(f => join(folderPath, f.name)) |
| 102 | + .filter(full => { |
| 103 | + if (full.endsWith('.hidden.md')) return false; |
| 104 | + try { |
| 105 | + const content = readFileSync(full, 'utf8'); |
| 106 | + if (content.includes(PROXY_MARKER)) return false; |
| 107 | + } catch (err) {} |
| 108 | + return true; |
| 109 | + }); |
| 110 | + |
| 111 | + if (!files.length) continue; |
| 112 | + const items = files.map(full => { |
| 113 | + const name = basenameWithoutExt(full); |
| 114 | + // link should be /folder/name (strip any index specialness) |
| 115 | + const linkName = name === 'index' ? `/${e.name}/` : `/${e.name}/${name}`; |
| 116 | + return { text: name, link: linkName }; |
| 117 | + }); |
| 118 | + |
| 119 | + sidebar.push({ text: e.name, items }); |
| 120 | + } |
| 121 | + |
| 122 | + // Prepend a home link |
| 123 | + sidebar.unshift({ text: 'home', link: '/azimuth' }); |
| 124 | + return sidebar; |
| 125 | +} |
| 126 | + |
| 127 | +function basenameWithoutExt(fullPath) { |
| 128 | + const base = fullPath.split(/\\|\//).pop(); |
| 129 | + return base.replace(/\.hidden\.md$|\.md$/i, ''); |
| 130 | +} |
| 131 | + |
| 132 | +const sidebar = buildSidebar(); |
| 133 | + |
| 134 | +export default defineConfig({ |
| 135 | + title: "Azimuth Docs", |
| 136 | + description: "Documentation of the Azimuth library", |
| 137 | + themeConfig: { |
| 138 | + nav: [ |
| 139 | + { text: 'home', link: '/azimuth' }, |
| 140 | + ], |
| 141 | + |
| 142 | + sidebar: sidebar, |
| 143 | + |
| 144 | + socialLinks: [ |
| 145 | + { icon: 'github', link: 'https://github.com/vuejs/vitepress' } |
| 146 | + ] |
| 147 | + }, |
| 148 | + srcDir: 'docs', |
| 149 | +}) |
0 commit comments