|
| 1 | +import path from 'node:path'; |
| 2 | +import { joinPath } from '@/lib/paths'; |
| 3 | +import { getIndexablePages } from '@/lib/sitemap'; |
| 4 | +import { getSiteStructureSections } from '@/lib/sites'; |
| 5 | +import { checkIsAnchor, checkIsExternalURL } from '@/lib/urls'; |
| 6 | +import type { RevisionPageDocument, SiteSection, SiteSpace } from '@gitbook/api'; |
| 7 | +import { type GitBookSiteContext, checkIsRootSiteContext } from '@v2/lib/context'; |
| 8 | +import { throwIfDataError } from '@v2/lib/data'; |
| 9 | +import assertNever from 'assert-never'; |
| 10 | +import type { Link, Paragraph, Root } from 'mdast'; |
| 11 | +import { fromMarkdown } from 'mdast-util-from-markdown'; |
| 12 | +import { frontmatterFromMarkdown } from 'mdast-util-frontmatter'; |
| 13 | +import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm'; |
| 14 | +import { toMarkdown } from 'mdast-util-to-markdown'; |
| 15 | +import { frontmatter } from 'micromark-extension-frontmatter'; |
| 16 | +import { gfm } from 'micromark-extension-gfm'; |
| 17 | +import { pMapIterable } from 'p-map'; |
| 18 | +import { remove } from 'unist-util-remove'; |
| 19 | +import { visit } from 'unist-util-visit'; |
| 20 | + |
| 21 | +// We limit the concurrency to 100 to avoid reaching limit with concurrent requests |
| 22 | +// or file descriptor limits. |
| 23 | +const MAX_CONCURRENCY = 100; |
| 24 | + |
| 25 | +/** |
| 26 | + * Generate a llms-full.txt file for the site. |
| 27 | + * As the result can be large, we stream it as we generate it. |
| 28 | + */ |
| 29 | +export async function serveLLMsFullTxt(context: GitBookSiteContext) { |
| 30 | + if (!checkIsRootSiteContext(context)) { |
| 31 | + return new Response('llms.txt is only served from the root of the site', { status: 404 }); |
| 32 | + } |
| 33 | + |
| 34 | + return new Response( |
| 35 | + new ReadableStream<Uint8Array>({ |
| 36 | + async pull(controller) { |
| 37 | + await streamMarkdownFromSiteStructure(context, controller); |
| 38 | + controller.close(); |
| 39 | + }, |
| 40 | + }), |
| 41 | + { |
| 42 | + headers: { |
| 43 | + 'Content-Type': 'text/markdown; charset=utf-8', |
| 44 | + }, |
| 45 | + } |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Stream markdown from site structure. |
| 51 | + */ |
| 52 | +async function streamMarkdownFromSiteStructure( |
| 53 | + context: GitBookSiteContext, |
| 54 | + stream: ReadableStreamDefaultController<Uint8Array> |
| 55 | +): Promise<void> { |
| 56 | + switch (context.structure.type) { |
| 57 | + case 'sections': |
| 58 | + return streamMarkdownFromSections( |
| 59 | + context, |
| 60 | + stream, |
| 61 | + getSiteStructureSections(context.structure, { ignoreGroups: true }) |
| 62 | + ); |
| 63 | + case 'siteSpaces': |
| 64 | + return streamMarkdownFromSiteSpaces(context, stream, context.structure.structure, ''); |
| 65 | + default: |
| 66 | + assertNever(context.structure); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Stream markdown from site sections. |
| 72 | + */ |
| 73 | +async function streamMarkdownFromSections( |
| 74 | + context: GitBookSiteContext, |
| 75 | + stream: ReadableStreamDefaultController<Uint8Array>, |
| 76 | + siteSections: SiteSection[] |
| 77 | +): Promise<void> { |
| 78 | + for (const siteSection of siteSections) { |
| 79 | + await streamMarkdownFromSiteSpaces( |
| 80 | + context, |
| 81 | + stream, |
| 82 | + siteSection.siteSpaces, |
| 83 | + siteSection.path |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Stream markdown from site spaces. |
| 90 | + */ |
| 91 | +async function streamMarkdownFromSiteSpaces( |
| 92 | + context: GitBookSiteContext, |
| 93 | + stream: ReadableStreamDefaultController<Uint8Array>, |
| 94 | + siteSpaces: SiteSpace[], |
| 95 | + basePath: string |
| 96 | +): Promise<void> { |
| 97 | + const { dataFetcher } = context; |
| 98 | + |
| 99 | + for (const siteSpace of siteSpaces) { |
| 100 | + const siteSpaceUrl = siteSpace.urls.published; |
| 101 | + if (!siteSpaceUrl) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + const rootPages = await throwIfDataError( |
| 105 | + dataFetcher.getRevisionPages({ |
| 106 | + spaceId: siteSpace.space.id, |
| 107 | + revisionId: siteSpace.space.revision, |
| 108 | + metadata: false, |
| 109 | + }) |
| 110 | + ); |
| 111 | + const pages = getIndexablePages(rootPages); |
| 112 | + |
| 113 | + for await (const markdown of pMapIterable( |
| 114 | + pages, |
| 115 | + async ({ page }) => { |
| 116 | + if (page.type !== 'document') { |
| 117 | + return ''; |
| 118 | + } |
| 119 | + |
| 120 | + return getMarkdownForPage( |
| 121 | + context, |
| 122 | + siteSpace, |
| 123 | + page, |
| 124 | + joinPath(basePath, siteSpace.path) |
| 125 | + ); |
| 126 | + }, |
| 127 | + { |
| 128 | + concurrency: MAX_CONCURRENCY, |
| 129 | + } |
| 130 | + )) { |
| 131 | + stream.enqueue(new TextEncoder().encode(markdown)); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Get markdown from a page. |
| 138 | + */ |
| 139 | +async function getMarkdownForPage( |
| 140 | + context: GitBookSiteContext, |
| 141 | + siteSpace: SiteSpace, |
| 142 | + page: RevisionPageDocument, |
| 143 | + basePath: string |
| 144 | +): Promise<string> { |
| 145 | + const { dataFetcher } = context; |
| 146 | + |
| 147 | + const pageMarkdown = await throwIfDataError( |
| 148 | + dataFetcher.getRevisionPageMarkdown({ |
| 149 | + spaceId: siteSpace.space.id, |
| 150 | + revisionId: siteSpace.space.revision, |
| 151 | + pageId: page.id, |
| 152 | + }) |
| 153 | + ); |
| 154 | + |
| 155 | + const tree = fromMarkdown(pageMarkdown, { |
| 156 | + extensions: [frontmatter(['yaml']), gfm()], |
| 157 | + mdastExtensions: [frontmatterFromMarkdown(['yaml']), gfmFromMarkdown()], |
| 158 | + }); |
| 159 | + |
| 160 | + // Remove frontmatter |
| 161 | + remove(tree, 'yaml'); |
| 162 | + |
| 163 | + if (page.description) { |
| 164 | + // The first node is the page title as a H1, we insert the description as a paragraph |
| 165 | + // after it. |
| 166 | + const descriptionNode: Paragraph = { |
| 167 | + type: 'paragraph', |
| 168 | + children: [{ type: 'text', value: page.description }], |
| 169 | + }; |
| 170 | + tree.children.splice(1, 0, descriptionNode); |
| 171 | + } |
| 172 | + |
| 173 | + // Rewrite relative links to absolute links |
| 174 | + transformLinks(context, tree, { currentPagePath: page.path, basePath }); |
| 175 | + |
| 176 | + const markdown = toMarkdown(tree, { extensions: [gfmToMarkdown()] }); |
| 177 | + return `${markdown}\n\n`; |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * Re-writes the URL of every relative <a> link so it is expressed from the site-root. |
| 182 | + */ |
| 183 | +export function transformLinks( |
| 184 | + context: GitBookSiteContext, |
| 185 | + tree: Root, |
| 186 | + options: { currentPagePath: string; basePath: string } |
| 187 | +): Root { |
| 188 | + const { linker } = context; |
| 189 | + const { currentPagePath, basePath } = options; |
| 190 | + const currentDir = path.posix.dirname(currentPagePath); |
| 191 | + |
| 192 | + visit(tree, 'link', (node: Link) => { |
| 193 | + const original = node.url; |
| 194 | + |
| 195 | + // Skip anchors, mailto:, http(s):, protocol-like, or already-rooted paths |
| 196 | + if (checkIsExternalURL(original) || checkIsAnchor(original) || original.startsWith('/')) { |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + // Resolve against the current page’s directory and strip any leading “/” |
| 201 | + const pathInSite = path.posix |
| 202 | + .normalize(path.posix.join(basePath, currentDir, original)) |
| 203 | + .replace(/^\/+/, ''); |
| 204 | + |
| 205 | + node.url = linker.toPathInSite(pathInSite); |
| 206 | + }); |
| 207 | + |
| 208 | + return tree; |
| 209 | +} |
0 commit comments