Skip to content

Commit 1fe0a7a

Browse files
authored
Rely on URL paths when choosing among multiple indices (#929)
Resolves: rdar://142337934
1 parent 53cfd22 commit 1fe0a7a

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/utils/navigatorData.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,21 @@ export function getSiblings(uid, childrenMap, children) {
179179
}
180180

181181
function extractRootNode(data) {
182+
// note: this "root" path won't always necessarily come at the beginning of
183+
// the URL in situations where the renderer is being hosted at some path
184+
// prefix
185+
const rootPathPattern = /(\/documentation\/[^/]+)/;
186+
const rootPath = window.location.href.match(rootPathPattern)?.[1] ?? '';
182187
// most of the time, it is expected that `data` always has a single item
183188
// that represents the top-level root node of the navigation tree
184189
//
185190
// there may be rare, unexpected scenarios where multiple top-level root
186-
// nodes are provide for some reason—if that happens, we would prefer the
187-
// first one categorized as a "module"
191+
// nodes are provide for some reason—if that happens, we would prefer the one
192+
// with a path that most closely resembles the current URL path
188193
//
189194
// otherwise, the first provided node will be used
190195
return data.length === 1 ? data[0] : (data.find(node => (
191-
node.type === TopicTypes.module
196+
node.path.toLowerCase() === rootPath.toLowerCase()
192197
)) ?? data[0]);
193198
}
194199

tests/unit/utils/navigatorData.spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ describe('when multiple top-level children are provided', () => {
359359
};
360360

361361
describe('flattenNavigationIndex', () => {
362-
it('prefers modules', () => {
362+
it('prefers the root child with the same url path prefix', () => {
363+
Object.defineProperty(window, 'location', {
364+
value: { href: 'http://localhost/documentation/b/b42' },
365+
});
366+
363367
// use first root node if only one is provided
364368
let flattenedIndex = flattenNavigationIndex({ swift: [a] });
365369
expect(flattenedIndex.swift.length).toBe(1);
@@ -368,7 +372,7 @@ describe('when multiple top-level children are provided', () => {
368372
expect(flattenedIndex.swift.length).toBe(1);
369373
expect(flattenedIndex.swift[0].title).toBe(b.children[0].title);
370374

371-
// prefer "module" root when multiple top-level nodes are provided
375+
// prefers root node with same url path prefix when multiple are provided
372376
flattenedIndex = flattenNavigationIndex({ swift: [a, b] });
373377
expect(flattenedIndex.swift.length).toBe(1);
374378
expect(flattenedIndex.swift[0].title).toBe(b.children[0].title);
@@ -388,14 +392,18 @@ describe('when multiple top-level children are provided', () => {
388392
});
389393

390394
describe('extractTechnologyProps', () => {
391-
it('prefers modules', () => {
395+
it('prefers the root child with the same url path prefix', () => {
396+
Object.defineProperty(window, 'location', {
397+
value: { href: 'http://localhost/documentation/b/b42' },
398+
});
399+
392400
// use first root node if only one is provided
393401
let props = extractTechnologyProps({ swift: [a] });
394402
expect(props.swift.technology).toBe(a.title);
395403
props = extractTechnologyProps({ swift: [b] });
396404
expect(props.swift.technology).toBe(b.title);
397405

398-
// prefer "module" root when multiple top-level nodes are provided
406+
// prefers root node with same url path prefix when multiple are provided
399407
props = extractTechnologyProps({ swift: [a, b] });
400408
expect(props.swift.technology).toBe(b.title);
401409

0 commit comments

Comments
 (0)