@mintlify/common 1.0.1153 → 1.0.1154

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import type { DecoratedDivisionConfig, DecoratedGroupConfig, DecoratedNavigationConfig, DecoratedPageConfig } from '@mintlify/validation';
2
+ export type DecoratedNavNode = DecoratedNavigationConfig | DecoratedDivisionConfig | DecoratedGroupConfig | DecoratedPageConfig;
3
+ export type DecoratedNavPageEntry = {
4
+ title: string;
5
+ mode?: string;
6
+ };
7
+ export declare function decoratedNavDivisions(node: DecoratedNavNode): DecoratedDivisionConfig[];
8
+ export declare function decoratedNavChildren(node: DecoratedNavNode): DecoratedNavNode[];
9
+ export declare const decoratedNavContains: (node: DecoratedNavNode, path: string) => boolean;
10
+ export declare const findDecoratedNavPage: (node: DecoratedNavNode, path: string) => DecoratedNavPageEntry | null;
11
+ export declare function decoratedNavSections(nav: DecoratedNavNode, path: string): DecoratedNavNode[];
@@ -0,0 +1,72 @@
1
+ import { replaceSlashIndex } from '../slug/replaceSlashIndex.js';
2
+ import { isPage } from './isPage.js';
3
+ const normalizeNavPath = (p) => replaceSlashIndex(p.replace(/^\/+/, '').replace(/\/+$/, ''));
4
+ const hrefOf = (node) => isPage(node) ? node.href : 'href' in node ? node.href : undefined;
5
+ export function decoratedNavDivisions(node) {
6
+ if (isPage(node))
7
+ return [];
8
+ if ('languages' in node)
9
+ return node.languages;
10
+ if ('versions' in node)
11
+ return node.versions;
12
+ if ('products' in node)
13
+ return node.products;
14
+ if ('tabs' in node)
15
+ return node.tabs;
16
+ if ('anchors' in node)
17
+ return node.anchors;
18
+ if ('dropdowns' in node)
19
+ return node.dropdowns;
20
+ return [];
21
+ }
22
+ export function decoratedNavChildren(node) {
23
+ if (isPage(node))
24
+ return [];
25
+ if ('group' in node)
26
+ return node.root ? [node.root, ...node.pages] : node.pages;
27
+ if ('pages' in node)
28
+ return node.pages;
29
+ if ('groups' in node)
30
+ return node.groups;
31
+ if ('menu' in node)
32
+ return node.menu;
33
+ return [];
34
+ }
35
+ const matches = (node, path) => {
36
+ const href = hrefOf(node);
37
+ return href !== undefined && normalizeNavPath(href) === path;
38
+ };
39
+ function contains(node, path) {
40
+ if (matches(node, path))
41
+ return true;
42
+ return (decoratedNavChildren(node).some((c) => contains(c, path)) ||
43
+ decoratedNavDivisions(node).some((c) => contains(c, path)));
44
+ }
45
+ function findPage(node, path) {
46
+ if (isPage(node))
47
+ return matches(node, path) ? { title: node.title, mode: node.mode } : null;
48
+ for (const child of [...decoratedNavChildren(node), ...decoratedNavDivisions(node)]) {
49
+ const found = findPage(child, path);
50
+ if (found)
51
+ return found;
52
+ }
53
+ return null;
54
+ }
55
+ export const decoratedNavContains = (node, path) => contains(node, normalizeNavPath(path));
56
+ export const findDecoratedNavPage = (node, path) => findPage(node, normalizeNavPath(path));
57
+ // the nodes whose own pages (not a nested division's) hold the path: the
58
+ // root for a flat nav, else the innermost division chain down to the page
59
+ export function decoratedNavSections(nav, path) {
60
+ const target = normalizeNavPath(path);
61
+ const out = [];
62
+ const walk = (node) => {
63
+ for (const division of decoratedNavDivisions(node)) {
64
+ if (contains(division, target))
65
+ walk(division);
66
+ }
67
+ if (decoratedNavChildren(node).some((c) => contains(c, target)))
68
+ out.push(node);
69
+ };
70
+ walk(nav);
71
+ return out;
72
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,51 @@
1
+ import { decoratedNavContains, decoratedNavSections, findDecoratedNavPage, } from './decoratedNavTree.js';
2
+ const nav = {
3
+ tabs: [
4
+ {
5
+ tab: 'Guides',
6
+ groups: [
7
+ { group: 'Start', pages: [{ href: '/guides/intro', title: 'Intro' }] },
8
+ { group: 'More', pages: [{ href: '/guides/deploy', title: 'Deploy', mode: 'custom' }] },
9
+ ],
10
+ },
11
+ { tab: 'API', pages: [{ href: '/api/overview', title: 'Overview' }] },
12
+ ],
13
+ };
14
+ describe('decoratedNavContains', () => {
15
+ it('finds pages through divisions and groups, ignoring slashes and /index', () => {
16
+ expect(decoratedNavContains(nav, '/guides/deploy')).toBe(true);
17
+ expect(decoratedNavContains(nav, 'guides/deploy/')).toBe(true);
18
+ expect(decoratedNavContains(nav, '/api/overview/index')).toBe(true);
19
+ expect(decoratedNavContains(nav, '/nowhere')).toBe(false);
20
+ });
21
+ });
22
+ describe('findDecoratedNavPage', () => {
23
+ it('returns the page title and mode', () => {
24
+ expect(findDecoratedNavPage(nav, '/guides/deploy')).toEqual({
25
+ title: 'Deploy',
26
+ mode: 'custom',
27
+ });
28
+ expect(findDecoratedNavPage(nav, '/api/overview')).toEqual({
29
+ title: 'Overview',
30
+ mode: undefined,
31
+ });
32
+ expect(findDecoratedNavPage(nav, '/nowhere')).toBeNull();
33
+ });
34
+ });
35
+ describe('decoratedNavSections', () => {
36
+ it('returns the division whose own pages hold the path', () => {
37
+ const sections = decoratedNavSections(nav, '/guides/intro');
38
+ expect(sections).toHaveLength(1);
39
+ expect(sections[0]).toBe(nav.tabs[0]);
40
+ expect(decoratedNavSections(nav, '/nowhere')).toEqual([]);
41
+ });
42
+ it('returns the root for a flat nav', () => {
43
+ const flat = {
44
+ pages: [
45
+ { href: '/a', title: 'A' },
46
+ { href: '/b', title: 'B' },
47
+ ],
48
+ };
49
+ expect(decoratedNavSections(flat, '/a')).toEqual([flat]);
50
+ });
51
+ });
@@ -4,6 +4,7 @@ export * from './generatePathToBreadcrumbsMap.js';
4
4
  export * from './getFirstPageFromNavigation.js';
5
5
  export * from './generatePathToBreadcrumbsMapForDocsConfig.js';
6
6
  export * from './getAllPathsInDocsNav.js';
7
+ export * from './decoratedNavTree.js';
7
8
  export * from './getAllPathsInDecoratedNav.js';
8
9
  export * from './checkNavAccess.js';
9
10
  export * from './filterDivisions.js';
@@ -4,6 +4,7 @@ export * from './generatePathToBreadcrumbsMap.js';
4
4
  export * from './getFirstPageFromNavigation.js';
5
5
  export * from './generatePathToBreadcrumbsMapForDocsConfig.js';
6
6
  export * from './getAllPathsInDocsNav.js';
7
+ export * from './decoratedNavTree.js';
7
8
  export * from './getAllPathsInDecoratedNav.js';
8
9
  export * from './checkNavAccess.js';
9
10
  export * from './filterDivisions.js';