Skip to content

Commit 2ab5545

Browse files
committed
Fix and improve search using fuzzysearch
1 parent 4c39ff0 commit 2ab5545

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

uilib-docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "uilib-docs",
33
"description": "uilib docs template",
4-
"version": "1.0.12",
4+
"version": "1.0.13",
55
"author": "Ethan Sharabi <[email protected]>",
66
"main": "index.js",
77
"dependencies": {
88
"classnames": "^2.2.6",
9+
"fuzzysearch": "^1.0.3",
910
"gatsby": "^2.0.0",
1011
"gatsby-image": "^2.0.34",
1112
"gatsby-plugin-layout": "^1.3.10",

uilib-docs/src/components/layout.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,4 @@ const Layout = ({children, location}) => {
6868
);
6969
};
7070

71-
Layout.propTypes = {
72-
navbar: PropTypes.element,
73-
children: PropTypes.func
74-
};
75-
7671
export default Layout;

uilib-docs/src/components/navbar/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {Component} from 'react';
22
import {StaticQuery, graphql} from 'gatsby';
33
import classnames from 'classnames';
44
import _ from 'lodash';
5+
import fuzzysearch from 'fuzzysearch';
56

67
import './index.scss';
78
import searchIcon from '../../images/search.svg';
@@ -35,15 +36,17 @@ class Navbar extends Component {
3536
}
3637
};
3738

38-
getMarkdownPages(data) {
39+
getMarkdownPages = data => {
40+
const {filter} = this.state;
3941
const markdownPages = data.allFile.edges;
4042
const pages = _.flow(
4143
pages => _.map(pages, ({node}) => node.childMarkdownRemark.frontmatter),
44+
items => _.filter(items, item => fuzzysearch(_.toLower(filter), _.toLower(item.title))),
4245
items => _.sortBy(items, 'index')
4346
)(markdownPages);
4447

4548
return pages;
46-
}
49+
};
4750

4851
getNavbarComponents(data) {
4952
const components = data.allComponentMetadata.edges;
@@ -84,9 +87,8 @@ class Navbar extends Component {
8487
const markdowns = this.getMarkdownPages(data);
8588
const components = this.getNavbarComponents(data);
8689
const filteredComponents = _.filter(components, component =>
87-
_.includes(_.lowerCase(component.node.displayName), _.lowerCase(filter))
90+
fuzzysearch(_.toLower(filter), _.toLower(component.node.displayName))
8891
);
89-
9092
const componentsByGroups = _.groupBy(filteredComponents, c => _.split(c.node.displayName, '.')[0]);
9193

9294
const navbarClassName = classnames('navbar', {
@@ -99,17 +101,20 @@ class Navbar extends Component {
99101
{this.renderSearch()}
100102
<ul>
101103
{_.map(markdowns, page => {
102-
return <Item id={page.title} link={page.path} onLinkClick={() => this.toggleNavbar(false)} />;
104+
return (
105+
<Item key={page.title} id={page.title} link={page.path} onLinkClick={() => this.toggleNavbar(false)} />
106+
);
103107
})}
104-
<li className="separator" />
108+
{!_.isEmpty(markdowns) && <li className="separator" />}
105109
{_.map(componentsByGroups, (components, key) => {
106110
return (
107111
<Item
112+
key={key}
108113
id={key}
109114
components={components}
110115
currentPage={currentPage}
111116
onLinkClick={() => this.toggleNavbar(false)}
112-
></Item>
117+
/>
113118
);
114119
})}
115120
</ul>

0 commit comments

Comments
 (0)