-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add vitepress local search #99
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"Chocolate1999", | ||
"HearLing", | ||
"holazz", | ||
"egoist" | ||
"egoist", | ||
"fxzer" | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# VitePress添加本地搜索功能 | ||
|
||
::: warning 踩坑历程 | ||
|
||
折腾了三遍Algolia都没能添加上搜索功能,最后在找到了这个[issus](https://github.com/vuejs/vitepress/issues/670)里大佬提供的解决方案,成功添加上了本地搜索功能。 | ||
|
||
::: | ||
|
||
### 安装插件 | ||
|
||
```bash | ||
npm i vitepress-plugin-search markdown-it flexsearch -D | ||
``` | ||
|
||
### 添加和配置插件 | ||
|
||
::: warning 坑点 | ||
1.[README](https://github.com/emersonbottero/vitepress-plugin-search#readme) 没写在哪个目录下存放`vite.config.ts`,依据经验放在根目录下不管用,放在`.vitepress`也不生效,最后挨个试才发现需要放在`docs` | ||
|
||
2.示例没有引入`flexSearchIndexOptions`,需要手动从`flexsearch`中引入 | ||
|
||
3.引入后发现之前搜索框样式没了,需要在`.vitepress/theme/styles/index.css`下重新覆盖样式 | ||
|
||
::: | ||
|
||
|
||
|
||
 | ||
|
||
|
||
|
||
```typescript | ||
|
||
//vite.config.ts | ||
import { SearchPlugin } from "vitepress-plugin-search"; | ||
import { defineConfig } from "vite"; | ||
import flexSearchIndexOptions from "flexsearch"; | ||
//default options | ||
var options = { | ||
...flexSearchIndexOptions, | ||
previewLength: 100,//搜索结果预览长度 | ||
buttonLabel: "搜索", | ||
placeholder: "情输入关键词", | ||
}; | ||
|
||
export default defineConfig({ | ||
plugins: [SearchPlugin(options)], | ||
}); | ||
``` | ||
|
||
|
||
|
||
### 样式覆盖 | ||
|
||
```css | ||
.DocSearch-Button { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
margin: 0; | ||
padding: 0; | ||
width: 32px; | ||
height: 32px; | ||
border-radius: 4px; | ||
background: transparent; | ||
transition: border-color 0.25s; | ||
} | ||
@media (min-width: 768px) { | ||
.DocSearch-Button { | ||
justify-content: flex-start; | ||
border: 1px solid transparent; | ||
border-radius: 8px; | ||
padding: 0 10px 0 12px; | ||
width: 100%; | ||
height: 40px; | ||
background-color: var(--vp-c-bg-alt); | ||
} | ||
} | ||
@media (max-width: 768px) { | ||
.DocSearch-Button-Keys { | ||
display: none; | ||
} | ||
.VPNavBarHamburger{ | ||
height: 32px !important; | ||
width: 32px !important; | ||
border-radius: 4px; | ||
} | ||
} | ||
.DocSearch-Button:hover { | ||
background-color: #f5f5f6; | ||
} | ||
|
||
``` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议:一些中英文最好用空格隔开,如果你用的是搜狗输入法的话,里面有个设置是可以自动给你加上的,会美观些