Skip to content

Commit 37d5b27

Browse files
Jérémy Riverainbrc-dd
Jérémy Riverain
andauthored
feat(search): allow excluding content from search results (#2602)
closes #2344 Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 94e2966 commit 37d5b27

File tree

12 files changed

+300
-205
lines changed

12 files changed

+300
-205
lines changed

__tests__/e2e/.vitepress/config.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ export default defineConfig({
8787
title: 'Example',
8888
description: 'An example app using VitePress.',
8989
themeConfig: {
90-
sidebar
90+
sidebar,
91+
search: {
92+
provider: 'local',
93+
options: {
94+
exclude(relativePath) {
95+
return relativePath.startsWith('local-search/excluded')
96+
}
97+
}
98+
}
9199
}
92100
})
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Local search config excluded
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
search: false
3+
---
4+
5+
# Local search frontmatter excluded

__tests__/e2e/local-search/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Local search included
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
describe('local search', () => {
2+
beforeEach(async () => {
3+
await goto('/')
4+
// FIXME: remove this when optimizeDeps.include is fixed
5+
await page.locator('#local-search button').click()
6+
await goto('/')
7+
})
8+
9+
test('exclude content from search results', async () => {
10+
await page.locator('#local-search button').click()
11+
12+
const input = await page.waitForSelector('input#localsearch-input')
13+
await input.type('local')
14+
15+
await page.waitForSelector('ul#localsearch-list', { state: 'visible' })
16+
17+
const searchResults = page.locator('#localsearch-list')
18+
expect(await searchResults.locator('li[role=option]').count()).toBe(1)
19+
20+
expect(
21+
await searchResults.filter({ hasText: 'Local search included' }).count()
22+
).toBe(1)
23+
24+
expect(
25+
await searchResults.filter({ hasText: 'Local search excluded' }).count()
26+
).toBe(0)
27+
28+
expect(
29+
await searchResults
30+
.filter({ hasText: 'Local search frontmatter excluded' })
31+
.count()
32+
).toBe(0)
33+
})
34+
})

__tests__/e2e/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"test": "vitest run",
77
"watch": "DEBUG=1 vitest",
8-
"site": "vitepress"
8+
"site:dev": "vitepress",
9+
"site:build": "vitepress build",
10+
"site:preview": "vitepress preview"
911
},
1012
"devDependencies": {
1113
"vitepress": "workspace:*"

docs/reference/default-theme-search.md

+18
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ export default defineConfig({
9898

9999
Learn more in [MiniSearch docs](https://lucaong.github.io/minisearch/classes/_minisearch_.minisearch.html).
100100

101+
### Excluding pages from search
102+
103+
You can exclude pages from search by adding `search: false` to the frontmatter of the page. Alternatively, you can also pass `exclude` function to `themeConfig.search.options` to exclude pages based on their path relative to `srcDir`:
104+
105+
```ts
106+
import { defineConfig } from 'vitepress'
107+
108+
export default defineConfig({
109+
themeConfig: {
110+
search: {
111+
options: {
112+
exclude: (path) => path.startsWith('/some/path')
113+
}
114+
}
115+
}
116+
})
117+
```
118+
101119
## Algolia Search
102120

103121
VitePress supports searching your docs site using [Algolia DocSearch](https://docsearch.algolia.com/docs/what-is-docsearch). Refer their getting started guide. In your `.vitepress/config.ts` you'll need to provide at least the following to make it work:

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
"test:unit": "vitest run -r __tests__/unit",
7070
"test:unit:watch": "vitest -r __tests__/unit",
7171
"test:e2e": "run-s test:e2e-dev test:e2e-build",
72-
"test:e2e:site": "pnpm -F=tests-e2e site",
72+
"test:e2e:site:dev": "pnpm -F=tests-e2e site:dev",
73+
"test:e2e:site:build": "pnpm -F=tests-e2e site:build",
74+
"test:e2e:site:preview": "pnpm -F=tests-e2e site:preview",
7375
"test:e2e-dev": "pnpm -F=tests-e2e test",
7476
"test:e2e-dev:watch": "pnpm -F=tests-e2e watch",
7577
"test:e2e-build": "VITE_TEST_BUILD=1 pnpm test:e2e-dev",
@@ -100,7 +102,7 @@
100102
"mark.js": "8.11.1",
101103
"minisearch": "^6.1.0",
102104
"shiki": "^0.14.3",
103-
"vite": "^4.4.4",
105+
"vite": "^4.4.6",
104106
"vue": "^3.3.4"
105107
},
106108
"devDependencies": {
@@ -138,7 +140,7 @@
138140
"conventional-changelog-cli": "^2",
139141
"cross-spawn": "^7.0.3",
140142
"debug": "^4.3.4",
141-
"esbuild": "^0.18.14",
143+
"esbuild": "^0.18.15",
142144
"escape-html": "^1.0.3",
143145
"execa": "^7.1.1",
144146
"fast-glob": "^3.3.0",

0 commit comments

Comments
 (0)