|
| 1 | +import {existsSync, readdirSync, readFileSync, statSync} from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import {fileURLToPath} from 'node:url'; |
| 4 | +import {unified} from 'unified'; |
| 5 | +import remarkParse from 'remark-parse'; |
| 6 | + |
| 7 | +const websiteDirectory = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 8 | +const buildDirectory = path.join(websiteDirectory, 'build'); |
| 9 | +const llmsTxtPath = path.join(buildDirectory, 'llms.txt'); |
| 10 | +const llmsFullTxtPath = path.join(buildDirectory, 'llms-full.txt'); |
| 11 | +const websiteOrigin = 'https://visgl.github.io'; |
| 12 | +const websiteBasePath = normalizeWebsiteBasePath('/probe.gl/'); |
| 13 | +const websiteUrl = new URL(websiteBasePath, websiteOrigin); |
| 14 | +const duplicatedWebsiteBaseUrl = |
| 15 | + websiteBasePath === '/' ? null : new URL(websiteBasePath.slice(1), websiteUrl).href; |
| 16 | + |
| 17 | +function normalizeWebsiteBasePath(basePath) { |
| 18 | + const pathSegments = basePath.split('/').filter(Boolean); |
| 19 | + return pathSegments.length === 0 ? '/' : `/${pathSegments.join('/')}/`; |
| 20 | +} |
| 21 | + |
| 22 | +function stripWebsiteBasePath(pathname) { |
| 23 | + if (websiteBasePath === '/' || !pathname.startsWith(websiteBasePath)) { |
| 24 | + return pathname; |
| 25 | + } |
| 26 | + return `/${pathname.slice(websiteBasePath.length)}`; |
| 27 | +} |
| 28 | + |
| 29 | +function fail(message) { |
| 30 | + throw new Error(`llms.txt output check failed: ${message}`); |
| 31 | +} |
| 32 | + |
| 33 | +function requireFile(relativePath) { |
| 34 | + const filePath = path.join(buildDirectory, relativePath); |
| 35 | + if (!existsSync(filePath) || !statSync(filePath).isFile()) { |
| 36 | + fail(`missing ${relativePath}`); |
| 37 | + } |
| 38 | + return filePath; |
| 39 | +} |
| 40 | + |
| 41 | +function findFiles(directory, extension) { |
| 42 | + if (!existsSync(directory)) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + const filePaths = []; |
| 47 | + for (const entry of readdirSync(directory, {withFileTypes: true})) { |
| 48 | + const entryPath = path.join(directory, entry.name); |
| 49 | + if (entry.isDirectory()) { |
| 50 | + filePaths.push(...findFiles(entryPath, extension)); |
| 51 | + } else if (entry.isFile() && entryPath.endsWith(extension)) { |
| 52 | + filePaths.push(entryPath); |
| 53 | + } |
| 54 | + } |
| 55 | + return filePaths; |
| 56 | +} |
| 57 | + |
| 58 | +const markdownParser = unified().use(remarkParse); |
| 59 | + |
| 60 | +function extractMarkdownLinks(markdown) { |
| 61 | + const links = []; |
| 62 | + function visit(node) { |
| 63 | + // Definitions supply destinations for full, collapsed, and shortcut references. |
| 64 | + if (['link', 'image', 'definition'].includes(node.type)) { |
| 65 | + links.push(node.url); |
| 66 | + } |
| 67 | + for (const child of node.children || []) { |
| 68 | + visit(child); |
| 69 | + } |
| 70 | + } |
| 71 | + visit(markdownParser.parse(markdown)); |
| 72 | + return links; |
| 73 | +} |
| 74 | + |
| 75 | +function resolveGeneratedMarkdownLink(sourcePath, link) { |
| 76 | + if (link.startsWith('#') || link.startsWith('mailto:')) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + let pathname; |
| 81 | + try { |
| 82 | + const url = new URL(link); |
| 83 | + if (url.origin !== websiteUrl.origin) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + pathname = decodeURIComponent(url.pathname); |
| 87 | + } catch { |
| 88 | + const linkWithoutFragment = link.split('#', 1)[0].split('?', 1)[0]; |
| 89 | + if (!linkWithoutFragment.endsWith('.md')) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + if (linkWithoutFragment.startsWith('/')) { |
| 93 | + pathname = decodeURIComponent(linkWithoutFragment); |
| 94 | + } else { |
| 95 | + return path.resolve(path.dirname(sourcePath), decodeURIComponent(linkWithoutFragment)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (!pathname.endsWith('.md')) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + pathname = stripWebsiteBasePath(pathname); |
| 103 | + if (path.isAbsolute(pathname) && pathname.startsWith(buildDirectory)) { |
| 104 | + return pathname; |
| 105 | + } |
| 106 | + return path.join(buildDirectory, pathname.replace(/^\/+/, '')); |
| 107 | +} |
| 108 | + |
| 109 | +if (!existsSync(llmsTxtPath)) { |
| 110 | + fail('missing llms.txt'); |
| 111 | +} |
| 112 | +if (existsSync(llmsFullTxtPath)) { |
| 113 | + fail('llms-full.txt must not be generated'); |
| 114 | +} |
| 115 | + |
| 116 | +const llmsTxt = readFileSync(llmsTxtPath, 'utf8'); |
| 117 | +if (!llmsTxt.startsWith('# probe.gl\n')) { |
| 118 | + fail('llms.txt has an unexpected title'); |
| 119 | +} |
| 120 | +const firstSectionHeading = llmsTxt.match(/^## .+$/m)?.[0]; |
| 121 | +if (firstSectionHeading !== '## docs') { |
| 122 | + fail(`llms.txt has an unexpected first section: ${firstSectionHeading || 'none'}`); |
| 123 | +} |
| 124 | +if (llmsTxt.includes('/examples/')) { |
| 125 | + fail('llms.txt contains a standalone example page'); |
| 126 | +} |
| 127 | + |
| 128 | +// Use Docusaurus metadata rather than the generated Markdown as the coverage source. |
| 129 | +// Permalinks account for frontmatter slugs and the website's route normalization. |
| 130 | +const docsMetadataDirectory = path.join( |
| 131 | + websiteDirectory, '.docusaurus/docusaurus-plugin-content-docs/default' |
| 132 | +); |
| 133 | +const currentDocs = findFiles(docsMetadataDirectory, '.json') |
| 134 | + .map((filePath) => JSON.parse(readFileSync(filePath, 'utf8'))) |
| 135 | + .filter((doc) => doc.version === 'current' && doc.permalink && !doc.draft); |
| 136 | +if (currentDocs.length === 0) { |
| 137 | + fail('missing current documentation route metadata'); |
| 138 | +} |
| 139 | +for (const doc of currentDocs) { |
| 140 | + const pathname = new URL(doc.permalink, websiteUrl).pathname.replace(/\/$/, '') + '.md'; |
| 141 | + const pageUrl = new URL(pathname, websiteUrl).href; |
| 142 | + if (!llmsTxt.includes(pageUrl)) { |
| 143 | + fail(`llms.txt is missing ${pageUrl}`); |
| 144 | + } |
| 145 | + requireFile(stripWebsiteBasePath(pathname).replace(/^\/+/, '')); |
| 146 | +} |
| 147 | + |
| 148 | +const markdownFiles = findFiles(buildDirectory, '.md'); |
| 149 | + |
| 150 | +const brokenLinks = []; |
| 151 | +for (const markdownPath of [llmsTxtPath, ...markdownFiles]) { |
| 152 | + const markdown = readFileSync(markdownPath, 'utf8'); |
| 153 | + const relativeMarkdownPath = path.relative(buildDirectory, markdownPath); |
| 154 | + if (markdownPath !== llmsTxtPath) { |
| 155 | + const pageUrl = new URL(relativeMarkdownPath.split(path.sep).join('/'), websiteUrl).href; |
| 156 | + if (!llmsTxt.includes(pageUrl)) { |
| 157 | + fail(`llms.txt is missing generated page ${pageUrl}`); |
| 158 | + } |
| 159 | + } |
| 160 | + if (markdown.trim().length < 40) { |
| 161 | + fail(`${relativeMarkdownPath} has empty extracted content`); |
| 162 | + } |
| 163 | + if (duplicatedWebsiteBaseUrl && markdown.includes(duplicatedWebsiteBaseUrl)) { |
| 164 | + fail(`${relativeMarkdownPath} contains a duplicated website base path`); |
| 165 | + } |
| 166 | + |
| 167 | + for (const link of extractMarkdownLinks(markdown)) { |
| 168 | + const targetPath = resolveGeneratedMarkdownLink(markdownPath, link); |
| 169 | + if (targetPath && !existsSync(targetPath)) { |
| 170 | + brokenLinks.push( |
| 171 | + `${path.relative(buildDirectory, markdownPath)} -> ${path.relative( |
| 172 | + buildDirectory, |
| 173 | + targetPath |
| 174 | + )}` |
| 175 | + ); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +if (brokenLinks.length > 0) { |
| 181 | + fail(`broken Markdown links:\n${brokenLinks.slice(0, 20).join('\n')}`); |
| 182 | +} |
| 183 | + |
| 184 | +console.log(`Validated llms.txt and ${markdownFiles.length} raw documentation pages.`); |
0 commit comments