Skip to content

Commit a966c61

Browse files
authored
feat: add findPageHeadline utils (#3416)
1 parent 63dbf9f commit a966c61

File tree

2 files changed

+116
-12
lines changed

2 files changed

+116
-12
lines changed

src/runtime/utils/index.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export function findPageBreadcrumb(navigation?: ContentNavigationItem[], path?:
2020
}, [])
2121
}
2222

23-
type FindPageChildrenOptions = { indexAsChild?: boolean }
23+
type FindPageOptions = { indexAsChild?: boolean }
2424

25-
export function findPageChildren(navigation?: ContentNavigationItem[], path?: string | undefined | null, options?: FindPageChildrenOptions): ContentNavigationItem[] {
25+
export function findPageChildren(navigation?: ContentNavigationItem[], path?: string | undefined | null, options?: FindPageOptions): ContentNavigationItem[] {
2626
if (!navigation?.length || !path) {
2727
return []
2828
}
@@ -41,7 +41,7 @@ export function findPageChildren(navigation?: ContentNavigationItem[], path?: st
4141
}, [])
4242
}
4343

44-
export function findPageSiblings(navigation?: ContentNavigationItem[], path?: string | undefined | null, options?: FindPageChildrenOptions): ContentNavigationItem[] {
44+
export function findPageSiblings(navigation?: ContentNavigationItem[], path?: string | undefined | null, options?: FindPageOptions): ContentNavigationItem[] {
4545
if (!navigation?.length || !path) {
4646
return []
4747
}
@@ -50,3 +50,39 @@ export function findPageSiblings(navigation?: ContentNavigationItem[], path?: st
5050

5151
return findPageChildren(navigation, parentPath, options).filter(c => c.path !== path)
5252
}
53+
54+
export function findPageHeadline(navigation?: ContentNavigationItem[], path?: string | undefined | null, options?: FindPageOptions): string | undefined {
55+
if (!navigation?.length || !path) {
56+
return
57+
}
58+
59+
for (const link of navigation) {
60+
if (options?.indexAsChild) {
61+
if (link.children) {
62+
const headline = findPageHeadline(link.children, path, options)
63+
if (headline) {
64+
return headline
65+
}
66+
for (const child of link.children) {
67+
if (child.path === path) {
68+
return link.title
69+
}
70+
}
71+
}
72+
}
73+
else {
74+
if (link.children) {
75+
for (const child of link.children) {
76+
const isIndex = child.stem?.endsWith('/index')
77+
if (child.path === path && !isIndex) {
78+
return link.title
79+
}
80+
}
81+
const headline = findPageHeadline(link.children, path, options)
82+
if (headline) {
83+
return headline
84+
}
85+
}
86+
}
87+
}
88+
}

test/unit/utils.test.ts

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest'
2-
import { findPageBreadcrumb, findPageChildren, findPageSiblings } from '@nuxt/content/utils'
2+
import { findPageBreadcrumb, findPageChildren, findPageSiblings, findPageHeadline } from '@nuxt/content/utils'
33

44
describe('utils', () => {
55
const navigation = [
@@ -32,6 +32,48 @@ describe('utils', () => {
3232
},
3333
]
3434

35+
const navigation2 = [
36+
{
37+
title: 'Guide Dir',
38+
path: '/guide',
39+
stem: 'guide',
40+
children: [
41+
{
42+
title: 'Guide Index',
43+
path: '/guide',
44+
stem: 'guide/index',
45+
},
46+
{
47+
title: 'Getting Started Dir',
48+
path: '/guide/getting-started',
49+
stem: 'guide/getting-started',
50+
children: [
51+
{
52+
title: 'Getting Started Index',
53+
path: '/guide/getting-started',
54+
stem: 'guide/getting-started/index',
55+
},
56+
{
57+
title: 'Getting Started 1',
58+
path: '/guide/getting-started/1',
59+
stem: 'guide/getting-started/1',
60+
},
61+
{
62+
title: 'Getting Started 2',
63+
path: '/guide/getting-started/2',
64+
stem: 'guide/getting-started/2',
65+
},
66+
],
67+
},
68+
{
69+
title: 'Introduction',
70+
path: '/guide/introduction',
71+
stem: 'guide/introduction',
72+
},
73+
],
74+
},
75+
]
76+
3577
function removeChildren(array) {
3678
return array.map((obj) => {
3779
const { children, ...rest } = obj
@@ -116,9 +158,9 @@ describe('utils', () => {
116158
})
117159

118160
it('findPageChildren', async () => {
119-
const breadcrumb = removeChildren(findPageChildren(navigation, '/guide'))
161+
const pages = removeChildren(findPageChildren(navigation, '/guide'))
120162

121-
expect(breadcrumb).toEqual([
163+
expect(pages).toEqual([
122164
{
123165
title: 'Getting Started',
124166
path: '/guide/getting-started',
@@ -133,9 +175,9 @@ describe('utils', () => {
133175
})
134176

135177
it('findPageChildren with indexAsChild option', async () => {
136-
const breadcrumb = removeChildren(findPageChildren(navigation, '/guide', { indexAsChild: true }))
178+
const pages = removeChildren(findPageChildren(navigation, '/guide', { indexAsChild: true }))
137179

138-
expect(breadcrumb).toEqual([
180+
expect(pages).toEqual([
139181
{
140182
title: 'Guide Index',
141183
path: '/guide',
@@ -155,9 +197,9 @@ describe('utils', () => {
155197
})
156198

157199
it('findPageSiblings', async () => {
158-
const breadcrumb = removeChildren(findPageSiblings(navigation, '/guide/getting-started'))
200+
const pages = removeChildren(findPageSiblings(navigation, '/guide/getting-started'))
159201

160-
expect(breadcrumb).toEqual([
202+
expect(pages).toEqual([
161203
{
162204
title: 'Introduction',
163205
path: '/guide/introduction',
@@ -167,9 +209,9 @@ describe('utils', () => {
167209
})
168210

169211
it('findPageSiblings with indexAsChild option', async () => {
170-
const breadcrumb = removeChildren(findPageSiblings(navigation, '/guide/getting-started', { indexAsChild: true }))
212+
const pages = removeChildren(findPageSiblings(navigation, '/guide/getting-started', { indexAsChild: true }))
171213

172-
expect(breadcrumb).toEqual([
214+
expect(pages).toEqual([
173215
{
174216
title: 'Guide Index',
175217
path: '/guide',
@@ -182,4 +224,30 @@ describe('utils', () => {
182224
},
183225
])
184226
})
227+
228+
it('findPageHeadline', async () => {
229+
const headline = findPageHeadline(navigation2, '/guide/getting-started')
230+
231+
expect(headline).toEqual('Guide Dir')
232+
})
233+
234+
it('findPageHeadline for index with indexAsChild', async () => {
235+
const headline = findPageHeadline(navigation2, '/guide', { indexAsChild: true })
236+
237+
expect(headline).toEqual('Guide Dir')
238+
239+
const headline2 = findPageHeadline(navigation2, '/guide/getting-started', { indexAsChild: true })
240+
241+
expect(headline2).toEqual('Getting Started Dir')
242+
})
243+
244+
it('findPageHeadline for index without indexAsChild', async () => {
245+
const headline = findPageHeadline(navigation2, '/guide')
246+
247+
expect(headline).toEqual(undefined)
248+
249+
const headline2 = findPageHeadline(navigation2, '/guide/getting-started')
250+
251+
expect(headline2).toEqual('Guide Dir')
252+
})
185253
})

0 commit comments

Comments
 (0)