@mintlify/common
Version:
Commonly shared code within Mintlify
41 lines (40 loc) • 1.98 kB
JavaScript
import { divisions } from '@mintlify/validation';
import { isPage } from '../navigation/isPage.js';
import { optionallyRemoveLeadingSlash } from '../optionallyRemoveLeadingSlash.js';
import { iterateObjectArrayProperty } from './iterateObjectArrayProperty.js';
/**
* Generates a map from page paths to their search boost multiplier based on navigation hierarchy.
* A page inherits its boost factor from itself or any parent group/division that sets `boost`.
* The map only contains entries for pages with a boost factor > 1; unboosted pages are omitted.
* Assumes page hrefs in navWithPageContext have a leading / but config page paths do not.
* Outputted dictionary will NOT have a leading / in the dictionary keys.
*/
export function generatePathToBoostDict(decoratedNav) {
const pathToBoostDict = new Map();
generatePathToBoostDictRecursive(pathToBoostDict, decoratedNav, 1);
return pathToBoostDict;
}
function generatePathToBoostDictRecursive(pathToBoostDict, nav, parentBoost) {
if (typeof nav !== 'object')
return;
const effectiveBoost = 'boost' in nav && typeof nav.boost === 'number' ? nav.boost : parentBoost;
if (isPage(nav)) {
const key = optionallyRemoveLeadingSlash(nav.href);
if (!pathToBoostDict.has(key) && effectiveBoost !== 1) {
pathToBoostDict.set(key, effectiveBoost);
}
}
if ('pages' in nav && Array.isArray(nav.pages)) {
if ('root' in nav && typeof nav.root === 'object') {
generatePathToBoostDictRecursive(pathToBoostDict, nav.root, effectiveBoost);
}
for (const page of nav.pages) {
if (typeof page === 'object') {
generatePathToBoostDictRecursive(pathToBoostDict, page, effectiveBoost);
}
}
}
for (const key of ['groups', ...divisions]) {
iterateObjectArrayProperty(nav, key, (item) => generatePathToBoostDictRecursive(pathToBoostDict, item, effectiveBoost));
}
}