Skip to content
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

add keyword search capabilities #203

Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/app/basics/test-suites/pr-comments/page.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: Pull Request Comments
keywords: PR, PR Comments, GitHub
description: Pull Request Comments show updated results of your test run with links to replays and the Test Suite Dashboard, all from the pull request in GitHub.
image: /images/pr-comment.png
---
Expand Down
4 changes: 2 additions & 2 deletions src/components/DocsHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export function DocsHeader({
}) {
let pathname = usePathname()

let section = flatNavigation.find((section) =>
section.links?.find((link) => link.href === pathname),
let section = flatNavigation.find(
(section) => section.links?.find((link) => link.href === pathname),
)

if (!title && !section) {
Expand Down
5 changes: 3 additions & 2 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ function SearchResult({
let id = useId()
let pathname = usePathname()

let sectionTitle = flatNavigation.find((section) =>
section.links?.find((link) => link.href === result.url.split('#')[0]),
let sectionTitle = flatNavigation.find(
(section) =>
section.links?.find((link) => link.href === result.url.split('#')[0]),
)?.title
let hierarchy = [sectionTitle, result.pageTitle].filter(
(x): x is string => typeof x === 'string',
Expand Down
14 changes: 9 additions & 5 deletions src/markdoc/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ export default function withSearch(nextConfig = {}) {
let md = fs.readFileSync(path.join(pagesDir, file), 'utf8')

let sections
let ast = Markdoc.parse(md)
let keywords =
ast.attributes?.frontmatter
?.match(/^keywords:\s*(.*?)\s*$/m)?.[1]
.split(/,\s+/) || []

if (cache.get(file)?.[0] === md) {
sections = cache.get(file)[1]
} else {
let ast = Markdoc.parse(md)
let title =
ast.attributes?.frontmatter?.match(
/^title:\s*(.*?)\s*$/m,
Expand All @@ -74,7 +78,7 @@ export default function withSearch(nextConfig = {}) {
cache.set(file, [md, sections])
}

return { url, sections }
return { url, sections, keywords }
})

// When this file is imported within the application
Expand All @@ -87,7 +91,7 @@ export default function withSearch(nextConfig = {}) {
document: {
id: 'url',
index: 'content',
store: ['title', 'pageTitle'],
store: ['title', 'pageTitle', 'keywords'],
},
context: {
resolution: 9,
Expand All @@ -98,12 +102,12 @@ export default function withSearch(nextConfig = {}) {

let data = ${JSON.stringify(data)}

for (let { url, sections } of data) {
for (let { url, sections, keywords } of data) {
for (let [title, hash, content] of sections) {
sectionIndex.add({
url: url + (hash ? ('#' + hash) : ''),
title,
content: [title, ...content].join('\\n'),
content: [title, ...keywords, ...content ].join('\\n'),
pageTitle: hash ? sections[0][0] : undefined,
})
}
Expand Down
Loading