11import React from 'react' ;
22import { List , Input , Icon , Breadcrumb } from 'antd' ;
3- import styles from './index.module.less' ;
43import { PageInfo } from '../utils' ;
54import { OneToc } from '../../templates/docs' ;
65import { Link } from 'gatsby' ;
@@ -19,6 +18,7 @@ interface SearchState {
1918
2019interface SearchProps {
2120 datas : Array < PageInfo > ;
21+ max : number ;
2222}
2323
2424function 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 >
0 commit comments