@writedocs/generator 0.10.19 → 0.11.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.
- package/astro.config.mjs +32 -8
- package/package.json +96 -93
- package/src/cli/base-path-layout.js +44 -0
- package/src/cli/build.js +177 -168
- package/src/cli/check-built-styles.js +41 -33
- package/src/cli/dev.js +261 -244
- package/src/cli/generate-api-pages.js +9 -5
- package/src/cli/page-list-watch.js +69 -66
- package/src/cli/run-pagefind.js +154 -148
- package/src/cli/write-mcp-files.js +103 -96
- package/src/cli/write-redirects-file.js +25 -8
- package/src/components/AppIcon.astro +51 -50
- package/src/components/Card.astro +193 -192
- package/src/components/CopyPageMenu.astro +374 -370
- package/src/components/Hint.astro +188 -187
- package/src/components/Icon.astro +72 -71
- package/src/components/Image.astro +165 -164
- package/src/components/Tile.astro +67 -66
- package/src/components/Video.astro +137 -136
- package/src/layout/BaseLayout.astro +21 -12
- package/src/layout/components/AnalyticsScripts.astro +6 -2
- package/src/layout/components/Breadcrumbs.astro +98 -97
- package/src/layout/components/ConsentBanner.astro +119 -118
- package/src/layout/components/MobileMenu.astro +229 -228
- package/src/layout/components/NavTree.astro +393 -392
- package/src/layout/components/Sidebar.astro +232 -231
- package/src/layout/components/SiteFooter.astro +87 -86
- package/src/layout/components/TopBar.astro +386 -385
- package/src/lib/agent-markdown.js +164 -162
- package/src/lib/asset-dimensions.js +89 -0
- package/src/lib/base-path.js +90 -0
- package/src/lib/config-schema.js +10 -0
- package/src/lib/config-schema.ts +17 -0
- package/src/lib/config.ts +1314 -1289
- package/src/lib/consent.js +108 -102
- package/src/lib/image-size.js +152 -140
- package/src/lib/json-schema-descriptions.js +1 -0
- package/src/lib/link-check.js +42 -4
- package/src/lib/llms-index.ts +90 -88
- package/src/lib/mcp-dev-integration.js +65 -60
- package/src/lib/mcp-index.ts +102 -101
- package/src/lib/pages.js +312 -304
- package/src/lib/project-files-integration.js +116 -61
- package/src/lib/project-files.js +137 -126
- package/src/lib/project-ignore.js +46 -0
- package/src/lib/rehype-base-path.js +62 -0
- package/src/lib/rehype-image-loading.js +71 -71
- package/src/lib/robots-txt.js +21 -16
- package/src/lib/seo-tags.js +155 -140
- package/src/lib/site-base.ts +27 -0
- package/src/lib/styles-asset-integration.js +58 -12
- package/src/lib/wdignore-cases.json +195 -0
- package/src/lib/wdignore.js +177 -0
- package/src/pages/404.astro +63 -62
- package/src/pages/[...slug].astro +1604 -1597
- package/src/pages/[...slug].md.ts +93 -93
- package/src/pages/llms-full.txt.ts +88 -86
- package/src/scripts/search.ts +241 -237
- package/writedocs.schema.json +6 -0
package/src/lib/link-check.js
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
// A link is broken when, on the built site:
|
|
8
8
|
// - no page, redirect or file is at its address;
|
|
9
9
|
// - its #anchor isn't on the target page (headings, titled components -
|
|
10
|
-
// Callout/Accordion/Update and friends - and elements with an `id`)
|
|
10
|
+
// Callout/Accordion/Update and friends - and elements with an `id`).
|
|
11
|
+
// #cookie-settings is on every page when writedocs.json turns on
|
|
12
|
+
// `integrations.consent`: it opens the consent banner (lib/consent.js);
|
|
11
13
|
// - it names a file the build doesn't publish (lib/project-files.js says
|
|
12
14
|
// which files of a project are published as they are).
|
|
13
15
|
//
|
|
@@ -38,7 +40,9 @@ import { createJsonLocator, contextMenuOptions } from './config-schema.js';
|
|
|
38
40
|
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
39
41
|
import { folderAddresses } from './folder-redirects.js';
|
|
40
42
|
import { isPublishedFile, readSpecPaths } from './project-files.js';
|
|
43
|
+
import { projectIgnore } from './project-ignore.js';
|
|
41
44
|
import { readMovedApiPages } from './moved-api-pages.js';
|
|
45
|
+
import { consentOptions, CONSENT_SETTINGS_ANCHOR } from './consent.js';
|
|
42
46
|
|
|
43
47
|
// The same github-slugger Astro builds page URLs and heading ids with -
|
|
44
48
|
// resolved through Astro itself, so the two can't drift apart.
|
|
@@ -307,7 +311,10 @@ export async function checkLinks(contentDir, configText) {
|
|
|
307
311
|
// Where generated API pages used to be - redirects too (lib/moved-api-pages.js).
|
|
308
312
|
for (const { source } of readMovedApiPages(contentDir)) redirects.add(`${source}/`);
|
|
309
313
|
const specs = readSpecPaths(contentDir);
|
|
314
|
+
const rules = projectIgnore(contentDir);
|
|
310
315
|
const special = new Set(['/llms.txt', '/llms-full.txt', '/404.html', '/404/', '/robots.txt']);
|
|
316
|
+
// With the consent banner on, #cookie-settings opens it from any page.
|
|
317
|
+
const pageWideAnchors = new Set(consentOptions(config.integrations?.consent) ? [CONSENT_SETTINGS_ANCHOR] : []);
|
|
311
318
|
if (config.domain) ['/sitemap.xml', '/sitemap-index.xml'].forEach((p) => special.add(p));
|
|
312
319
|
const knownUrls = [...pages.keys(), ...generated, ...redirects];
|
|
313
320
|
// Folder addresses with no page of their own redirect to one under them
|
|
@@ -406,7 +413,7 @@ export async function checkLinks(contentDir, configText) {
|
|
|
406
413
|
}
|
|
407
414
|
if (pageKey !== null) {
|
|
408
415
|
if (pages.has(pageKey)) {
|
|
409
|
-
if (!hash || hash === 'top') return;
|
|
416
|
+
if (!hash || hash === 'top' || pageWideAnchors.has(hash)) return;
|
|
410
417
|
const anchors = await anchorsOf(pages.get(pageKey).abs);
|
|
411
418
|
if (anchors.has(hash)) return;
|
|
412
419
|
const suggestion = closest(hash, [...anchors]);
|
|
@@ -459,14 +466,30 @@ export async function checkLinks(contentDir, configText) {
|
|
|
459
466
|
// Any other file.
|
|
460
467
|
if (special.has(pathname)) return;
|
|
461
468
|
const segments = pathname.replace(/^\/+/, '').split('/');
|
|
462
|
-
|
|
469
|
+
// A file the ignore rules leave out (lib/wdignore.js) is never published,
|
|
470
|
+
// in public/ or anywhere else - so a link to it is a broken one.
|
|
471
|
+
const leftOut = (rel) => {
|
|
472
|
+
if (!rules.ignores(rel)) return false;
|
|
473
|
+
report(
|
|
474
|
+
`${value} - ${rel} is in the project, but the ignore rules leave it out, so it isn't published.`,
|
|
475
|
+
rules.isLocked(rel)
|
|
476
|
+
? "It's a kind of file that is never published (a key, a .env, .git/)."
|
|
477
|
+
: `To publish it, bring it back in .wdignore - a line with !${rel}, or its folder's (!folder/) when the whole folder is left out.`
|
|
478
|
+
);
|
|
479
|
+
return true;
|
|
480
|
+
};
|
|
481
|
+
if (fs.existsSync(path.join(contentDir, 'public', ...segments))) {
|
|
482
|
+
leftOut(['public', ...segments].join('/'));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
463
485
|
const inProject = path.join(contentDir, ...segments);
|
|
464
486
|
if (fs.existsSync(inProject)) {
|
|
487
|
+
if (leftOut(segments.join('/'))) return;
|
|
465
488
|
// The build publishes the project's files at their own path
|
|
466
489
|
// (lib/project-files.js) - images, PDFs, data files. What it doesn't:
|
|
467
490
|
// the files the site is built from (writedocs.json, an OpenAPI spec),
|
|
468
491
|
// anything in a dot-folder, and kinds of file it doesn't know.
|
|
469
|
-
if (isPublishedFile(segments.join('/'), { specs })) return;
|
|
492
|
+
if (isPublishedFile(segments.join('/'), { specs, rules })) return;
|
|
470
493
|
// An image or font a page displays is copied wherever it is
|
|
471
494
|
// (lib/styles-asset-integration.js).
|
|
472
495
|
if (ASSET_EXTENSIONS.has(extension) && displayedAssets.has(pathname)) return;
|
|
@@ -489,6 +512,21 @@ export async function checkLinks(contentDir, configText) {
|
|
|
489
512
|
for (const link of scan.links) await check(link.href, { file, line: link.line, kind: link.kind, pageUrl, fileAbs: abs });
|
|
490
513
|
for (const spec of scan.imports) {
|
|
491
514
|
const snippet = snippetPath(spec, abs);
|
|
515
|
+
// A snippet the ignore rules leave out (lib/wdignore.js) isn't part of
|
|
516
|
+
// the project a site is built from: where the rules are applied before
|
|
517
|
+
// the build (the WriteDocs platform's sync), the import finds no file.
|
|
518
|
+
const snippetRel = relOf(snippet);
|
|
519
|
+
if (!snippetRel.startsWith('..') && fs.existsSync(snippet) && rules.ignores(snippetRel)) {
|
|
520
|
+
const key = [file, 'import', snippetRel].join('\n');
|
|
521
|
+
if (!reported.has(key)) {
|
|
522
|
+
reported.add(key);
|
|
523
|
+
broken.push({
|
|
524
|
+
file,
|
|
525
|
+
message: `import of ${spec} - ${snippetRel} is in the project, but the ignore rules leave it out, so the build can't count on it.`,
|
|
526
|
+
suggestion: rules.isLocked(snippetRel) ? 'Move the snippet out of that path.' : `Bring it back in .wdignore (!${snippetRel}), or move the snippet.`,
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
492
530
|
if (seen.has(snippet) || snippetsChecked.has(`${snippet}\n${pageUrl}`)) continue;
|
|
493
531
|
snippetsChecked.add(`${snippet}\n${pageUrl}`);
|
|
494
532
|
await checkFile(snippet, pageUrl, new Set([...seen, snippet]));
|
package/src/lib/llms-index.ts
CHANGED
|
@@ -1,88 +1,90 @@
|
|
|
1
|
-
// llms.txt and its child files (/llms/*.md) - the pages an AI tool can
|
|
2
|
-
// discover, in the site's navigation structure. Shared by llms.txt.ts (the
|
|
3
|
-
// root file) and llms/[...path].md.ts (the child files a big site's llms.txt
|
|
4
|
-
// links to), so both render from the same tree in the same run. The
|
|
5
|
-
// structure and the splitting live in lib/llms.js.
|
|
6
|
-
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
7
|
-
import fs from 'node:fs';
|
|
8
|
-
import path from 'node:path';
|
|
9
|
-
import { loadDocsConfig, normalizeEntryId, findAllPages,
|
|
10
|
-
import { buildLlmsTree, renderLlmsFiles } from './llms.js';
|
|
11
|
-
import { navigationPageOrder } from './pages.js';
|
|
12
|
-
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
13
|
-
|
|
14
|
-
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
15
|
-
|
|
16
|
-
// Mirrors Mintlify's own llms.txt behavior: a page's frontmatter
|
|
17
|
-
// `description` is cut at the first line break (a multi-paragraph
|
|
18
|
-
// description would blow out a one-line list entry) and at 300 characters,
|
|
19
|
-
// so every entry stays scannable.
|
|
20
|
-
const DESCRIPTION_MAX_CHARS = 300;
|
|
21
|
-
function truncateDescription(description: string | undefined): string | undefined {
|
|
22
|
-
if (!description) return undefined;
|
|
23
|
-
const firstLine = description.split('\n')[0].trim();
|
|
24
|
-
if (!firstLine) return undefined;
|
|
25
|
-
return firstLine.length > DESCRIPTION_MAX_CHARS ? firstLine.slice(0, DESCRIPTION_MAX_CHARS).trimEnd() + '…' : firstLine;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Most characters one file holds - llms.txt or a child file. The llms.txt
|
|
29
|
-
// spec sets no limit ("small enough to fit in context"); this is Mintlify's,
|
|
30
|
-
// about 25,000 tokens. Past it, sections move into child files instead of
|
|
31
|
-
// pages being dropped.
|
|
32
|
-
export const LLMS_MAX_CHARS = 100_000;
|
|
33
|
-
|
|
34
|
-
/** The generated files as Map<path, text> - 'llms.txt', then 'llms/<...>.md'
|
|
35
|
-
* when the site needs them - or null when the project has its own
|
|
36
|
-
* llms.txt, which replaces all of it. */
|
|
37
|
-
export async function llmsIndexFiles(): Promise<Map<string, string> | null> {
|
|
38
|
-
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
39
|
-
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
40
|
-
// A hand-authored llms.txt at the project root (next to writedocs.json)
|
|
41
|
-
// wins outright - same override convention Mintlify documents.
|
|
42
|
-
if (fs.existsSync(path.join(contentDir, 'llms.txt'))) return null;
|
|
43
|
-
|
|
44
|
-
const config = loadDocsConfig(contentDir);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// page
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
1
|
+
// llms.txt and its child files (/llms/*.md) - the pages an AI tool can
|
|
2
|
+
// discover, in the site's navigation structure. Shared by llms.txt.ts (the
|
|
3
|
+
// root file) and llms/[...path].md.ts (the child files a big site's llms.txt
|
|
4
|
+
// links to), so both render from the same tree in the same run. The
|
|
5
|
+
// structure and the splitting live in lib/llms.js.
|
|
6
|
+
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { loadDocsConfig, normalizeEntryId, findAllPages, resolveSiteRoot, fileIdForEntry, contextMenuOptions } from './config';
|
|
10
|
+
import { buildLlmsTree, renderLlmsFiles } from './llms.js';
|
|
11
|
+
import { navigationPageOrder } from './pages.js';
|
|
12
|
+
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
13
|
+
|
|
14
|
+
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
15
|
+
|
|
16
|
+
// Mirrors Mintlify's own llms.txt behavior: a page's frontmatter
|
|
17
|
+
// `description` is cut at the first line break (a multi-paragraph
|
|
18
|
+
// description would blow out a one-line list entry) and at 300 characters,
|
|
19
|
+
// so every entry stays scannable.
|
|
20
|
+
const DESCRIPTION_MAX_CHARS = 300;
|
|
21
|
+
function truncateDescription(description: string | undefined): string | undefined {
|
|
22
|
+
if (!description) return undefined;
|
|
23
|
+
const firstLine = description.split('\n')[0].trim();
|
|
24
|
+
if (!firstLine) return undefined;
|
|
25
|
+
return firstLine.length > DESCRIPTION_MAX_CHARS ? firstLine.slice(0, DESCRIPTION_MAX_CHARS).trimEnd() + '…' : firstLine;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Most characters one file holds - llms.txt or a child file. The llms.txt
|
|
29
|
+
// spec sets no limit ("small enough to fit in context"); this is Mintlify's,
|
|
30
|
+
// about 25,000 tokens. Past it, sections move into child files instead of
|
|
31
|
+
// pages being dropped.
|
|
32
|
+
export const LLMS_MAX_CHARS = 100_000;
|
|
33
|
+
|
|
34
|
+
/** The generated files as Map<path, text> - 'llms.txt', then 'llms/<...>.md'
|
|
35
|
+
* when the site needs them - or null when the project has its own
|
|
36
|
+
* llms.txt, which replaces all of it. */
|
|
37
|
+
export async function llmsIndexFiles(): Promise<Map<string, string> | null> {
|
|
38
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
39
|
+
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
40
|
+
// A hand-authored llms.txt at the project root (next to writedocs.json)
|
|
41
|
+
// wins outright - same override convention Mintlify documents.
|
|
42
|
+
if (fs.existsSync(path.join(contentDir, 'llms.txt'))) return null;
|
|
43
|
+
|
|
44
|
+
const config = loadDocsConfig(contentDir);
|
|
45
|
+
// The site's URL if `domain` is set, else its base ("/docs", or "" at the
|
|
46
|
+
// root) - lib/config.ts.
|
|
47
|
+
const siteRoot = resolveSiteRoot(config);
|
|
48
|
+
|
|
49
|
+
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
50
|
+
const hasPages = findAllPages(contentDir).length > 0;
|
|
51
|
+
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
52
|
+
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
53
|
+
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
54
|
+
]);
|
|
55
|
+
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
56
|
+
|
|
57
|
+
const pages = new Map<string, { title: string; href: string; description?: string; slug: string }>();
|
|
58
|
+
for (const entry of entries) {
|
|
59
|
+
// A page that opted out of search-engine indexing (`seo.noindex`, also
|
|
60
|
+
// left out of sitemap.xml) is left out here too, and a frontmatter `url`
|
|
61
|
+
// page (Mintlify's external link) has no content of its own.
|
|
62
|
+
if (entry.data.seo?.noindex || entry.data.url) continue;
|
|
63
|
+
const slug = normalizeEntryId(entry.id);
|
|
64
|
+
// The .md route when it exists ([...slug].md.ts - unless `contextMenu` is off,
|
|
65
|
+
// and never for an OpenAPI page, which renders from the spec), else the
|
|
66
|
+
// HTML page.
|
|
67
|
+
const hasMarkdownRoute = contextMenuOptions(config.contextMenu).length > 0;
|
|
68
|
+
const href = siteRoot + (hasMarkdownRoute ? `/${slug}.md` : slug === 'index' ? '/' : `/${slug}/`);
|
|
69
|
+
let description = truncateDescription(entry.data.description);
|
|
70
|
+
// Mirrors Mintlify: an OpenAPI operation page's description gets its
|
|
71
|
+
// "METHOD /path" appended - the page itself renders from the spec.
|
|
72
|
+
if (entry.data.openapi) description = description ? `${description} (${entry.data.openapi})` : entry.data.openapi;
|
|
73
|
+
pages.set(fileIdForEntry(contentDir, packageRoot, entry), { title: entry.data.title, href, description, slug });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const listed = new Set(navigationPageOrder(config.navigation));
|
|
77
|
+
const unlisted = [...pages.entries()]
|
|
78
|
+
.filter(([id]) => !listed.has(id))
|
|
79
|
+
.sort((a, b) => a[1].slug.localeCompare(b[1].slug))
|
|
80
|
+
.map(([id]) => id);
|
|
81
|
+
const tree = buildLlmsTree(config.navigation, (id: string) => pages.get(id) ?? null, unlisted);
|
|
82
|
+
|
|
83
|
+
return renderLlmsFiles({
|
|
84
|
+
name: config.name,
|
|
85
|
+
description: config.description,
|
|
86
|
+
tree,
|
|
87
|
+
maxChars: LLMS_MAX_CHARS,
|
|
88
|
+
urlFor: (file: string) => `${siteRoot}/${file}`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -1,60 +1,65 @@
|
|
|
1
|
-
// `writedocs dev`: the site's MCP server at /mcp, as the build serves it - the
|
|
2
|
-
// same handler (src/mcp/server.js), answering from the dev server's own
|
|
3
|
-
// /mcp-index.json, so an edit shows up on the next request. Lets an author
|
|
4
|
-
// point an MCP client at http://localhost:4321/mcp before deploying.
|
|
5
|
-
import { handleMcpHttp } from '../mcp/server.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
req.on('
|
|
12
|
-
req.on('
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
1
|
+
// `writedocs dev`: the site's MCP server at /mcp, as the build serves it - the
|
|
2
|
+
// same handler (src/mcp/server.js), answering from the dev server's own
|
|
3
|
+
// /mcp-index.json, so an edit shows up on the next request. Lets an author
|
|
4
|
+
// point an MCP client at http://localhost:4321/mcp before deploying.
|
|
5
|
+
import { handleMcpHttp } from '../mcp/server.js';
|
|
6
|
+
import { withoutBase } from './base-path.js';
|
|
7
|
+
|
|
8
|
+
function readBody(req) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const chunks = [];
|
|
11
|
+
req.on('data', (chunk) => chunks.push(chunk));
|
|
12
|
+
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
13
|
+
req.on('error', reject);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// `basePath`: writedocs.json's ("" or "/docs") - the server is at /docs/mcp.
|
|
18
|
+
// Astro's dev server takes the base off req.url before this middleware runs
|
|
19
|
+
// (its devBaseMiddleware), so /docs/mcp and a bare /mcp would look the same
|
|
20
|
+
// there: the path asked for is req.originalUrl.
|
|
21
|
+
export function mcpDevServer({ version, basePath = '' }) {
|
|
22
|
+
return {
|
|
23
|
+
name: 'writedocs-mcp-dev',
|
|
24
|
+
hooks: {
|
|
25
|
+
'astro:server:setup': ({ server }) => {
|
|
26
|
+
server.middlewares.use(async (req, res, next) => {
|
|
27
|
+
const pathname = withoutBase(basePath, (req.originalUrl ?? req.url ?? '').split('?')[0]);
|
|
28
|
+
if (pathname !== '/mcp' && pathname !== '/mcp/') return next();
|
|
29
|
+
try {
|
|
30
|
+
const origin = `http://${req.headers.host}`;
|
|
31
|
+
const headers = new Headers();
|
|
32
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
33
|
+
if (typeof value === 'string') headers.set(key, value);
|
|
34
|
+
}
|
|
35
|
+
const body = req.method === 'POST' ? await readBody(req) : undefined;
|
|
36
|
+
const request = new Request(origin + (req.originalUrl ?? req.url), { method: req.method, headers, body });
|
|
37
|
+
let missing = false;
|
|
38
|
+
const response = await handleMcpHttp(request, {
|
|
39
|
+
version,
|
|
40
|
+
loadIndex: async () => {
|
|
41
|
+
const index = await fetch(`${origin}${basePath}/mcp-index.json`);
|
|
42
|
+
if (!index.ok) {
|
|
43
|
+
missing = true;
|
|
44
|
+
return { meta: {}, docs: [] };
|
|
45
|
+
}
|
|
46
|
+
return index.json();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
if (missing) {
|
|
50
|
+
res.statusCode = 404;
|
|
51
|
+
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
52
|
+
res.end('No MCP server: writedocs.json has "mcp": false.');
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
res.statusCode = response.status;
|
|
56
|
+
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
57
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
58
|
+
} catch (err) {
|
|
59
|
+
next(err);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
package/src/lib/mcp-index.ts
CHANGED
|
@@ -1,101 +1,102 @@
|
|
|
1
|
-
// mcp-index.json - everything the site's MCP server (src/mcp/server.js)
|
|
2
|
-
// answers from: every page an agent may read, as it should read it. Built
|
|
3
|
-
// from the same pipeline as llms-full.txt (markdownForAgents(): snippets
|
|
4
|
-
// inlined, `variables` filled in, <Visibility> for agents), in navigation
|
|
5
|
-
// order, with each page's real URL and the version/language it sits under in
|
|
6
|
-
// the navigation - what search_docs filters on.
|
|
7
|
-
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
8
|
-
import fs from 'node:fs';
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
import {
|
|
11
|
-
loadDocsConfig,
|
|
12
|
-
normalizeEntryId,
|
|
13
|
-
findAllPages,
|
|
14
|
-
|
|
15
|
-
fileIdForEntry,
|
|
16
|
-
resolveSections,
|
|
17
|
-
resolveLandings,
|
|
18
|
-
flattenNav,
|
|
19
|
-
} from './config';
|
|
20
|
-
import { pageMarkdown } from './openapi-markdown';
|
|
21
|
-
import { orderByNavigation } from './llms.js';
|
|
22
|
-
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
23
|
-
|
|
24
|
-
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
25
|
-
|
|
26
|
-
export interface McpDoc {
|
|
27
|
-
path: string;
|
|
28
|
-
url: string;
|
|
29
|
-
title: string;
|
|
30
|
-
description: string;
|
|
31
|
-
content: string;
|
|
32
|
-
version: string;
|
|
33
|
-
language: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface McpIndex {
|
|
37
|
-
meta: { name: string; description: string; siteUrl: string; generator: 'writedocs' };
|
|
38
|
-
docs: McpDoc[];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function mcpIndex(): Promise<McpIndex> {
|
|
42
|
-
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
43
|
-
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
44
|
-
const config = loadDocsConfig(contentDir);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (segment.kind === '
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
.
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
1
|
+
// mcp-index.json - everything the site's MCP server (src/mcp/server.js)
|
|
2
|
+
// answers from: every page an agent may read, as it should read it. Built
|
|
3
|
+
// from the same pipeline as llms-full.txt (markdownForAgents(): snippets
|
|
4
|
+
// inlined, `variables` filled in, <Visibility> for agents), in navigation
|
|
5
|
+
// order, with each page's real URL and the version/language it sits under in
|
|
6
|
+
// the navigation - what search_docs filters on.
|
|
7
|
+
import { getCollection, type CollectionEntry } from 'astro:content';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import {
|
|
11
|
+
loadDocsConfig,
|
|
12
|
+
normalizeEntryId,
|
|
13
|
+
findAllPages,
|
|
14
|
+
resolveSiteRoot,
|
|
15
|
+
fileIdForEntry,
|
|
16
|
+
resolveSections,
|
|
17
|
+
resolveLandings,
|
|
18
|
+
flattenNav,
|
|
19
|
+
} from './config';
|
|
20
|
+
import { pageMarkdown } from './openapi-markdown';
|
|
21
|
+
import { orderByNavigation } from './llms.js';
|
|
22
|
+
import { writedocsTempDir } from './writedocs-temp-dir.js';
|
|
23
|
+
|
|
24
|
+
type DocsEntry = CollectionEntry<'pages'> | CollectionEntry<'generatedDocs'>;
|
|
25
|
+
|
|
26
|
+
export interface McpDoc {
|
|
27
|
+
path: string;
|
|
28
|
+
url: string;
|
|
29
|
+
title: string;
|
|
30
|
+
description: string;
|
|
31
|
+
content: string;
|
|
32
|
+
version: string;
|
|
33
|
+
language: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface McpIndex {
|
|
37
|
+
meta: { name: string; description: string; siteUrl: string; generator: 'writedocs' };
|
|
38
|
+
docs: McpDoc[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function mcpIndex(): Promise<McpIndex> {
|
|
42
|
+
const contentDir = process.env.WRITEDOCS_CONTENT_DIR || process.cwd();
|
|
43
|
+
const packageRoot = process.env.WRITEDOCS_PACKAGE_ROOT || process.cwd();
|
|
44
|
+
const config = loadDocsConfig(contentDir);
|
|
45
|
+
// {domain}{basePath} - or the base alone ("/docs", or "") with no `domain`.
|
|
46
|
+
const siteUrl = resolveSiteRoot(config);
|
|
47
|
+
|
|
48
|
+
const hasGeneratedDocs = fs.existsSync(path.join(writedocsTempDir(contentDir), 'generated-docs'));
|
|
49
|
+
const hasPages = findAllPages(contentDir).length > 0;
|
|
50
|
+
const [pagesEntries, generatedDocsEntries] = await Promise.all([
|
|
51
|
+
hasPages ? getCollection('pages') : Promise.resolve([]),
|
|
52
|
+
hasGeneratedDocs ? getCollection('generatedDocs') : Promise.resolve([]),
|
|
53
|
+
]);
|
|
54
|
+
const entries: DocsEntry[] = [...pagesEntries, ...generatedDocsEntries];
|
|
55
|
+
|
|
56
|
+
// The version and language each page sits under, from its section's path
|
|
57
|
+
// through the navigation (tabs > versions > languages > ...).
|
|
58
|
+
// An item's own page that no sidebar lists sits under that item.
|
|
59
|
+
const sections = resolveSections(config.navigation);
|
|
60
|
+
const landings = resolveLandings(config.navigation);
|
|
61
|
+
const placeOf = (fileId: string) => {
|
|
62
|
+
const section =
|
|
63
|
+
sections.find((s) => flattenNav(s.pages).some((e) => e.slug === fileId)) ??
|
|
64
|
+
landings.find((landing) => landing.slug === fileId);
|
|
65
|
+
const place = { version: '', language: '' };
|
|
66
|
+
for (const segment of section?.path ?? []) {
|
|
67
|
+
const item = segment.items[segment.index] as { version?: string; language?: string };
|
|
68
|
+
if (segment.kind === 'version' && item.version) place.version = item.version;
|
|
69
|
+
if (segment.kind === 'language' && item.language) place.language = item.language;
|
|
70
|
+
}
|
|
71
|
+
return place;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const docs = entries
|
|
75
|
+
// Same pages llms.txt lists: not `noindex`, not a frontmatter `url`
|
|
76
|
+
// (an external link, with no content of its own).
|
|
77
|
+
.filter((entry) => !entry.data.seo?.noindex && !entry.data.url)
|
|
78
|
+
.map((entry) => {
|
|
79
|
+
const slug = normalizeEntryId(entry.id);
|
|
80
|
+
const fileId = fileIdForEntry(contentDir, packageRoot, entry);
|
|
81
|
+
// An API page's source is a near-empty stub - its operation, written
|
|
82
|
+
// out from the spec, follows its own text (lib/openapi-markdown.ts).
|
|
83
|
+
const content = pageMarkdown(entry, { contentDir, packageRoot, variables: config.variables, siteUrl: siteUrl || null });
|
|
84
|
+
return {
|
|
85
|
+
slug,
|
|
86
|
+
fileId,
|
|
87
|
+
doc: {
|
|
88
|
+
path: fileId,
|
|
89
|
+
url: `${siteUrl}${slug === 'index' ? '/' : `/${slug}/`}`,
|
|
90
|
+
title: entry.data.title,
|
|
91
|
+
description: entry.data.description ?? '',
|
|
92
|
+
content,
|
|
93
|
+
...placeOf(fileId),
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
meta: { name: config.name, description: config.description ?? '', siteUrl, generator: 'writedocs' },
|
|
100
|
+
docs: orderByNavigation(docs, config.navigation).map((d) => d.doc),
|
|
101
|
+
};
|
|
102
|
+
}
|