@writedocs/generator 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/astro.config.mjs +8 -2
  2. package/package.json +1 -1
  3. package/packages/components/src/components/CodeGroup.astro +6 -6
  4. package/src/cli/build.js +39 -4
  5. package/src/cli/dev.js +16 -0
  6. package/src/cli/rewrite-redirect-pages.js +8 -6
  7. package/src/cli/run-pagefind.js +24 -11
  8. package/src/cli/write-mcp-files.js +4 -1
  9. package/src/cli/write-redirects-file.js +9 -4
  10. package/src/content.config.ts +21 -1
  11. package/src/layout/BaseLayout.astro +38 -49
  12. package/src/layout/components/LockMark.astro +28 -0
  13. package/src/layout/components/MobileMenu.astro +13 -5
  14. package/src/layout/components/Sidebar.astro +3 -2
  15. package/src/layout/components/TopBar.astro +17 -4
  16. package/src/lib/appearance.js +64 -0
  17. package/src/lib/asset-dimensions.js +2 -1
  18. package/src/lib/bom.js +20 -0
  19. package/src/lib/config-file.js +21 -8
  20. package/src/lib/config-schema.js +55 -10
  21. package/src/lib/config-schema.ts +78 -18
  22. package/src/lib/config.ts +23 -11
  23. package/src/lib/content-check.js +54 -4
  24. package/src/lib/content-store-prune.js +44 -0
  25. package/src/lib/json-schema-descriptions.js +7 -2
  26. package/src/lib/llms-index.ts +5 -0
  27. package/src/lib/mcp-index.ts +5 -1
  28. package/src/lib/mintlify-convert.js +11 -1
  29. package/src/lib/pages.js +5 -3
  30. package/src/lib/protected-sections.js +238 -0
  31. package/src/lib/wdignore-cases.json +9 -2
  32. package/src/lib/wdignore.js +5 -0
  33. package/src/lib/writedocs-legacy-convert.js +1 -1
  34. package/src/pages/[...slug].astro +53 -14
  35. package/src/pages/llms-full.txt.ts +10 -3
  36. package/src/pages/llms.txt.ts +4 -4
  37. package/src/pages/og/[...slug].png.ts +11 -2
  38. package/writedocs.schema.json +478 -70
@@ -66,6 +66,11 @@ export const DEFAULT_PATTERNS = [
66
66
  'bun.lock',
67
67
  'composer.json',
68
68
  'composer.lock',
69
+ // The repository's README, at any depth and in any case (README.md,
70
+ // readme.md, docs/Readme.md): it describes the repository, not the
71
+ // site. Without frontmatter it was never a page, but with frontmatter it
72
+ // became one, at /readme/.
73
+ 'README.md',
69
74
  // Dependencies and build output, at any depth.
70
75
  'node_modules/',
71
76
  'dist/',
@@ -490,7 +490,7 @@ export async function convertLegacyConfig(old, contentDir) {
490
490
  * navbar labels and category/group names. */
491
491
  function readTranslations(contentDir) {
492
492
  try {
493
- return JSON.parse(fs.readFileSync(path.join(contentDir, 'translations.json'), 'utf-8')) ?? {};
493
+ return JSON.parse(readJsonText(path.join(contentDir, 'translations.json'))) ?? {};
494
494
  } catch {
495
495
  return {};
496
496
  }
@@ -77,6 +77,8 @@ import ApiReferencePanel from "../components/ApiReferencePanel.astro";
77
77
  import { searchScope, sectionTrail, ALL_SCOPES } from "../lib/search-scope.js";
78
78
  import { selectorPlacements } from "../lib/selector-placement.js";
79
79
  import { spaceOf } from "../lib/hidden-sections.js";
80
+ import { protectedPages, searchSpaceOf } from "../lib/protected-sections.js";
81
+ import { isNoindex } from "../lib/seo-tags.js";
80
82
  import { navigationPageOrder } from "../lib/pages.js";
81
83
  import { folderRedirects } from "../lib/folder-redirects.js";
82
84
  import { DRAFT_BANNER_HTML } from "../lib/drafts.js";
@@ -303,8 +305,12 @@ export async function getStaticPaths() {
303
305
  // (`/docs/creator/`) redirect to the first page under them
304
306
  // (lib/folder-redirects.js). Preferred in the reader's order: navigation
305
307
  // order, public sections before hidden ones, pages outside the
306
- // navigation last.
308
+ // navigation after them - and protected pages (lib/protected-sections.js)
309
+ // last, so a folder with any page anyone can read leads to one.
307
310
  const slugOf = (route: { params: { slug?: string } }) => route.params.slug ?? "index";
311
+ const protectedRules = protectedPages(config.navigation);
312
+ const ruleOfRoute = (route: { props: { entry?: DocsEntry } }) =>
313
+ route.props.entry ? (protectedRules.get(fileIdForEntry(contentDir, packageRoot, route.props.entry)) ?? null) : null;
308
314
  const inHiddenSection = sections.map((section) => spaceOf(section.path) !== null);
309
315
  const contentRoutes = pageRoutes.filter((route) => !("redirectTo" in route.props));
310
316
  // An item's own page counts where the navigation names it: before the
@@ -325,25 +331,44 @@ export async function getStaticPaths() {
325
331
  ...contentRoutes,
326
332
  ...landingRoutes.filter((route) => !("redirectTo" in route.props)),
327
333
  ] as ContentRoute[];
334
+ const isProtected = (route: ContentRoute) => ruleOfRoute(route) !== null;
328
335
  const folderTargets = [
329
336
  ...navContent
330
- .filter((route) => !inHiddenItem(route))
337
+ .filter((route) => !inHiddenItem(route) && !isProtected(route))
331
338
  .sort((a, b) => orderOf(a) - orderOf(b))
332
339
  .map(slugOf),
333
340
  ...navContent
334
- .filter(inHiddenItem)
341
+ .filter((route) => inHiddenItem(route) && !isProtected(route))
335
342
  .sort((a, b) => orderOf(a) - orderOf(b))
336
343
  .map(slugOf),
337
344
  ...hiddenRoutes.map(slugOf),
345
+ ...navContent
346
+ .filter(isProtected)
347
+ .sort((a, b) => orderOf(a) - orderOf(b))
348
+ .map(slugOf),
338
349
  ];
339
350
  const takenAddresses = new Set([
340
351
  ...[...pageRoutes, ...landingRoutes, ...hiddenRoutes].map(slugOf),
341
352
  ...(config.redirects ?? []).map((r) => r.source.replace(/^\/+|\/+$/g, "")),
342
353
  ]);
343
- const folderRoutes = [...folderRedirects(folderTargets, takenAddresses)].map(([folder, target]) => ({
344
- params: { slug: folder },
345
- props: { redirectTo: `/${target}/`, temporary: true },
346
- }));
354
+ // The protected addresses, `{ slug, rule }`: the pages, and a folder
355
+ // redirect that leads to one - its folder has no other page, and the
356
+ // redirect page names the page, so it's behind the same rule.
357
+ const protectedRoutes = navContent.flatMap((route) => {
358
+ const rule = ruleOfRoute(route);
359
+ return rule ? [{ slug: slugOf(route), rule }] : [];
360
+ });
361
+ const ruleBySlug = new Map(protectedRoutes.map((route) => [route.slug, route.rule]));
362
+ const folderRoutes = [...folderRedirects(folderTargets, takenAddresses)].map(([folder, target]) => {
363
+ const rule = ruleBySlug.get(target);
364
+ if (rule) protectedRoutes.push({ slug: folder, rule });
365
+ return { params: { slug: folder }, props: { redirectTo: `/${target}/`, temporary: true } };
366
+ });
367
+ // `writedocs build` names a file here; the build makes
368
+ // dist/.writedocs/access.json from it (lib/protected-sections.js,
369
+ // src/cli/build.js) - the og route hands its list over the same way.
370
+ const accessFile = process.env.WRITEDOCS_ACCESS_FILE;
371
+ if (accessFile) fs.writeFileSync(accessFile, JSON.stringify(protectedRoutes));
347
372
 
348
373
  return [...rootRedirect, ...pageRoutes, ...landingRoutes, ...hiddenRoutes, ...folderRoutes];
349
374
  }
@@ -475,6 +500,9 @@ const hrefForSlug = (slug: string) => {
475
500
  // and what every "is this the active page" comparison below needs, since
476
501
  // navTree/globalDropdowns are built from writedocs.json's file-id space.
477
502
  const currentFileId = fileIdForEntry(contentDir, packageRoot, entry);
503
+ // The access rule the page is behind, or null (lib/protected-sections.js).
504
+ const protectedRules = oncePerBuild("slug:protected", () => protectedPages(config.navigation));
505
+ const pageRule = protectedRules.get(currentFileId) ?? null;
478
506
  // A section's tree doesn't depend on which of its pages is open (NavTree.astro
479
507
  // marks the current one), so it's built once per section in a build.
480
508
  const navTree = oncePerBuild(`slug:nav:${landingIndex === undefined ? `s${sectionIndex}` : `l${landingIndex}`}`, () =>
@@ -514,9 +542,10 @@ const pageSearchScope = isHidden ? { key: ALL_SCOPES, label: "" } : searchScope(
514
542
  const pageSectionTrail = [entry.data.draft ? "Draft" : "", isHidden ? "" : sectionTrail(activeSection.path, breadcrumbs)]
515
543
  .filter(Boolean)
516
544
  .join(" · ");
517
- // A page under a hidden navigation item searches its own index, not the
518
- // site's (lib/hidden-sections.js, cli/run-pagefind.js).
519
- const pageSearchSpace = isHidden ? null : spaceOf(activeSection.path);
545
+ // A page under a hidden or a protected navigation item searches its own
546
+ // index, not the site's (lib/hidden-sections.js, lib/protected-sections.js,
547
+ // cli/run-pagefind.js) - a protected page by its rule.
548
+ const pageSearchSpace = isHidden ? null : searchSpaceOf(activeSection.path, pageRule);
520
549
  const languageSegment = isHidden ? undefined : activeSection.path.find((segment) => segment.kind === "language");
521
550
  const pageLang = languageSegment
522
551
  ? (languageSegment.items[languageSegment.index] as { language: string }).language
@@ -536,6 +565,12 @@ const globalDropdowns = buildGlobalDropdowns(
536
565
  // than recomputing it, since it already encodes the same
537
566
  // frontmatter-slug-override-aware routing logic.
538
567
  const pageSeo = mergeSeo(config.seo, entry.data.seo);
568
+ // A protected page (lib/protected-sections.js) is never indexed, whatever
569
+ // its own `seo` says - a `robots` that allows indexing gives way.
570
+ if (pageRule) {
571
+ if (!isNoindex(pageSeo)) pageSeo.robots = undefined;
572
+ pageSeo.noindex = true;
573
+ }
539
574
  // A page with no picture of its own for link previews gets the generated
540
575
  // card (lib/og-image.js, served by src/pages/og/[...slug].png.ts).
541
576
  if (wantsGeneratedCard(pageSeo, entry.data, config.seo)) pageSeo.ogImage = ogImagePath(normalizeEntryId(entry.id));
@@ -544,10 +579,14 @@ const currentPath = hrefForSlug(currentFileId);
544
579
  // lib/item-pages.js) - a hidden page belongs to no language level.
545
580
  const alternates = isHidden
546
581
  ? []
547
- : languageAlternates(activeSection.path, currentFileId, activeSection.pages, landing !== undefined).map((a) => ({
548
- lang: a.lang,
549
- href: hrefForSlug(a.slug),
550
- }));
582
+ : languageAlternates(activeSection.path, currentFileId, activeSection.pages, landing !== undefined)
583
+ // Not a protected page in another language: a search engine can't
584
+ // read it (a protected page itself lists none - it's noindex).
585
+ .filter((a) => !protectedRules.has(a.slug))
586
+ .map((a) => ({
587
+ lang: a.lang,
588
+ href: hrefForSlug(a.slug),
589
+ }));
551
590
  // The trail for the page's structured data (lib/json-ld.js): home, the
552
591
  // groups above the page that have a page of their own, the page.
553
592
  const structuredTrail = [
@@ -6,6 +6,8 @@ import { loadDocsConfig, normalizeEntryId, findAllPages, resolveSiteRoot, fileId
6
6
  import { writedocsTempDir } from '../lib/writedocs-temp-dir.js';
7
7
  import { pageMarkdown } from '../lib/openapi-markdown';
8
8
  import { orderByNavigation } from '../lib/llms.js';
9
+ import { readProjectText } from '../lib/config-file.js';
10
+ import { protectedPages } from '../lib/protected-sections.js';
9
11
 
10
12
  // The "everything, concatenated" half of the llms.txt pair - see
11
13
  // llms.txt.ts (right next to this file) for the lightweight index half,
@@ -23,9 +25,11 @@ type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
23
25
  export const GET: APIRoute = async () => {
24
26
  const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
25
27
 
26
- const customPath = path.join(contentDir, 'llms-full.txt');
27
- if (fs.existsSync(customPath)) {
28
- return new Response(fs.readFileSync(customPath, 'utf-8'), {
28
+ // A hand-written llms-full.txt replaces the generated one - served
29
+ // without a byte order mark (lib/bom.js).
30
+ const custom = readProjectText(contentDir, 'llms-full.txt');
31
+ if (custom !== null) {
32
+ return new Response(custom, {
29
33
  headers: { 'Content-Type': 'text/plain; charset=utf-8' },
30
34
  });
31
35
  }
@@ -44,10 +48,13 @@ export const GET: APIRoute = async () => {
44
48
  ]);
45
49
  const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
46
50
 
51
+ const protectedRules = protectedPages(config.navigation);
47
52
  const unordered = entries
48
53
  // Same noindex exclusion llms.txt.ts applies to its own listing -
49
54
  // see that file's comment.
50
55
  .filter((entry) => !entry.data.seo?.noindex)
56
+ // Same for a protected page (lib/protected-sections.js).
57
+ .filter((entry) => !protectedRules.has(fileIdForEntry(contentDir, packageRoot, entry)))
51
58
  // Same `url` exclusion as llms.txt.ts - a redirect, not a page.
52
59
  .filter((entry) => !entry.data.url)
53
60
  .map((entry: DocsEntry) => {
@@ -1,7 +1,6 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
1
  import type { APIRoute } from 'astro';
4
2
  import { llmsIndexFiles } from '../lib/llms-index';
3
+ import { readProjectText } from '../lib/config-file.js';
5
4
 
6
5
  // A lightweight, LLM-facing index of every page on the site - the
7
6
  // llms.txt convention (see https://llmstxt.org), also adopted by Mintlify
@@ -30,10 +29,11 @@ export const GET: APIRoute = async () => {
30
29
  const files = await llmsIndexFiles();
31
30
  // null: the project has its own llms.txt at its root, next to
32
31
  // writedocs.json - it replaces the generated one (and its child files)
33
- // outright, and a site can fall back by just deleting it.
32
+ // outright, and a site can fall back by just deleting it. Served without
33
+ // a byte order mark (lib/bom.js).
34
34
  const text = files
35
35
  ? files.get('llms.txt')
36
- : fs.readFileSync(path.join(process.env.WRITEDOCS_CONTENT_DIR || process.cwd(), 'llms.txt'), 'utf-8');
36
+ : readProjectText(process.env.WRITEDOCS_CONTENT_DIR || process.cwd(), 'llms.txt');
37
37
  return new Response(text, {
38
38
  headers: { 'Content-Type': 'text/plain; charset=utf-8' },
39
39
  });
@@ -2,7 +2,8 @@ import { getCollection, type CollectionEntry } from 'astro:content';
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import type { APIRoute } from 'astro';
5
- import { loadDocsConfig, normalizeEntryId, findAllPages, mergeSeo } from '../../lib/config';
5
+ import { loadDocsConfig, normalizeEntryId, findAllPages, mergeSeo, fileIdForEntry } from '../../lib/config';
6
+ import { protectedPages } from '../../lib/protected-sections.js';
6
7
  import { writedocsTempDir } from '../../lib/writedocs-temp-dir.js';
7
8
  import { ogImagePng } from '../../lib/og-image.js';
8
9
  import { ogCardSet, ogCardOptions } from '../../lib/og-cards.js';
@@ -36,9 +37,17 @@ export async function getStaticPaths() {
36
37
  ]);
37
38
  const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
38
39
 
40
+ // A protected page (lib/protected-sections.js) is noindex, and a noindex
41
+ // page gets no card - its link preview is the sign-in page's anyway.
42
+ const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
43
+ const protectedRules = protectedPages(config.navigation);
39
44
  const set = ogCardSet(
40
45
  config,
41
- entries.map((entry) => ({ id: normalizeEntryId(entry.id), data: entry.data, seo: mergeSeo(config.seo, entry.data.seo) })),
46
+ entries.map((entry) => {
47
+ const seo = mergeSeo(config.seo, entry.data.seo);
48
+ if (protectedRules.has(fileIdForEntry(contentDir, packageRoot, entry))) seo.noindex = true;
49
+ return { id: normalizeEntryId(entry.id), data: entry.data, seo };
50
+ }),
42
51
  contentDir
43
52
  );
44
53
  const listFile = process.env.WRITEDOCS_OG_CARDS_FILE;