Skip to content

Commit 0963c58

Browse files
committed
feat(antdsite): add searchMaxSuggestions for searchbox
1 parent e13ef83 commit 0963c58

8 files changed

Lines changed: 141 additions & 55 deletions

File tree

packages/antdsite/__default__/default-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
showAvatarList: true,
1717
showBackToTop: true,
1818
maxTocDeep: 3,
19-
showSearchBox: true,
19+
search: true,
20+
searchMaxSuggestions: 10,
2021
},
2122
};

packages/antdsite/src/assets/index.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
@import './theme';
2222
@import './docsearch';
2323
@import './page-scrollbar';
24+
@import './search-box';

packages/antdsite/src/components/search-box/index.module.less renamed to packages/antdsite/src/assets/search-box.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
margin: 16px auto 0;
44
position: relative;
55

6+
.hight-light {
7+
color: #1890ff;
8+
}
9+
610
input {
711
border-radius: 32px;
812
width: 200px;

packages/antdsite/src/assets/toc.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
position: absolute;
1212
top: 8px;
1313
right: 20px;
14-
max-width: 174px;
14+
max-width: 170px;
1515
.ant-affix {
1616
background: #fff;
1717
max-height: 80vh;

packages/antdsite/src/components/search-box/index.tsx

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import { List, Input, Icon, Breadcrumb } from 'antd';
3-
import styles from './index.module.less';
43
import { PageInfo } from '../utils';
54
import { OneToc } from '../../templates/docs';
65
import { Link } from 'gatsby';
@@ -19,6 +18,7 @@ interface SearchState {
1918

2019
interface SearchProps {
2120
datas: Array<PageInfo>;
21+
max: number;
2222
}
2323

2424
function match(a: string, b: string) {
@@ -77,22 +77,27 @@ export default class Search extends React.Component<SearchProps, SearchState> {
7777
};
7878

7979
search = () => {
80-
const { datas } = this.props;
80+
const { datas, max } = this.props;
8181
const query = this.state.query.trim();
8282
const results: filterDatas = [];
8383

84+
function hightlightRes(res: string) {
85+
return res.replace(new RegExp(query, 'g'), `<span class='hight-light'>${query}</span>`);
86+
}
87+
8488
function resolveOnePageItem(currentItem: PageInfo) {
8589
if (match(currentItem.title, query)) {
8690
results.push([
8791
{
8892
url: currentItem.slug,
89-
title: currentItem.title,
93+
title: hightlightRes(currentItem.title),
9094
important: currentItem.important,
9195
},
9296
]);
9397
} else if (currentItem.toc && currentItem.toc.items.length) {
9498
let tocs = flattenToc(currentItem.toc.items);
95-
tocs.forEach(t => {
99+
for (let i = 0; i < tocs.length && results.length < max; i++) {
100+
let t = tocs[i];
96101
if (match(t.title, query)) {
97102
results.push([
98103
{
@@ -101,15 +106,15 @@ export default class Search extends React.Component<SearchProps, SearchState> {
101106
},
102107
{
103108
url: currentItem.slug + t.url,
104-
title: t.title,
109+
title: hightlightRes(t.title),
105110
},
106111
]);
107112
}
108-
});
113+
}
109114
}
110115
}
111116

112-
for (let i = 0; i < datas.length; i++) {
117+
for (let i = 0; i < datas.length && results.length < max; i++) {
113118
const currentItem = datas[i];
114119
if (currentItem.slug) {
115120
resolveOnePageItem(currentItem);
@@ -129,8 +134,8 @@ export default class Search extends React.Component<SearchProps, SearchState> {
129134
const { filterDatas } = this.state;
130135

131136
return (
132-
<div id="search-box" className={styles.searchBox}>
133-
<div className={styles.searchInputComponent}>
137+
<div id="search-box" className="search-box">
138+
<div className="searchInput-component">
134139
<Icon type="search" />
135140
<Input
136141
ref={ref => {
@@ -143,7 +148,7 @@ export default class Search extends React.Component<SearchProps, SearchState> {
143148
/>
144149
</div>
145150

146-
<div className={styles.searchResultList}>
151+
<div className="search-result-list">
147152
{this.state.isSearchListShow && this.state.filterDatas.length ? (
148153
<List
149154
key="search-list"
@@ -153,7 +158,7 @@ export default class Search extends React.Component<SearchProps, SearchState> {
153158
<List.Item>
154159
<Link
155160
to={dataItem[dataItem.length - 1].url as string}
156-
className={styles.searchItem}
161+
className="search-item "
157162
onMouseDown={() => {
158163
this.isClickLink = true;
159164
}}
@@ -164,10 +169,16 @@ export default class Search extends React.Component<SearchProps, SearchState> {
164169
>
165170
<List.Item.Meta
166171
description={
167-
<Breadcrumb separator=">" className={styles.ellipsis}>
172+
<Breadcrumb separator=">" className="ellipsis">
168173
{dataItem.map((item, index) => (
169174
// <Badge dot={i.important}>
170-
<Breadcrumb.Item key={index}>{item.title}</Breadcrumb.Item>
175+
<Breadcrumb.Item key={index}>
176+
<span
177+
dangerouslySetInnerHTML={{
178+
__html: item.title,
179+
}}
180+
></span>
181+
</Breadcrumb.Item>
171182
// </Badge>
172183
))}
173184
</Breadcrumb>

packages/antdsite/src/layout/Header.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Header extends React.Component<HeaderProps, HeaderState> {
9898
} = utils.getcurrentLocaleConfigBySlug(webConfig, slug);
9999
const { locales } = webConfig.themeConfig;
100100

101-
const { nav = [], showSearchBox } = themeConfig;
101+
const { nav = [], search, searchMaxSuggestions } = themeConfig;
102102
const activeMenuItem = nav
103103
.filter((item: any) => {
104104
return item.link && slug.startsWith(item.link);
@@ -158,8 +158,8 @@ class Header extends React.Component<HeaderProps, HeaderState> {
158158
}}
159159
/>
160160
</div> */}
161-
{showSearchBox && allPagesSidebarItems.length ? (
162-
<SearchBox datas={allPagesSidebarItems} />
161+
{search && allPagesSidebarItems.length ? (
162+
<SearchBox datas={allPagesSidebarItems} max={searchMaxSuggestions} />
163163
) : null}
164164
<div className="header-meta">
165165
<div className="right-header">

packages/docs/docs/zh/guide/getting-started.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,109 @@ title: 开始使用
44

55
# 开始上手
66

7-
1212
7+
## 通过 cli 安装
8+
9+
#### 使用 [yarn](https://yarnpkg.com) 进行安装`antdsite-cli`
10+
11+
```bash
12+
yarn global add antdsite-cli
13+
```
14+
15+
#### 或者使用[npm](https://docs.npmjs.com/cli/install.html)进行安装
16+
17+
```bash
18+
npm install antdsite-cli -g
19+
```
20+
21+
#### 使用 `cli` 来初始化一个网站
22+
23+
```bash
24+
antdsite my-docs
25+
```
26+
27+
#### 切换到初始化的网站目录,运行网站
28+
29+
```bash
30+
cd my-docs
31+
32+
yarn start
33+
# OR
34+
npm start
35+
```
36+
37+
#### 访问默认的地址 `localhost:8000` 即可看到效果页面。
38+
39+
<p aligin="center">
40+
<img src="https://github.com/wangyi7099/pictureCdn/blob/master/allPic/antdsite/screenshot.png?raw=true" width="700" />
41+
</p>
42+
43+
## 在现有项目安装
44+
45+
### 修改 package.json
46+
47+
#### `package.json`里添加依赖和启动脚本
48+
49+
#### 添加依赖
50+
51+
```diff
52+
"dependencies": {
53+
+ "antdsite": "^0.0.7",
54+
+ "gatsby": "^2.13.39",
55+
+ "react": "^16.8.0",
56+
+ "react-dom": "^16.8.0"
57+
},
58+
```
59+
60+
#### 如果你使用 yarn 的话,添加的脚本如下
61+
62+
```diff
63+
"scripts": {
64+
+ "build": "yarn clean && gatsby build",
65+
+ "start": "yarn clean && gatsby develop",
66+
+ "clean": "gatsby clean"
67+
}
68+
```
69+
70+
#### npm 脚本如下
71+
72+
```diff
73+
"scripts": {
74+
+ "build": "npm run clean && gatsby build",
75+
+ "start": "npm run clean && gatsby develop",
76+
+ "clean": "gatsby clean"
77+
}
78+
```
79+
80+
### 安装依赖
81+
82+
##### 执行`yarn`或者`npm`命令安装依赖
83+
84+
```bash
85+
yarn
86+
# OR
87+
npm
88+
```
89+
90+
### 新建一个 docs 文件夹
91+
92+
```bash
93+
mkdir docs
94+
```
95+
96+
### 新建`.antsite.js`
97+
98+
在项目的根目录建立`.antsite.js`
99+
100+
```js
101+
module.exports = {
102+
title: 'hello'
103+
};
104+
```
105+
106+
### 运行项目
107+
108+
```bash
109+
yarn start
110+
# OR
111+
npm start
112+
```

packages/docs/docs/zh/guide/introduction.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,7 @@ AntdSite 是一个基于[Ant Design](https://ant.design),由[GatsbyJs](https:/
3535
- 现代浏览器和 IE11。
3636
- 支持服务端渲染。
3737

38-
### 333333333333333333333333333333333333333333333
39-
4038
## 版本
4139

4240
- antdsite 版本:[![npm package](https://img.shields.io/npm/v/antdsite.svg?style=flat-square)](https://www.npmjs.org/package/antdsite)
4341
- antdsite-cli 版本:[![npm package](https://img.shields.io/npm/v/antdsite-cli.svg?style=flat-square)](https://www.npmjs.org/package/antdsite-cli)
44-
45-
## 安装
46-
47-
#### 使用 [yarn](https://yarnpkg.com) 进行安装`cli`
48-
49-
```bash
50-
yarn global add antdsite-cli
51-
```
52-
53-
#### 或者使用[npm](https://docs.npmjs.com/cli/install.html)进行安装
54-
55-
```bash
56-
npm install antdsite-cli -g
57-
```
58-
59-
#### 使用 `cli` 来初始化一个网站
60-
61-
```bash
62-
antdsite my-docs
63-
```
64-
65-
#### 切换到初始化的网站目录,运行网站
66-
67-
```bash
68-
cd my-docs
69-
70-
yarn start
71-
# OR
72-
npm start
73-
```
74-
75-
#### 访问默认的地址 `localhost:8000` 即可看到效果页面。
76-
77-
<img src="https://github.com/wangyi7099/pictureCdn/blob/master/allPic/antdsite/screenshot.png?raw=true" width="700" />

0 commit comments

Comments
 (0)