Skip to content

Commit 27d1552

Browse files
authored
fix: Search was not working (#117)
The computed URL was incorrect. This broke in d34bd4f (april 3) which changed the file structure without updating the search code.
1 parent 9e44c7d commit 27d1552

File tree

3 files changed

+118
-157
lines changed

3 files changed

+118
-157
lines changed

components/search.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Anchor.displayName = 'Anchor';
2828
interface SearchDocument {
2929
id: string;
3030
title: string;
31-
folderName: string;
3231
content: string;
3332
url: string;
3433
headings: {
@@ -87,15 +86,13 @@ export default function Search() {
8786
lunrIndex = lunr(b => {
8887
b.ref('id');
8988
b.field('title', {boost: 10}); // Prioritize document title
90-
b.field('folderName', {boost: 8}); // Prioritize folder name
9189
b.field('content');
9290
b.field('headings', {boost: 9}); // Prioritize headings
9391

9492
for (const doc of searchDocs) {
9593
b.add({
9694
...doc,
9795
title: doc.title?.toLowerCase() ?? '',
98-
folderName: doc.folderName?.toLowerCase() ?? '', // Ensure folder name is indexed
9996
headings: doc.headings.map(h => h.text.toLowerCase()).join(' '), // Convert headings to searchable string
10097
});
10198
}
@@ -186,9 +183,7 @@ export default function Search() {
186183

187184
// Add an extra result if the search term exactly matches a document title
188185
const exactMatch = searchDocs.find(
189-
doc =>
190-
doc.title.toLowerCase() === sanitizedInput.toLowerCase() ||
191-
doc.folderName.toLowerCase() === sanitizedInput.toLowerCase(),
186+
doc => doc.title.toLowerCase() === sanitizedInput.toLowerCase(),
192187
);
193188

194189
if (exactMatch) {

lib/generateSearchIndex.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const OUTPUT_FILE = path.join(process.cwd(), 'public', 'search-index.json');
1414
interface SearchDocument {
1515
id: string;
1616
title: string;
17-
folderName: string;
1817
content: string;
1918
url: string;
2019
headings: {
@@ -87,17 +86,17 @@ async function extractTextFromMDX(filePath: string): Promise<SearchDocument> {
8786
// Extract headings with IDs
8887
const headings = extractHeadings(content);
8988

90-
// Derive a URL from the folder structure
91-
const relativePath = path.relative(DOCS_ROOT, path.dirname(filePath)); // Get folder name
92-
const url = `/docs/${relativePath}`; // Generate URL based on the folder structure
93-
const folderName = relativePath.split('/').pop() || relativePath; // Extract last folder name
89+
// Derive a URL from the file name
90+
const pathWithoutExtension = path
91+
.relative(DOCS_ROOT, filePath)
92+
.replace(/\.mdx$/, '');
93+
const url = `/docs/${pathWithoutExtension}`;
9494

9595
return {
96-
id: relativePath, // Use folder name as ID
97-
title: data.title || relativePath, // Use frontmatter title or fallback to folder name
98-
folderName, // Add folder name as a searchable field
99-
content: plainText.toString().replace(/\n+/g, ' ').trim(), // Normalize whitespace
96+
id: pathWithoutExtension, // Use file name as ID
97+
title: data.title || pathWithoutExtension, // Use frontmatter title or fallback to path
10098
url,
99+
content: plainText.toString().replace(/\n+/g, ' ').trim(),
101100
headings, // Include extracted headings with IDs
102101
};
103102
}

0 commit comments

Comments
 (0)