diff --git a/packages/api-model/src/languages/lua-examples.ts b/packages/api-model/src/languages/lua-examples.ts index 8abbde228..da9c4da7d 100644 --- a/packages/api-model/src/languages/lua-examples.ts +++ b/packages/api-model/src/languages/lua-examples.ts @@ -69,6 +69,7 @@ export function attachCompleteLuaExamples( : `Save the complete program as \`${name}\`. Closing the library connection leaves the daemon running; the launcher owns its lifetime.`, sourceUrl: url(example.file), code, + sourcePath: example.file, }, launcher === undefined ? { diff --git a/packages/api-model/src/model.ts b/packages/api-model/src/model.ts index 7a177e165..303888010 100644 --- a/packages/api-model/src/model.ts +++ b/packages/api-model/src/model.ts @@ -243,6 +243,8 @@ export interface DocBlock { outro?: string /** Source for this complete example, independent of native comment syntax. */ sourceUrl?: string + /** Repository-relative path of a validated complete program. */ + sourcePath?: string }[] /** * Citations the docstring defines, as `.. [name] text`. diff --git a/scripts/stage-port-docs.mjs b/scripts/stage-port-docs.mjs index 924380902..d470bcbe3 100644 --- a/scripts/stage-port-docs.mjs +++ b/scripts/stage-port-docs.mjs @@ -223,13 +223,45 @@ export function lispReferenceRoutes(symbols) { return routes } -export function stagedPortGuides(port, artifact) { +function luaExampleRoutes(artifact, model) { + if (model.repo !== artifact.source.repository || model.revision !== artifact.source.revision) return [] + const files = Map.groupBy(artifact.examples ?? [], (file) => file.path) + const targets = Map.groupBy(model.symbols.filter((symbol) => + symbol.slug && symbol.apiScope !== 'internal' && (symbol.product ?? 'core') === 'core'), (symbol) => symbol.id) + const routes = [] + for (const example of artifact.api_examples.examples) { + const sources = files.get(example.file) + if (sources?.length !== 1) continue + const [{ content }] = sources + // The native manifest orders the targets for each complete program. + for (const id of example.symbols) { + const matches = targets.get(id) + if (matches?.length !== 1) continue + const symbol = matches[0] + if (!symbol.doc?.examples?.some((block) => + block.lang === 'lua' && block.sourcePath === example.file && block.code === content)) continue + routes.push([example.file, `reference/${symbol.slug}`]) + break + } + } + return routes +} + +export function stagedPortGuides(port, artifact, apiModel) { const identity = PORTS.find((entry) => entry.slug === port) - const routes = ROUTES[port] + const guides = new Map((artifact.guides ?? []).map((guide) => [guide.path, guide.content])) + const routes = Object.fromEntries(Object.entries(ROUTES[port]).filter(([path, guide]) => !guide.optional || guides.has(path))) const linkRoutes = Object.fromEntries(Object.entries(routes).map(([sourcePath, guide]) => [sourcePath, [guide.route]])) const nativeReferenceFiles = new Set() const nativeDeclarations = new Map() for (const redirect of sourceGuideRedirectsFor(port)) if (redirect.sourcePath) linkRoutes[redirect.sourcePath] = [redirect.target] + if (port === 'lua' && guides.has('docs/programs.md') && artifact.api_examples?.examples?.length) { + const model = apiModel ?? JSON.parse(readFileSync(join(root, 'site/src/data/api/lua.json'), 'utf8')) + for (const [file, route] of luaExampleRoutes(artifact, model)) { + linkRoutes[file] = [route] + nativeReferenceFiles.add(file) + } + } if (port === 'haskell') { const model = JSON.parse(readFileSync(join(root, 'site/src/data/api/haskell.json'), 'utf8')) if (model.revision !== artifact.source.revision) throw new Error('haskell: guide source differs from the Haskell API model') @@ -291,9 +323,13 @@ export function stagedPortGuides(port, artifact) { if (parent) linkRoutes[`${directory}/${parent.slug}.md#${symbol.name}`] = [`reference/${symbol.slug}`] } } - const guides = new Map((artifact.guides ?? []).map((guide) => [guide.path, guide.content])) const files = new Map() const descriptions = new Map() + const checkoutGuidePaths = new Set(port === 'lua' ? artifact.checkoutGuidePaths : undefined) + const sourceFor = (path) => ({ + repo: artifact.source.repository, path, + ...(checkoutGuidePaths.has(path) ? { workingCopy: true } : { ref: artifact.source.revision }), + }) const titles = new Map(Object.entries(routes).map(([path, entry]) => [entry.route, entry.title ?? titleAndBody(guides.get(path) ?? '', path).title])) for (const [sourcePath, guide] of Object.entries(routes)) { const { route, product, package: packageId, domain, aliases, equivalence, sidebar } = guide @@ -318,7 +354,7 @@ export function stagedPortGuides(port, artifact) { ...(aliases.length ? { aliases } : {}), ...(equivalence ? { equivalence } : {}), route, - source: { repo: artifact.source.repository, path: sourcePath, ref: artifact.source.revision }, + source: sourceFor(sourcePath), sidebar: sidebar ?? { group: product ? (product === 'mcp' ? 'MCP' : 'Workspace Manager') : route === 'reference' ? 'API reference' : route.split('/')[0].replace(/^./, (letter) => letter.toUpperCase()) }, } @@ -327,7 +363,7 @@ export function stagedPortGuides(port, artifact) { files.set(`${port}/${route}/index.md`, `---\n${frontmatter}\n---\n\n${rewritten.trim()}\n`) } { - const source = { repo: artifact.source.repository, path: Object.keys(routes)[0], ref: artifact.source.revision } + const source = sourceFor(Object.keys(routes)[0]) const writeIndex = (route, title, body, cards = []) => { const data = { title, description: leadSentence(body) || `${title} for ${identity.packageName}.`, port, route, source, cards, sidebar: { group: route === 'reference' ? 'API reference' : route[0].toUpperCase() + route.slice(1), label: 'Overview', order: 1 } } @@ -412,7 +448,7 @@ function linkDocumenter(content, source) { } /** Read the integrated commit even when a contributor's working tree has advanced. */ -export function artifactFromRevision(port, checkout, revision) { +export function artifactFromRevision(port, checkout, revision, { allowMissing = false } = {}) { if (!/^[a-f0-9]{40}$/.test(revision)) throw new Error(`${port.slug}: invalid integrated source revision`) const git = (...args) => execFileSync('git', ['-C', checkout, ...args], { encoding: 'utf8', stdio: 'pipe' }) try { @@ -420,7 +456,8 @@ export function artifactFromRevision(port, checkout, revision) { } catch (cause) { throw new Error(`${port.slug}: integrated guides need ${port.repo}@${revision} in ${checkout}. Set LIBTMUX_DOCS_CHECKOUT_${port.slug.toUpperCase()} to a local checkout containing that commit; this check does not fetch.`, { cause }) } - const paths = Object.keys(ROUTES[port.slug]) + const routes = ROUTES[port.slug] + const paths = Object.keys(routes) const blobs = execFileSync('git', ['-C', checkout, 'cat-file', '--batch'], { input: paths.map((path) => `${revision}:${path}\n`).join(''), maxBuffer: 16 * 1024 * 1024, stdio: ['pipe', 'pipe', 'pipe'], @@ -430,6 +467,10 @@ export function artifactFromRevision(port, checkout, revision) { for (const path of paths) { const end = blobs.indexOf(10, offset) const header = blobs.subarray(offset, end).toString('ascii') + if (end >= offset && (allowMissing || routes[path].optional) && header === `${revision}:${path} missing`) { + offset = end + 1 + continue + } const match = /^[a-f0-9]{40} blob (\d+)$/.exec(header) const size = match && Number(match[1]) if (end < offset || size === null || !Number.isSafeInteger(size) || size < 0 || @@ -443,11 +484,26 @@ export function artifactFromRevision(port, checkout, revision) { const readSource = (file) => contents.get(file) ?? git('show', `${revision}:${file}`) return { source: { repository: port.repo, revision }, - guides: paths.map((path) => { - const content = readSource(path) - return { path, content: port.slug === 'julia' ? expandDocumenter(content, path, readSource) : content } - }), + guides: [...contents].map(([path, content]) => ({ + path, content: port.slug === 'julia' ? expandDocumenter(content, path, readSource) : content, + })), + } +} + +function luaCheckoutGuides(port, checkout, artifact) { + const { revision } = artifact.source + const pinned = artifactFromRevision(port, checkout, revision, { allowMissing: true }) + const contents = new Map(pinned.guides.map(({ path, content }) => [path, content])) + const checkoutGuidePaths = [] + for (const { path, content } of artifact.guides ?? []) { + if (!Object.hasOwn(ROUTES.lua, path) || typeof content !== 'string' || contents.get(path) === content) continue + const file = join(checkout, path) + if (!existsSync(file) || readFileSync(file, 'utf8') !== content) { + throw new Error(`lua: exported guide ${path} differs from both ${revision} and ${file}. Regenerate docs/_build/api.json from the selected checkout.`) + } + checkoutGuidePaths.push(path) } + return checkoutGuidePaths.length ? { ...artifact, checkoutGuidePaths } : artifact } export function run() { @@ -478,20 +534,23 @@ export function run() { if (artifact.source.repository !== port.repo || !/^[a-f0-9]{40}$/.test(artifact.source.revision)) { throw new Error(`${port.slug}: invalid source guide provenance`) } + let model if (!port.parentLibrary) { - const model = JSON.parse(readFileSync(join(root, `site/src/data/api/${port.slug}.json`), 'utf8')) + model = JSON.parse(readFileSync(join(root, `site/src/data/api/${port.slug}.json`), 'utf8')) if (artifact.source.revision !== model.revision) throw new Error(`${port.slug}: guide source ${artifact.source.revision} differs from integrated model ${model.revision}`) } const expected = selectedSource if (expected && artifact.source?.revision !== expected) { throw new Error(`${port.slug}: expected source ${expected}, artifact records ${artifact.source?.revision}`) } + const stagingArtifact = port.slug === 'lua' && !cached && !integrated && !fromSource && existsSync(join(checkout, '.git')) + ? luaCheckoutGuides(port, checkout, artifact) : artifact if (!check && (refreshCache || selectedSource)) { mkdirSync(cacheRoot, { recursive: true }) - const content = `${JSON.stringify(artifact, null, 2)}\n` + const content = `${JSON.stringify(stagingArtifact, null, 2)}\n` if (!existsSync(cachePath) || readFileSync(cachePath, 'utf8') !== content) writeFileSync(cachePath, content) } - for (const [file, content] of stagedPortGuides(port.slug, artifact)) generated.set(file, content) + for (const [file, content] of stagedPortGuides(port.slug, stagingArtifact, model)) generated.set(file, content) } const actual = new Map() diff --git a/site/src/components/SiteFooter.astro b/site/src/components/SiteFooter.astro index 633c426db..d18b9edd4 100644 --- a/site/src/components/SiteFooter.astro +++ b/site/src/components/SiteFooter.astro @@ -32,6 +32,7 @@ interface Props { const { locale, port, version, pagePath, source, markdownHref, llmsRoot, githubUrl, flush = false, compact = false } = Astro.props const inventories = inventoryLinks(port, version, pagePath) +const rawSource = source && rawSourceUrl(source) ---