-
Notifications
You must be signed in to change notification settings - Fork 1
feat: full-text search with FlexSearch #102
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
Open
s-adamantine
wants to merge
9
commits into
main
Choose a base branch
from
hypercerts-atproto-documentation-7fu.1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6417d68
Add build-time search index generator (hypercerts-atproto-documentati…
s-adamantine 5cff476
beads: close hypercerts-atproto-documentation-7fu.1
s-adamantine 7cb8d0e
Replace SearchDialog with FlexSearch-powered full-text search (hyperc…
s-adamantine 4fcd51b
beads: close hypercerts-atproto-documentation-7fu.2
s-adamantine 9f49190
Add CSS for search result snippets and mark highlighting (hypercerts-…
s-adamantine 882a76e
beads: close hypercerts-atproto-documentation-7fu.3
s-adamantine f1b54b2
Update lastUpdated.json from build
s-adamantine 1ed7c96
fix: join headings array to string before FlexSearch indexing
s-adamantine 5b90aa9
fix: FlexSearch results are plain IDs not objects, handle both formats
s-adamantine 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
Large diffs are not rendered by default.
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
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,139 @@ | ||
| const { readdirSync, statSync, readFileSync, writeFileSync } = require("fs"); | ||
| const { join, relative } = require("path"); | ||
|
|
||
| const PAGES_DIR = join(__dirname, "..", "pages"); | ||
| const OUTPUT = join(__dirname, "..", "public", "search-index.json"); | ||
| const MAX_BODY_LENGTH = 5000; | ||
|
|
||
| function walkDir(dir) { | ||
| const results = []; | ||
| for (const entry of readdirSync(dir)) { | ||
| const full = join(dir, entry); | ||
| if (statSync(full).isDirectory()) { | ||
| results.push(...walkDir(full)); | ||
| } else if (full.endsWith(".md")) { | ||
| results.push(full); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| function extractFrontmatter(content) { | ||
| const fmMatch = content.match(/^---\n([\s\S]*?)\n---/); | ||
| if (!fmMatch) return { title: "", description: "" }; | ||
|
|
||
| const frontmatter = fmMatch[1]; | ||
| const titleMatch = frontmatter.match(/^title:\s*(.+)$/m); | ||
| const descMatch = frontmatter.match(/^description:\s*(.+)$/m); | ||
|
|
||
| return { | ||
| title: titleMatch ? titleMatch[1].trim() : "", | ||
| description: descMatch ? descMatch[1].trim() : "", | ||
| }; | ||
| } | ||
|
|
||
| function extractHeadings(content) { | ||
| const headings = []; | ||
| const lines = content.split("\n"); | ||
|
|
||
| for (const line of lines) { | ||
| // Match h2 (## ) or h3 (### ) | ||
| const h2Match = line.match(/^##\s+(.+)$/); | ||
| const h3Match = line.match(/^###\s+(.+)$/); | ||
|
|
||
| if (h2Match) { | ||
| headings.push(h2Match[1].trim()); | ||
| } else if (h3Match) { | ||
| headings.push(h3Match[1].trim()); | ||
| } | ||
| } | ||
|
|
||
| return headings; | ||
| } | ||
|
|
||
| function stripMarkdown(content) { | ||
| let text = content; | ||
|
|
||
| // Remove frontmatter | ||
| text = text.replace(/^---\n[\s\S]*?\n---\n?/, ""); | ||
|
|
||
| // Remove code blocks | ||
| text = text.replace(/```[\s\S]*?```/g, ""); | ||
|
|
||
| // Remove Markdoc tags ({% ... %} and {% /... %}) | ||
| text = text.replace(/\{%[\s\S]*?%\}/g, ""); | ||
|
|
||
| // Remove HTML tags | ||
| text = text.replace(/<[^>]+>/g, ""); | ||
|
|
||
| // Remove heading markers | ||
| text = text.replace(/^#{1,6}\s+/gm, ""); | ||
|
|
||
| // Remove markdown links [text](url) -> text | ||
| text = text.replace(/\[([^\]]+)\]\([^\)]+\)/g, "$1"); | ||
|
|
||
| // Remove bold/italic markers | ||
| text = text.replace(/\*\*([^*]+)\*\*/g, "$1"); | ||
| text = text.replace(/\*([^*]+)\*/g, "$1"); | ||
| text = text.replace(/__([^_]+)__/g, "$1"); | ||
| text = text.replace(/_([^_]+)_/g, "$1"); | ||
|
|
||
| // Remove inline code backticks | ||
| text = text.replace(/`([^`]+)`/g, "$1"); | ||
|
|
||
| // Collapse whitespace | ||
| text = text.replace(/\s+/g, " "); | ||
|
|
||
| return text.trim(); | ||
| } | ||
|
|
||
| function getSection(path) { | ||
| if (path === "/") return "Get Started"; | ||
| if (path.startsWith("/getting-started")) return "Get Started"; | ||
| if (path.startsWith("/core-concepts")) return "Core Concepts"; | ||
| if (path.startsWith("/tools")) return "Tools"; | ||
| if (path.startsWith("/architecture")) return "Architecture"; | ||
| if (path.startsWith("/lexicons")) return "Reference"; | ||
| if (path.startsWith("/reference")) return "Reference"; | ||
| if (path.startsWith("/ecosystem")) return "Ecosystem & Vision"; | ||
| if (path === "/roadmap") return "Reference"; | ||
| return "Other"; | ||
| } | ||
|
|
||
| const files = walkDir(PAGES_DIR); | ||
| const index = []; | ||
|
|
||
| for (const file of files) { | ||
| const content = readFileSync(file, "utf-8"); | ||
| const rel = "/" + relative(PAGES_DIR, file).replace(/\.md$/, ""); | ||
| const path = rel === "/index" ? "/" : rel; | ||
|
|
||
| const { title, description } = extractFrontmatter(content); | ||
| const headings = extractHeadings(content); | ||
| const section = getSection(path); | ||
|
|
||
| // For the home page, only include title (body is mostly card markup) | ||
| let body = ""; | ||
| if (path !== "/") { | ||
| body = stripMarkdown(content); | ||
| if (body.length > MAX_BODY_LENGTH) { | ||
| body = body.substring(0, MAX_BODY_LENGTH); | ||
| } | ||
| } | ||
|
|
||
| index.push({ | ||
| path, | ||
| title, | ||
| description: description || "", | ||
| section, | ||
| headings, | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| writeFileSync(OUTPUT, JSON.stringify(index, null, 2) + "\n"); | ||
| console.log( | ||
| `Generated search index for ${index.length} pages (${ | ||
| Buffer.byteLength(JSON.stringify(index)) / 1024 | ||
| } KB)` | ||
| ); |
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
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.
Silent failure when index fails to load.
If the fetch fails, the error is only logged to the console. Users see an empty quick links state (since
searchDataremainsnull,quickLinkswill be empty) with no indication that something went wrong.🛡️ Proposed fix to show error state
Then in the render:
{loading ? ( <div className="search-no-results">Loading...</div> +) : error ? ( + <div className="search-no-results">{error}</div> ) : !hasQuery ? (🤖 Prompt for AI Agents