1
1
import type { MDCNode , MDCRoot , MDCElement } from '@nuxtjs/mdc'
2
2
import type { MinimalTree } from '@nuxt/content'
3
3
import { decompressTree } from './abstract-tree'
4
+ import { pick } from './utils'
4
5
import type { CollectionQueryBuilder , PageCollectionItemBase } from '~/src/types'
5
6
6
7
type Section = {
@@ -26,23 +27,25 @@ interface SectionablePage {
26
27
body : MDCRoot
27
28
}
28
29
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 || { }
31
32
32
33
const documents = await queryBuilder
33
34
. where ( 'extension' , '=' , 'md' )
34
- . select ( 'path' , 'body' , 'description' , 'title' )
35
+ . select ( 'path' , 'body' , 'description' , 'title' , ... ( extraFields || [ ] ) )
35
36
. all ( )
36
37
37
- return documents . flatMap ( doc => splitPageIntoSections ( doc , { ignoredTags } ) )
38
+ return documents . flatMap ( doc => splitPageIntoSections ( doc , { ignoredTags, extraFields : extraFields as string [ ] } ) )
38
39
}
39
40
40
- function splitPageIntoSections ( page : SectionablePage , { ignoredTags } : { ignoredTags : string [ ] } ) {
41
+ function splitPageIntoSections ( page : SectionablePage , { ignoredTags, extraFields } : { ignoredTags : string [ ] , extraFields : Array < string > } ) {
41
42
const body = ( ! page . body || page . body ?. type === 'root' ) ? page . body : decompressTree ( page . body as unknown as MinimalTree )
42
43
const path = ( page . path ?? '' )
44
+ const extraFieldsData = pick ( extraFields ) ( page as unknown as Record < string , unknown > )
43
45
44
46
// TODO: title in frontmatter must be added
45
47
const sections : Section [ ] = [ {
48
+ ...extraFieldsData ,
46
49
id : path ,
47
50
title : page . title as string || '' ,
48
51
titles : [ ] ,
@@ -79,6 +82,7 @@ function splitPageIntoSections(page: SectionablePage, { ignoredTags }: { ignored
79
82
}
80
83
81
84
sections . push ( {
85
+ ...extraFieldsData ,
82
86
id : `${ path } #${ ( item as MDCElement ) . props ?. id } ` ,
83
87
title,
84
88
titles : [ ...titles ] ,
0 commit comments