Skip to content

Commit

Permalink
fix(search): improve sections with root node (#2672)
Browse files Browse the repository at this point in the history
Co-authored-by: Farnabaz <2047945+farnabaz@users.noreply.github.com>
  • Loading branch information
benjamincanac and farnabaz committed Jun 20, 2024
1 parent d6463f3 commit e33de6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export default defineNuxtModule<ModuleOptions>({
if (options.experimental?.search) {
const defaultSearchOptions: Partial<ModuleOptions['experimental']['search']> = {
indexed: true,
ignoredTags: ['style', 'code'],
ignoredTags: ['script', 'style', 'pre'],
filterQuery: { _draft: false, _partial: false },
options: {
fields: ['title', 'content', 'titles'],
Expand Down Expand Up @@ -774,4 +774,4 @@ declare module 'nitropack' {
'content:file:beforeParse': (file: { _id: string; body: string }) => void;
'content:file:afterParse': (file: ParsedContent) => void;
}
}
}
34 changes: 17 additions & 17 deletions src/runtime/server/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ export function splitPageIntoSections (page: ParsedContent, { ignoredTags }: { i
const path = page._path ?? ''

// TODO: title in frontmatter must be added
const sections: Section[] = []
const sections: Section[] = [{
id: path,
title: page.title || '',
titles: [],
content: page.description.trim() || '',
level: 1
}]

if (!page?.body?.children) {
return sections
}

// No section
let section = 0
let section = 1
let previousHeadingLevel = 0
const titles = []
const titles = [page.title ?? '']
for (const item of page.body.children) {
const tag = item.tag || ''
if (isHeading(tag)) {
Expand Down Expand Up @@ -71,20 +77,14 @@ export function splitPageIntoSections (page: ParsedContent, { ignoredTags }: { i
// Swap to a new section
previousHeadingLevel = currentHeadingLevel
section += 1
}
} else {
const content = extractTextFromAst(item, ignoredTags).trim()

if (!isHeading(tag)) {
if (!sections[section]) {
sections[section] = {
id: path,
title: page.title || '',
titles: [],
content: '',
level: 1
}
if (section === 1 && sections[section - 1]?.content === content) {
continue
}

sections[section]!.content += extractTextFromAst(item, ignoredTags).trim()
sections[section - 1]!.content = `${sections[section - 1]!.content} ${content}`.trim()
}
}

Expand All @@ -97,7 +97,7 @@ function extractTextFromAst (node: MarkdownNode, ignoredTags: string[] = []) {

// Get text from markdown AST
if (node.type === 'text') {
text += (node.value || '').trim()
text += (node.value || '')
}

// Do not explore children
Expand All @@ -106,8 +106,8 @@ function extractTextFromAst (node: MarkdownNode, ignoredTags: string[] = []) {
}

// Explore children
if (node.children) {
text += node.children.map(child => extractTextFromAst(child, ignoredTags)).filter(Boolean).join(' ')
if (node.children?.length) {
text += node.children.map((child: any) => extractTextFromAst(child, ignoredTags)).filter(Boolean).join('')
}

return text
Expand Down
4 changes: 2 additions & 2 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export interface ModuleOptions {
*
* By default, will extract text from each tag.
*
* @default ['style', 'code']
* @default ['script', 'style', 'pre']
*/
ignoredTags?: Array<string>
/**
Expand Down Expand Up @@ -289,4 +289,4 @@ export interface ModuleOptions {
export interface ContentContext extends ModuleOptions {
base: Readonly<string>
transformers: Array<string>
}
}

0 comments on commit e33de6a

Please sign in to comment.