|
| 1 | +/** |
| 2 | + * Markdown guide loader for the Scalar API reference. |
| 3 | + * |
| 4 | + * Per-module markdown guides live under `modules/{name}/doc/guides/*.md` |
| 5 | + * and are discovered by the same globbing mechanism as OpenAPI YAML files |
| 6 | + * (see `config/assets.js` → `allGuides`). |
| 7 | + * |
| 8 | + * Guides are merged into the OpenAPI spec via `info.description`, which |
| 9 | + * Scalar renders as a top-level "Introduction" section in the sidebar and |
| 10 | + * splits on markdown H1/H2 headings. |
| 11 | + */ |
| 12 | +import fs from 'fs'; |
| 13 | +import path from 'path'; |
| 14 | + |
| 15 | +import logger from '../services/logger.js'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Derive a human-readable title from a guide file path. |
| 19 | + * E.g. `modules/auth/doc/guides/getting-started.md` → `Getting Started` |
| 20 | + * @param {string} filePath - Absolute or relative path to the guide file. |
| 21 | + * @returns {string} Title-cased guide name. |
| 22 | + */ |
| 23 | +const titleFromPath = (filePath) => { |
| 24 | + const base = path.basename(String(filePath), path.extname(String(filePath))); |
| 25 | + return base |
| 26 | + .split(/[-_\s]+/) |
| 27 | + .filter(Boolean) |
| 28 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 29 | + .join(' '); |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Strip the first H1 heading from a markdown body (if present). |
| 34 | + * The loader injects its own H1 based on the file name so Scalar's sidebar |
| 35 | + * stays consistent even when guides omit a title or use a different one. |
| 36 | + * @param {string} markdown - Raw markdown content. |
| 37 | + * @returns {string} Markdown without the leading H1. |
| 38 | + */ |
| 39 | +const stripLeadingH1 = (markdown) => String(markdown).replace(/^\s*#\s+[^\n]*\n+/, ''); |
| 40 | + |
| 41 | +/** |
| 42 | + * Load markdown guides from disk and return normalized entries. |
| 43 | + * Invalid/unreadable files are skipped with a warning so one broken guide |
| 44 | + * cannot take down the whole API reference. |
| 45 | + * @param {string[]} filePaths - Absolute paths to `.md` guide files. |
| 46 | + * @returns {{ title: string, body: string, path: string }[]} Loaded guides. |
| 47 | + */ |
| 48 | +const loadGuides = (filePaths) => { |
| 49 | + if (!Array.isArray(filePaths) || filePaths.length === 0) return []; |
| 50 | + return filePaths |
| 51 | + .map((filePath) => { |
| 52 | + try { |
| 53 | + const raw = fs.readFileSync(filePath, 'utf8'); |
| 54 | + const body = stripLeadingH1(raw).trim(); |
| 55 | + if (!body) { |
| 56 | + logger.warn(`[guides] skipping ${filePath}: empty markdown content`); |
| 57 | + return null; |
| 58 | + } |
| 59 | + return { title: titleFromPath(filePath), body, path: filePath }; |
| 60 | + } catch (err) { |
| 61 | + logger.warn(`[guides] failed to load ${filePath}: ${err.message}`); |
| 62 | + return null; |
| 63 | + } |
| 64 | + }) |
| 65 | + .filter(Boolean) |
| 66 | + // Stable alphabetical order so the sidebar is deterministic across |
| 67 | + // filesystems (glob order varies on macOS vs Linux containers). |
| 68 | + .sort((a, b) => a.title.localeCompare(b.title)); |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Merge loaded guides into an OpenAPI spec's `info.description`. |
| 73 | + * Each guide becomes a top-level H1 section, which Scalar renders as a |
| 74 | + * sidebar entry alongside the API reference. |
| 75 | + * |
| 76 | + * The original spec is mutated (and returned) to match the merge style used |
| 77 | + * by `initSwagger` in `lib/services/express.js`. |
| 78 | + * |
| 79 | + * @param {object} spec - OpenAPI spec object (will be mutated). |
| 80 | + * @param {{ title: string, body: string }[]} guides - Loaded guide entries. |
| 81 | + * @returns {object} The same spec, with guides appended to `info.description`. |
| 82 | + */ |
| 83 | +const mergeGuidesIntoSpec = (spec, guides) => { |
| 84 | + if (!spec || typeof spec !== 'object') return spec; |
| 85 | + if (!Array.isArray(guides) || guides.length === 0) return spec; |
| 86 | + |
| 87 | + const sections = guides.map(({ title, body }) => `# ${title}\n\n${body}`); |
| 88 | + const existing = typeof spec.info?.description === 'string' ? spec.info.description.trim() : ''; |
| 89 | + const merged = [existing, ...sections].filter(Boolean).join('\n\n'); |
| 90 | + |
| 91 | + spec.info = { ...(spec.info || {}), description: merged }; |
| 92 | + return spec; |
| 93 | +}; |
| 94 | + |
| 95 | +export default { |
| 96 | + titleFromPath, |
| 97 | + stripLeadingH1, |
| 98 | + loadGuides, |
| 99 | + mergeGuidesIntoSpec, |
| 100 | +}; |
0 commit comments