Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/utils/navigatorData.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,21 @@ export function getSiblings(uid, childrenMap, children) {
}

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

Expand Down
16 changes: 12 additions & 4 deletions tests/unit/utils/navigatorData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,11 @@ describe('when multiple top-level children are provided', () => {
};

describe('flattenNavigationIndex', () => {
it('prefers modules', () => {
it('prefers the root child with the same url path prefix', () => {
Object.defineProperty(window, 'location', {
value: { href: 'http://localhost/documentation/b/b42' },
});

// use first root node if only one is provided
let flattenedIndex = flattenNavigationIndex({ swift: [a] });
expect(flattenedIndex.swift.length).toBe(1);
Expand All @@ -368,7 +372,7 @@ describe('when multiple top-level children are provided', () => {
expect(flattenedIndex.swift.length).toBe(1);
expect(flattenedIndex.swift[0].title).toBe(b.children[0].title);

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

describe('extractTechnologyProps', () => {
it('prefers modules', () => {
it('prefers the root child with the same url path prefix', () => {
Object.defineProperty(window, 'location', {
value: { href: 'http://localhost/documentation/b/b42' },
});

// use first root node if only one is provided
let props = extractTechnologyProps({ swift: [a] });
expect(props.swift.technology).toBe(a.title);
props = extractTechnologyProps({ swift: [b] });
expect(props.swift.technology).toBe(b.title);

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

Expand Down