From f9e6c8869cd21c35f3362758761881da110ebabf Mon Sep 17 00:00:00 2001 From: Filip Hric Date: Mon, 17 Jun 2024 17:49:18 +0200 Subject: [PATCH 1/3] add keyword search capabilities --- src/app/basics/test-suites/pr-comments/page.md | 1 + src/markdoc/search.mjs | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/app/basics/test-suites/pr-comments/page.md b/src/app/basics/test-suites/pr-comments/page.md index bf57a6bc..d9e3b767 100644 --- a/src/app/basics/test-suites/pr-comments/page.md +++ b/src/app/basics/test-suites/pr-comments/page.md @@ -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 --- diff --git a/src/markdoc/search.mjs b/src/markdoc/search.mjs index 216a439d..27ea760f 100644 --- a/src/markdoc/search.mjs +++ b/src/markdoc/search.mjs @@ -60,11 +60,14 @@ 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, @@ -74,7 +77,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 @@ -87,7 +90,7 @@ export default function withSearch(nextConfig = {}) { document: { id: 'url', index: 'content', - store: ['title', 'pageTitle'], + store: ['title', 'pageTitle', 'keywords'], }, context: { resolution: 9, @@ -98,12 +101,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, }) } From f288d7e88957e5012444ae5915d37d0eff3a1d96 Mon Sep 17 00:00:00 2001 From: Filip Hric Date: Mon, 17 Jun 2024 17:55:45 +0200 Subject: [PATCH 2/3] fix formatting --- src/components/DocsHeader.tsx | 4 ++-- src/components/Search.tsx | 5 +++-- src/markdoc/search.mjs | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/components/DocsHeader.tsx b/src/components/DocsHeader.tsx index faebff14..47b7f2b6 100644 --- a/src/components/DocsHeader.tsx +++ b/src/components/DocsHeader.tsx @@ -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) { diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 13d8726f..80aba795 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -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', diff --git a/src/markdoc/search.mjs b/src/markdoc/search.mjs index 27ea760f..0eaf7ca7 100644 --- a/src/markdoc/search.mjs +++ b/src/markdoc/search.mjs @@ -61,9 +61,10 @@ export default function withSearch(nextConfig = {}) { let sections let ast = Markdoc.parse(md) - let keywords = ast.attributes?.frontmatter?.match( - /^keywords:\s*(.*?)\s*$/m, - )?.[1].split(/,\s+/) || [] + let keywords = + ast.attributes?.frontmatter + ?.match(/^keywords:\s*(.*?)\s*$/m)?.[1] + .split(/,\s+/) || [] if (cache.get(file)?.[0] === md) { sections = cache.get(file)[1] From a6e260cb4da31c40bf77e32fd29b260fea9d4e0e Mon Sep 17 00:00:00 2001 From: Filip Hric Date: Tue, 18 Jun 2024 10:12:34 +0200 Subject: [PATCH 3/3] fix formatting --- src/components/DocsHeader.tsx | 4 ++-- src/components/Search.tsx | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/components/DocsHeader.tsx b/src/components/DocsHeader.tsx index 47b7f2b6..faebff14 100644 --- a/src/components/DocsHeader.tsx +++ b/src/components/DocsHeader.tsx @@ -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) { diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 80aba795..13d8726f 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -162,9 +162,8 @@ 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',