1
0

gen-openapi.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { readFileSync } from 'node:fs';
  2. import { basename } from 'node:path';
  3. import { generateFiles } from 'fumadocs-openapi';
  4. import { openapi } from '../lib/openapi.ts';
  5. // Map a slugified tag name back to its clean display name, so generated page
  6. // titles read "API Tokens" instead of the auto-split "A P I Tokens".
  7. const spec = JSON.parse(readFileSync('./public/openapi.json', 'utf8')) as {
  8. tags?: { name: string }[];
  9. };
  10. const titleBySlug = new Map(
  11. (spec.tags ?? []).map((t) => [t.name.toLowerCase().replace(/\s+/g, '-'), t.name]),
  12. );
  13. // Generate one MDX page per tag into the English reference/api folder.
  14. // Other locales fall back to English for the API reference.
  15. await generateFiles({
  16. input: openapi,
  17. output: './content/docs/en/reference/api',
  18. per: 'tag',
  19. beforeWrite(files) {
  20. for (const file of files) {
  21. const slug = basename(file.path).replace(/\.mdx$/, '');
  22. const title = titleBySlug.get(slug);
  23. if (title) {
  24. file.content = file.content.replace(/^title:.*$/m, `title: ${title}`);
  25. }
  26. }
  27. },
  28. });
  29. console.log('Generated API reference pages.');