Files
OmniRoute/scripts/docs/add-frontmatter.mjs
diegosouzapw caa262a4c5 feat(docs): add YAML frontmatter to all docs (title/version/lastUpdated)
Every .md under docs/{architecture,guides,reference,frameworks,routing,
security,compression,ops,diagrams} plus docs/README.md now opens with:

  ---
  title: "<inferred from first H1>"
  version: 3.8.0
  lastUpdated: 2026-05-13
  ---

46 files updated (no docs were skipped — none had pre-existing
frontmatter). [slug]/page.tsx already reads frontmatter.version and
frontmatter.lastUpdated via gray-matter and renders a "v3.8.0" pill
plus a "Last updated" caption, so the UI picks these up automatically.

Helper: scripts/docs/add-frontmatter.mjs — idempotent (skips files that
already start with `---`), falls back to a humanized basename when no
leading H1 exists. Excludes docs/i18n/, docs/screenshots/,
docs/superpowers/, docs/diagrams/exported/. Re-runnable safely.

Also regenerated src/app/docs/lib/docs-auto-generated.ts: 44 docs across
8 sections (Architecture / Guides / Reference / Frameworks / Routing /
Security / Compression / Ops), which now includes the 14 docs that were
missing from the v3.7 sidebar (Cloud Agents, Guardrails, Memory, Skills,
Webhooks, Evals, Authz, Agent Protocols, Repository Map, Provider
Reference, Reasoning Replay, Stealth Guide, Tunnels Guide, Electron
Guide).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 18:46:05 -03:00

149 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* add-frontmatter.mjs — one-shot helper that ensures every documentation
* file under docs/<sub>/*.md and docs/README.md has a YAML frontmatter
* header with `title`, `version`, and `lastUpdated`.
*
* Idempotent: docs that already have a `---` block at the top are skipped
* (the existing frontmatter is preserved as-is). Files without a leading
* `# Title` heading fall back to the basename humanized as a title.
*
* Excludes: docs/i18n/, docs/screenshots/, docs/superpowers/,
* docs/diagrams/exported/. Subfolder READMEs are included.
*
* Usage: node scripts/docs/add-frontmatter.mjs [--version X.Y.Z] [--date YYYY-MM-DD]
*/
import { promises as fs } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ROOT = path.resolve(__dirname, "..", "..");
const DOCS_DIR = path.join(ROOT, "docs");
const EXCLUDE_PREFIXES = [
"docs/i18n/",
"docs/screenshots/",
"docs/superpowers/",
"docs/diagrams/exported/",
];
const args = process.argv.slice(2);
let version = "3.8.0";
let lastUpdated = "2026-05-13";
for (let i = 0; i < args.length; i += 1) {
if (args[i] === "--version" && args[i + 1]) {
version = args[i + 1];
i += 1;
} else if (args[i] === "--date" && args[i + 1]) {
lastUpdated = args[i + 1];
i += 1;
}
}
async function walk(dir, files = []) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
await walk(full, files);
} else if (entry.isFile() && entry.name.endsWith(".md")) {
files.push(full);
}
}
return files;
}
function isExcluded(rel) {
return EXCLUDE_PREFIXES.some((p) => rel === p || rel.startsWith(p));
}
function hasFrontmatter(content) {
return /^---\r?\n/.test(content);
}
function extractTopHeading(content) {
const lines = content.replace(/^/, "").split(/\r?\n/);
for (const line of lines) {
const m = line.match(/^#\s+(.+?)\s*$/);
if (m) return m[1].trim();
// Allow blank/HTML-comment lines before the first heading
if (line.trim() === "" || /^<!--/.test(line.trim())) continue;
// Anything else (e.g. badge image, paragraph) — no usable heading
return null;
}
return null;
}
function humanizeBasename(filePath) {
const base = path.basename(filePath, ".md");
if (base.toLowerCase() === "readme") {
const parent = path.basename(path.dirname(filePath));
if (parent && parent !== "." && parent !== "docs") {
const cap = parent.charAt(0).toUpperCase() + parent.slice(1);
return `${cap} Docs`;
}
return "Documentation";
}
return base
.replace(/[-_]+/g, " ")
.replace(/\s+/g, " ")
.replace(/\b\w/g, (c) => c.toUpperCase());
}
function buildFrontmatter(title) {
// Quote title with double quotes; escape internal double quotes.
const safe = title.replace(/"/g, '\\"');
return [
`---`,
`title: "${safe}"`,
`version: ${version}`,
`lastUpdated: ${lastUpdated}`,
`---`,
``,
].join("\n");
}
async function main() {
const allFiles = await walk(DOCS_DIR);
const targets = allFiles
.map((abs) => ({ abs, rel: path.relative(ROOT, abs).replace(/\\/g, "/") }))
.filter(({ rel }) => !isExcluded(rel));
let added = 0;
let skipped = 0;
let noHeading = [];
for (const { abs, rel } of targets) {
const content = await fs.readFile(abs, "utf8");
if (hasFrontmatter(content)) {
skipped += 1;
continue;
}
let title = extractTopHeading(content);
if (!title) {
title = humanizeBasename(abs);
noHeading.push(rel);
}
const fm = buildFrontmatter(title);
const next = `${fm}\n${content.replace(/^/, "")}`;
await fs.writeFile(abs, next, "utf8");
added += 1;
console.log(`[add-frontmatter] added → ${rel}`);
}
console.log(`[add-frontmatter] done — added=${added} skipped=${skipped} total=${targets.length}`);
if (noHeading.length > 0) {
console.log(
`[add-frontmatter] fallback title used (no leading H1) for: ${noHeading.join(", ")}`
);
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});