Add highlighting for search terms in results

This commit is contained in:
Samy Pesse
2016-10-13 19:33:04 +02:00
parent 4db57fe5c5
commit 4c174f916e
3 changed files with 21 additions and 6 deletions
+2 -1
View File
@@ -5,7 +5,8 @@
"browser": "./_assets/theme.js",
"version": "4.0.0",
"dependencies": {
"gitbook-core": "4.0.0"
"gitbook-core": "4.0.0",
"react": "^15.3.2"
},
"devDependencies": {
"gitbook-plugin": "4.0.0",
@@ -1,22 +1,31 @@
const GitBook = require('gitbook-core');
const { React } = GitBook;
const Highlight = require('react-highlighter');
const Result = React.createClass({
propTypes: {
result: React.PropTypes.object
result: React.PropTypes.object,
query: React.PropTypes.string
},
render() {
const { result } = this.props;
const { result, query } = this.props;
return (
<div className="Search-ResultContainer">
<GitBook.InjectedComponent matching={{ role: 'search:result' }} props={{ result }}>
<GitBook.InjectedComponent matching={{ role: 'search:result' }} props={{ result, query }}>
<div className="Search-Result">
<h3>
<GitBook.Link to={result.url}>{result.title}</GitBook.Link>
</h3>
<p>{result.body}</p>
<p>
<Highlight
matchElement="span"
matchClass="Search-MatchSpan"
search={query}>
{result.body}
</Highlight>
</p>
</div>
</GitBook.InjectedComponent>
</div>
@@ -46,7 +55,7 @@ const SearchResults = React.createClass({
<h1>{i18n.t('SEARCH_RESULTS_TITLE', { query, count: results.size })}</h1>
<div className="Search-Results">
{results.map((result, i) => {
return <Result key={i} result={result} />;
return <Result key={i} result={result} query={query} />;
})}
</div>
</div>
@@ -21,3 +21,8 @@
color: inherit;
}
}
.Search-MatchSpan {
background: #ffd54f;
}