Skip to content
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
47 changes: 47 additions & 0 deletions src/hooks/__tests__/use-search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {flattenHeadings} from '../use-search'

describe('flattenHeadings', () => {
test('returns empty array for null/undefined input', () => {
expect(flattenHeadings(null)).toEqual([])
expect(flattenHeadings(undefined)).toEqual([])
})

test('returns empty array for empty items', () => {
expect(flattenHeadings([])).toEqual([])
})

test('flattens single level of headings', () => {
const items = [{title: 'Description'}, {title: 'Configuration'}]
expect(flattenHeadings(items)).toEqual(['Description', 'Configuration'])
})

test('flattens nested headings', () => {
const items = [
{
title: 'Description',
items: [{title: 'before'}, {title: 'min-release-age'}],
},
]
expect(flattenHeadings(items)).toEqual(['Description', 'before', 'min-release-age'])
})

test('flattens deeply nested headings', () => {
const items = [
{
title: 'Top',
items: [
{
title: 'Mid',
items: [{title: 'Deep'}],
},
],
},
]
expect(flattenHeadings(items)).toEqual(['Top', 'Mid', 'Deep'])
})

test('skips items without a title', () => {
const items = [{items: [{title: 'child'}]}, {title: 'sibling'}]
expect(flattenHeadings(items)).toEqual(['child', 'sibling'])
})
})
11 changes: 11 additions & 0 deletions src/hooks/use-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import usePage from './use-page'
import * as getNav from '../util/get-nav'
import {CLI_PATH} from '../constants'

export const flattenHeadings = items => {
if (!items) return []
return items.reduce((acc, item) => {
if (item.title) acc.push(item.title)
if (item.items) acc.push(...flattenHeadings(item.items))
return acc
}, [])
}

const useSearchData = () => {
const data = useStaticQuery(graphql`
{
Expand All @@ -15,6 +24,7 @@ const useSearchData = () => {
frontmatter {
title
}
tableOfContents
body
}
}
Expand All @@ -40,6 +50,7 @@ const useSearchData = () => {
return {
path: node.path,
title: mdxNode.frontmatter.title,
headings: flattenHeadings(mdxNode.tableOfContents?.items).join(' '),
body: mdxNode.body,
}
})
Expand Down
9 changes: 8 additions & 1 deletion src/util/search.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ class Fuse {
const items = this.allItems.filter(item =>
item.path.startsWith(this.cliVersion.root) ? item.path.startsWith(this.cliVersion.current) : true,
)
this.instances.set(this.cliVersion.current, new FuseJs(items, {threshold: 0.2, keys: ['title', 'body']}))
this.instances.set(
this.cliVersion.current,
new FuseJs(items, {
threshold: 0.2,
ignoreLocation: true,
keys: ['title', 'headings', 'body'],
}),
)
}

setItems(items) {
Expand Down
Loading