Skip to content

Commit 4d9d4a9

Browse files
dmealingclaude
andcommitted
refactor: dedupe apiRefs mapper + drop redundant surface re-map (behavior-preserving)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 53723cc commit 4d9d4a9

2 files changed

Lines changed: 17 additions & 27 deletions

File tree

server/typescript/packages/cli/src/commands/docs.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,10 @@ export async function docsCommand(args: string[], cwd: string): Promise<number>
281281
// README.md). Keep the render-error handling tight around docsFile() only.
282282
if (docsCfg.surfaces.includes("model")) {
283283
// When api is selected, link EVERY declared surface from the model page (so a
284-
// polyglot model page references each port's api docs). Otherwise no api refs.
285-
const modelOpts = apiSelected
286-
? {
287-
apiSurfaces: labeled.map(({ label, subDir, baseUrl }) => ({
288-
label,
289-
subDir,
290-
...(baseUrl ? { baseUrl } : {}),
291-
})),
292-
}
293-
: {};
284+
// polyglot model page references each port's api docs). `labeled` already
285+
// carries {label, subDir, baseUrl?} (docsFile ignores the extra `lang`), so
286+
// pass it straight through. Otherwise no api refs.
287+
const modelOpts = apiSelected ? { apiSurfaces: labeled } : {};
294288
try {
295289
modelFiles = await docsFile(modelOpts).generate(ctx);
296290
} catch (err) {

server/typescript/packages/codegen-ts/src/generators/docs-file.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ export const docsFile = function docsFile(opts?: DocsFileOpts): Generator {
9494
const rc = ctx.renderContext;
9595
const provider = projectProvider(ctx.projectRoot ?? process.cwd());
9696
const layout = ctx.config.outputLayout ?? "flat";
97+
// One {label, href} per api surface, every href computed via the shared
98+
// `apiSurfaceHref` from the FROM page's own output path (so it resolves
99+
// relative in BOTH layouts, or absolute when the surface declares a
100+
// baseUrl). The api page for a node lives at `<subDir>/<same placement>`,
101+
// so the from-path doubles as the page placement. ABSENT/empty surfaces ⇒
102+
// undefined → output byte-identical to historical model-only runs.
103+
const apiRefsFor = (fromPath: string): Array<{ label: string; href: string }> | undefined =>
104+
opts?.apiSurfaces?.map((s) => ({ label: s.label, href: apiSurfaceHref(fromPath, s, fromPath) }));
97105
// Track every (path, fqn) so we can hard-error on a collision (defense
98106
// against silent doc-page overwrite) AFTER all pages are placed.
99107
const placements: DocPagePlacement[] = [];
@@ -110,15 +118,8 @@ export const docsFile = function docsFile(opts?: DocsFileOpts): Generator {
110118
entityNodes.push(node);
111119
const path = docPageOutputPath(layout, node);
112120
placements.push({ path, fqn: entity.resolutionKey() });
113-
// Cross-link to the sibling api surfaces, when emitted. The api page for
114-
// this node lives at `<subDir>/<same placement>` per surface; each href
115-
// is derived from the SAME docPageOutputPath placement via apiSurfaceHref
116-
// so it resolves in both flat and package layout (or absolute when the
117-
// surface declares a baseUrl). ABSENT otherwise.
118-
const apiRefs = opts?.apiSurfaces?.map((s) => ({
119-
label: s.label,
120-
href: apiSurfaceHref(path, s, path),
121-
}));
121+
// Cross-link to the sibling api surfaces, when emitted (shared builder).
122+
const apiRefs = apiRefsFor(path);
122123
const payload = buildEntityDocData(entity, {
123124
dialect: rc.dialect,
124125
layout,
@@ -157,14 +158,9 @@ export const docsFile = function docsFile(opts?: DocsFileOpts): Generator {
157158
// produces nothing (no orphan landing page with an empty diagram).
158159
if (files.length > 0) {
159160
// The api index lives at `<subDir>/README.md` per surface; the model index
160-
// lives at the docs root, so each cross-link href is computed via the
161-
// shared `apiSurfaceHref` from the root-level index path (relative-path
162-
// rule shared, never hand-rolled; absolute when a baseUrl is given).
163-
// ABSENT when no api surface is emitted → index output byte-identical.
164-
const apiIndexRefs = opts?.apiSurfaces?.map((s) => ({
165-
label: s.label,
166-
href: apiSurfaceHref(INDEX_FILENAME, s, INDEX_FILENAME),
167-
}));
161+
// lives at the docs root, so the from-path is the root-level index path
162+
// (shared builder — same relative/absolute rule as the entity refs).
163+
const apiIndexRefs = apiRefsFor(INDEX_FILENAME);
168164
const indexContent = renderIndexPage(
169165
ctx.loadedRoot,
170166
layout,

0 commit comments

Comments
 (0)