8000 feat: add vitepress local search by fxzer · Pull Request #99 · chodocs/chodocs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default {
items: [
{ text: '发布 npm 包', link: '/program/npm-package/' },
{ text: '给 VitePress 添加 algolia 搜索', link: '/program/vitepress-algolia/' },
{ text: '给 VitePress 添加本地搜索功能', link: '/program/vitepress-local-search/' },
],
},
{
Expand Down
3 changes: 2 additions & 1 deletion docs/contributors.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"Chocolate1999",
"HearLing",
"holazz",
"egoist"
"egoist",
"fxzer"
]
11 changes: 11 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ const members = [
{ icon: 'twitter', link: 'https://twitter.com/holazz1208' },
]
},
{
avatar: 'https://www.github.com/fxzer.png',
name: 'fxzer',
title: 'Open Source Contributor',
links: [
{ icon: 'github', link: 'https://github.com/fxzer' },
{
icon: { svg: icons.bilibili } ,link: "https://space.bilibili.com/228134791",
},
]
},
]
</script>

Expand Down
94 changes: 94 additions & 0 deletions docs/program/vitepress-local-search/index.md
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)里大佬提供的解决方案,成功添加上了本地搜索功能。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:一些中英文最好用空格隔开,如果你用的是搜狗输入法的话,里面有个设置是可以自动给你加上的,会美观些


:::

### 安装插件

```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`下重新覆盖样式

:::



![image-20230205233032922](https://zerdocs.oss-cn-shanghai.aliyuncs.com/202302052330956.png)



```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;
}

```

0