Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): retrieve extra fields in search sections #3178

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions src/runtime/internal/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ContentNavigationItem, PageCollectionItemBase, CollectionQueryBuilder } from '@nuxt/content'
import { pascalCase } from 'scule'
import { pick } from './utils'

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

/**
* Returns a new object with the specified keys
*/
function pick(keys?: string[]) {
return (obj: Record<string, unknown>) => {
obj = obj || {}
if (keys && keys.length) {
return keys
.filter(key => typeof obj[key] !== 'undefined')
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {})
}
return obj
}
}

function isObject(obj: unknown) {
return obj !== null && Object.prototype.toString.call(obj) === '[object Object]'
}
Expand Down
14 changes: 9 additions & 5 deletions src/runtime/internal/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MDCNode, MDCRoot, MDCElement } from '@nuxtjs/mdc'
import type { MinimalTree } from '@nuxt/content'
import { decompressTree } from './abstract-tree'
import { pick } from './utils'
import type { CollectionQueryBuilder, PageCollectionItemBase } from '~/src/types'

type Section = {
Expand All @@ -26,23 +27,25 @@ interface SectionablePage {
body: MDCRoot
}

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

const documents = await queryBuilder
.where('extension', '=', 'md')
.select('path', 'body', 'description', 'title')
.select('path', 'body', 'description', 'title', ...(extraFields || []))
.all()

return documents.flatMap(doc => splitPageIntoSections(doc, { ignoredTags }))
return documents.flatMap(doc => splitPageIntoSections(doc, { ignoredTags, extraFields: extraFields as string[] }))
}

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

// TODO: title in frontmatter must be added
const sections: Section[] = [{
...extraFieldsData,
id: path,
title: page.title as string || '',
titles: [],
Expand Down Expand Up @@ -79,6 +82,7 @@ function splitPageIntoSections(page: SectionablePage, { ignoredTags }: { ignored
}

sections.push({
...extraFieldsData,
id: `${path}#${(item as MDCElement).props?.id}`,
title,
titles: [...titles],
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Returns a new object with the specified keys
*/
export function pick(keys?: string[]) {
return (obj: Record<string, unknown>) => {
obj = obj || {}
if (keys && keys.length) {
return keys
.filter(key => typeof obj[key] !== 'undefined')
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {})
}
return obj
}
}
Loading