Skip to content

Commit 1c95d6c

Browse files
authored
docs(scripts): include page in search results (#2392)
Add base component page entries to the search index before H2 sections to improve discoverability and ranking in search results.
1 parent 3c97934 commit 1c95d6c

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

docs/scripts/index-docs.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const files = fs.readdirSync(COMPONENTS_PATH);
1717
* @property {string} text
1818
* @property {string} description
1919
* @property {string} href
20+
* @property {boolean} isComponent
2021
*/
2122

2223
/** @type {Document[]} */
@@ -26,8 +27,20 @@ for (const file of files) {
2627
const [componentName] = file.split(".");
2728
const filePath = path.join(COMPONENTS_PATH, file);
2829
const fileContent = fs.readFileSync(filePath, "utf8");
29-
30-
for (const line of fileContent.split("\n")) {
30+
const lines = fileContent.split("\n");
31+
32+
// Add base component page entry first (for higher ranking)
33+
// Use component name in description to boost ranking for MiniSearch.
34+
documents.push({
35+
id: `${componentName}-page`,
36+
text: componentName,
37+
description: componentName,
38+
href: `/components/${componentName}`,
39+
isComponent: true,
40+
});
41+
42+
// Add H2 sections
43+
for (const line of lines) {
3144
if (line.startsWith(H2_DELMIMITER)) {
3245
const [, h2] = line.split(H2_DELMIMITER);
3346
const hash = slug(h2);
@@ -37,6 +50,7 @@ for (const file of files) {
3750
text: componentName,
3851
description: h2,
3952
href: `/components/${componentName}#${hash}`,
53+
isComponent: false,
4054
});
4155
}
4256
}

docs/src/pages/_layout.svelte

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
SideNavItems,
2020
SideNavMenuItem,
2121
SkipToContent,
22+
Stack,
2223
Tag,
2324
Theme,
2425
} from "carbon-components-svelte";
@@ -29,7 +30,7 @@
2930
3031
const miniSearch = new MiniSearch({
3132
fields: ["text", "description"],
32-
storeFields: ["text", "description", "href"],
33+
storeFields: ["text", "description", "href", "isComponent"],
3334
searchOptions: {
3435
prefix: true,
3536
boost: { description: 2 },
@@ -155,10 +156,27 @@
155156
placeholder="Search"
156157
spellcheck="false"
157158
{results}
159+
let:result
158160
on:select={(e) => {
159161
$goto(e.detail.selectedResult.href);
160162
}}
161-
/>
163+
>
164+
<Stack
165+
gap={3}
166+
orientation="horizontal"
167+
style="display: flex; align-items: center;"
168+
>
169+
{result.text}
170+
{#if result.description && !result.isComponent}
171+
<span class="bx--header-search-menu-description">
172+
{result.description}
173+
</span>
174+
{/if}
175+
{#if result.isComponent}
176+
<Tag size="sm" type="blue" style="margin: 0">Component</Tag>
177+
{/if}
178+
</Stack>
179+
</HeaderSearch>
162180
<HeaderActionLink
163181
icon={LogoGithub}
164182
href="https://github.com/carbon-design-system/carbon-components-svelte"

0 commit comments

Comments
 (0)