Skip to content

Commit 29f4b3e

Browse files
authoredMar 18, 2025
feat(search): retrieve extra fields in search sections (#3178)
1 parent e7a500f commit 29f4b3e

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed
 

‎src/runtime/internal/navigation.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ContentNavigationItem, PageCollectionItemBase, CollectionQueryBuilder } from '@nuxt/content'
22
import { pascalCase } from 'scule'
3+
import { pick } from './utils'
34

45
/**
56
* Create NavItem array to be consumed from runtime plugin.
@@ -190,21 +191,6 @@ function sortAndClear(nav: ContentNavigationItem[]) {
190191
return nav
191192
}
192193

193-
/**
194-
* Returns a new object with the specified keys
195-
*/
196-
function pick(keys?: string[]) {
197-
return (obj: Record<string, unknown>) => {
198-
obj = obj || {}
199-
if (keys && keys.length) {
200-
return keys
201-
.filter(key => typeof obj[key] !== 'undefined')
202-
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {})
203-
}
204-
return obj
205-
}
206-
}
207-
208194
function isObject(obj: unknown) {
209195
return obj !== null && Object.prototype.toString.call(obj) === '[object Object]'
210196
}

‎src/runtime/internal/search.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { MDCNode, MDCRoot, MDCElement } from '@nuxtjs/mdc'
22
import type { MinimalTree } from '@nuxt/content'
33
import { decompressTree } from './abstract-tree'
4+
import { pick } from './utils'
45
import type { CollectionQueryBuilder, PageCollectionItemBase } from '~/src/types'
56

67
type Section = {
@@ -26,23 +27,25 @@ interface SectionablePage {
2627
body: MDCRoot
2728
}
2829

29-
export async function generateSearchSections<T extends PageCollectionItemBase>(queryBuilder: CollectionQueryBuilder<T>, opts?: { ignoredTags: string[] }) {
30-
const { ignoredTags = [] } = opts || {}
30+
export async function generateSearchSections<T extends PageCollectionItemBase>(queryBuilder: CollectionQueryBuilder<T>, opts?: { ignoredTags?: string[], extraFields?: Array<keyof T> }) {
31+
const { ignoredTags = [], extraFields = [] } = opts || {}
3132

3233
const documents = await queryBuilder
3334
.where('extension', '=', 'md')
34-
.select('path', 'body', 'description', 'title')
35+
.select('path', 'body', 'description', 'title', ...(extraFields || []))
3536
.all()
3637

37-
return documents.flatMap(doc => splitPageIntoSections(doc, { ignoredTags }))
38+
return documents.flatMap(doc => splitPageIntoSections(doc, { ignoredTags, extraFields: extraFields as string[] }))
3839
}
3940

40-
function splitPageIntoSections(page: SectionablePage, { ignoredTags }: { ignoredTags: string[] }) {
41+
function splitPageIntoSections(page: SectionablePage, { ignoredTags, extraFields }: { ignoredTags: string[], extraFields: Array<string> }) {
4142
const body = (!page.body || page.body?.type === 'root') ? page.body : decompressTree(page.body as unknown as MinimalTree)
4243
const path = (page.path ?? '')
44+
const extraFieldsData = pick(extraFields)(page as unknown as Record<string, unknown>)
4345

4446
// TODO: title in frontmatter must be added
4547
const sections: Section[] = [{
48+
...extraFieldsData,
4649
id: path,
4750
title: page.title as string || '',
4851
titles: [],
@@ -79,6 +82,7 @@ function splitPageIntoSections(page: SectionablePage, { ignoredTags }: { ignored
7982
}
8083

8184
sections.push({
85+
...extraFieldsData,
8286
id: `${path}#${(item as MDCElement).props?.id}`,
8387
title,
8488
titles: [...titles],

‎src/runtime/internal/utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Returns a new object with the specified keys
3+
*/
4+
export function pick(keys?: string[]) {
5+
return (obj: Record<string, unknown>) => {
6+
obj = obj || {}
7+
return (keys || [])
8+
.filter(key => typeof obj[key] !== 'undefined')
9+
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {})
10+
}
11+
}

0 commit comments

Comments
 (0)