-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Implement Lunr-based search for Markdown documentation and rend… #150
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| const lunr = require("lunr"); | ||
| const fs = require("node:fs"); | ||
| const path = require("node:path"); | ||
|
|
||
| // Helper function to recursively get all markdown files | ||
| function getAllMarkdownFiles(dirPath, arrayOfFiles) { | ||
| const files = fs.readdirSync(dirPath); | ||
| arrayOfFiles = arrayOfFiles || []; | ||
|
|
||
| files.forEach(function (file) { | ||
| const filePath = path.join(dirPath, file); | ||
| const stats = fs.statSync(filePath); | ||
|
|
||
| if (stats.isDirectory()) { | ||
| // If it's a directory, recurse into it | ||
| arrayOfFiles = getAllMarkdownFiles(filePath, arrayOfFiles); | ||
| } else if (filePath.endsWith(".mdx") || filePath.endsWith(".md")) { | ||
| // If it's a markdown file, add it to the array | ||
| arrayOfFiles.push(filePath); | ||
| } | ||
| }); | ||
|
|
||
| return arrayOfFiles; | ||
| } | ||
|
|
||
| // Custom function to extract metadata | ||
| function extractMetadata(fileContent) { | ||
| const metadataRegex = /export const metadata = ({[\s\S]*?});/; | ||
| const match = fileContent.match(metadataRegex); | ||
|
|
||
| if (match) { | ||
| try { | ||
| const metadata = eval("(" + match[1] + ")"); | ||
| return metadata; | ||
| } catch (err) { | ||
| console.error("Error parsing metadata:", err); | ||
| } | ||
| } | ||
|
|
||
| return {}; // Return empty object if no metadata is found | ||
| } | ||
|
|
||
| function createIndex() { | ||
| const docsDirectory = path.join(process.cwd(), "pages", "docs"); | ||
|
|
||
| // Get all markdown files, including those in subdirectories | ||
| const markdownFiles = getAllMarkdownFiles(docsDirectory); | ||
|
|
||
| // Store documents separately | ||
| const documents = []; | ||
|
|
||
| const idx = lunr(function () { | ||
| this.ref("id"); | ||
| this.field("title"); | ||
| this.field("body"); | ||
|
|
||
| markdownFiles.forEach((filePath, id) => { | ||
| const fileContent = fs.readFileSync(filePath, "utf-8"); | ||
| const metadata = extractMetadata(fileContent); | ||
| const content = fileContent | ||
| .replace(/export const metadata = ({[\s\S]*?});/, "") | ||
| .trim(); // Remove metadata section | ||
|
|
||
| // Remove process root and "pages" from the file path | ||
| let relativePath = filePath.replace(process.cwd(), ""); | ||
|
|
||
| // Ensure we only replace the first occurrence of "/pages/docs" | ||
| relativePath = relativePath.replace("/pages/docs", "/docs"); | ||
|
|
||
| // Remove the file extension | ||
| relativePath = relativePath.replace(/\.mdx?$/, ""); | ||
|
|
||
| // Remove trailing '/index' if present for clean URLs | ||
| relativePath = relativePath.replace(/\/index$/, ""); | ||
|
|
||
| // Construct the document object | ||
| const doc = { | ||
| id: id.toString(), | ||
| title: metadata.title || path.basename(filePath), | ||
| body: content, | ||
| path: relativePath, // Corrected path for Next.js | ||
| }; | ||
|
|
||
| documents.push(doc); // Add document to the store | ||
| this.add(doc); // Add document to the index | ||
| }); | ||
| }); | ||
|
|
||
| return { idx, documents }; | ||
| } | ||
|
|
||
| module.exports = { createIndex }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| const { withFaust, getWpHostname } = require("@faustwp/core"); | ||
| const { createSecureHeaders } = require("next-secure-headers"); | ||
| const withMDX = require("@next/mdx")(); | ||
| const withMDX = require("@next/mdx")({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this is needed. But can you articulate why? I'm guessing its unrelated to the specifics of the PR. Maybe we should separate this out. |
||
| extension: /\.mdx?$/, | ||
| options: { | ||
| providerImportSource: "@mdx-js/react", // Enables MDX context components | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * @type {import('next').NextConfig} | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will still need out search to cover blog posts, yeah? Should we setup search to do both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, we could or should we setup Smart Search for the WP side of things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can decide this after our meeting tomorrow.