UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

62 lines (61 loc) 2.46 kB
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 whether they are hidden from search, * sitemap, AI assistant, and llms.txt indexing. Mirrors generatePathToHiddenDict * for nav purposes, but a node with `hidden: true, searchable: true` opts its * descendants back into indexing — they remain hidden in the rendered nav. * * Semantic: * - Walking down the tree, an "indexable" chain is broken by `hidden: true` * unless the same node also has `searchable: true`, in which case the * chain is restored from that point down. * - `searchable` on a non-hidden node has no effect. * - A page's own frontmatter `hidden: true` is always honored. * - Output keys have no leading /. */ export function generatePathToHiddenFromSearchDict(decoratedNav) { const dict = new Map(); recurse(dict, decoratedNav, false); return dict; } function recurse(dict, nav, ancestorHiddenAndNotOptedIn) { var _a; if (typeof nav !== 'object') return; const hasExplicitHidden = 'hidden' in nav && typeof nav.hidden === 'boolean'; const ownHidden = hasExplicitHidden && nav.hidden === true; const ownSearchable = 'searchable' in nav && nav.searchable === true; let effectiveHidden; if (ownHidden) { effectiveHidden = !ownSearchable; } else if (hasExplicitHidden) { effectiveHidden = false; } else { effectiveHidden = ancestorHiddenAndNotOptedIn; } if (isPage(nav)) { const key = optionallyRemoveLeadingSlash(nav.href); const pageHidden = (_a = nav.hidden) !== null && _a !== void 0 ? _a : effectiveHidden; if (pageHidden && !dict.has(key)) { dict.set(key, true); } } if ('pages' in nav && Array.isArray(nav.pages)) { if ('root' in nav && typeof nav.root === 'object') { recurse(dict, nav.root, effectiveHidden); } for (const page of nav.pages) { if (typeof page === 'object') { recurse(dict, page, effectiveHidden); } } } for (const key of ['groups', ...divisions]) { iterateObjectArrayProperty(nav, key, (item) => recurse(dict, item, effectiveHidden)); } }