Skip to content

Commit

Permalink
Merge pull request #203 from replayio/filip/dev-495-docs-structure-do…
Browse files Browse the repository at this point in the history
…es-not-play-well-with-search-and-breadcumbs

add keyword search capabilities
  • Loading branch information
filiphric authored Jun 18, 2024
2 parents 25c6098 + a6e260c commit 13d8a86
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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
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

0 comments on commit 13d8a86

Please sign in to comment.