Skip to content

Commit e27a6b7

Browse files
committed
Fix broken links with structure-dir router
Resolves #2928
1 parent e577bfc commit e27a6b7

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ title: Changelog
2020
- When printing entry point globs which fail to match any paths, TypeDoc will no longer normalize the glob, #2918.
2121
- Inlining types can now handle more type variants, #2920.
2222
- Fixed behavior of `externalSymbolLinkMappings` option when URL is set to `#`, #2921.
23+
- Fixed broken links within module pages when structure-dir router was used, #2928.
2324
- API: `toString` on types containing index signatures now behave correctly, #2917.
2425

2526
## v0.28.1 (2025-03-20)

src/lib/output/router.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ export abstract class BaseRouter implements Router {
221221
// an own document.
222222
from = from.parent as RouterTarget;
223223
}
224+
225+
let toPage = to;
226+
while (!this.hasOwnDocument(toPage)) {
227+
toPage = toPage.parent as RouterTarget;
228+
}
229+
230+
// We unfortunately have to special case ProjectReflection as it is
231+
// the model used twice for rendering. This should be changed in a
232+
// future version to remove this hackery.
233+
if (from === toPage && !(to instanceof ProjectReflection)) {
234+
return to === toPage ? "" : `#${this.getAnchor(to)}`;
235+
}
236+
224237
const fromUrl = this.getFullUrl(from);
225238
const toUrl = this.getFullUrl(to);
226239
let equal = true;
@@ -237,13 +250,15 @@ export abstract class BaseRouter implements Router {
237250
}
238251
}
239252

240-
if (equal && !(to instanceof ProjectReflection)) {
241-
if (fromUrl === toUrl) {
242-
return "";
243-
}
244-
return `#${this.getAnchor(to)}`;
253+
// If equal is still set, we're going to a page either in
254+
// the same directory as this page, or a lower directory,
255+
// don't bother going up directories just to come back down.
256+
if (equal) {
257+
return toUrl.substring(start);
245258
}
246259

260+
// Otherwise, go up until we get to the common directory
261+
// and then back down to the target path.
247262
return "../".repeat(slashes) + toUrl.substring(start);
248263
}
249264

src/test/output/router.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ describe("StructureDirRouter", () => {
330330
),
331331
"../../Foo/#codegeneration",
332332
);
333+
334+
equal(
335+
router.relativeUrl(
336+
query(project, "Nested"),
337+
query(project, "Nested.refl"),
338+
),
339+
"refl/",
340+
);
341+
342+
equal(router.relativeUrl(query(project, "Nested.refl"), query(project, "Nested")), "../");
333343
});
334344
});
335345

0 commit comments

Comments
 (0)