Skip to content
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

🐈 [new-site-search] Add custom site search using Lunr.js. #203

Merged
merged 22 commits into from
Nov 28, 2017

Conversation

jgerigmeyer
Copy link
Member

@jgerigmeyer jgerigmeyer commented Nov 22, 2017

Another attempt at fixing #28.

zebra

wlonk and others added 14 commits November 17, 2017 12:03
With no real search content yet.
* master: (25 commits)
  Upgrade deps.
  compile
  cleanup
  add subtitle supplement
  icon path above icon preview section
  review cleanup
  display icon path, edit path style
  remove tooltip code, simplify icon styles
  Normalize iconsPath
  Remove filename from icon path.
  compile
  Cleanup fonts/scale docs & previews
  if we choose filepath on top, keep this commit
  filepath - version 2 - above filename
  Cleanup font docs
  force tooltips to break
  stylelint
  adding tooltip pattern styles, edit icon layout
  Add example to njk examples doc
  Add use-cases to relevant config
  ...
* master:
  Style branding
  update changelog
  Add Herman favicon.
  Add attribution/links to nav footer (needs styling).
@jgerigmeyer
Copy link
Member Author

@mirisuzanne Take a look at the results here... ready for styling if the functionality looks okay. We have more control over the functionality and markup here, so let me know if you want changes.

@jgerigmeyer jgerigmeyer changed the title [new-site-search] Add custom site search using Lunr.js. 🐈 [new-site-search] Add custom site search using Lunr.js. Nov 22, 2017
Copy link
Member

@mirisuzanne mirisuzanne left a comment

Choose a reason for hiding this comment

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

Looks great to me!

gulpfile.js Outdated
@@ -421,3 +423,10 @@ gulp.task('imagemin', () => {
});

gulp.task('minify', ['jsmin', 'svgmin', 'imagemin']);

gulp.task('precompile-templates', () =>
Copy link
Member

Choose a reason for hiding this comment

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

This had some issues on occasion – I had to restart my server quite often while working.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, what sort of errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

Only time I've been able to reproduce errors is when I make multiple quick changes in a row, before compilation has time to complete.

@jgerigmeyer
Copy link
Member Author

Right now I'm showing 15 words before and after every search result match, but showing all result matches. When you search for a common word (e.g. color), that means a lot of matches on some pages. I wonder about limiting it to only display the context for the first 3 or 4 or 5 matches or something like that?

@jgerigmeyer
Copy link
Member Author

Okay, I think this is to a point where I'm okay reviewing/merging it. There's still one small bug in the JS search tool that I'm waiting on a fix for (see olivernn/lunr.js#320), but I expect that to come in shortly.

This branch also inspired me to add two new issues (#204, which can probably wait for after 1.0, and #205, which should probably happen ASAP).

<aside>
To see the Lunr bug, search for two words, e.g. color palette -- the second word is not properly associated with the "field" it was matched in, e.g. "title" or "contents". Right now I'm defaulting to "contents", which means if it was actually matched in the "title" then it gets highlighted in the wrong place. The highlight of nting co in the following screenshot should be highlighting Palettes in the title.

screenshot 2017-11-23 12 22 51
</aside>

@jgerigmeyer
Copy link
Member Author

Sidenote: I added another branch, new-site-search-fuse (https://github.com/oddbird/sassdoc-theme-herman/tree/new-site-search-fuse), which uses the Fuse.js package instead of Lunr.js. Check it out if you're interested in seeing the difference between two search algorithms. It's much smaller in size and there are a few things I like better about it, but it's much more "fuzzy" and I can't get it to act more like a typical strict search. (There are some relevant issues on the repo that I'm following, e.g. krisk/Fuse#147). So until the results there are better, I think we stick with Lunr.

if (typeof window.deparam !== 'undefined') {
params = window.deparam(window.location.search.substr(1));
}
return params;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to get URL params if deparam is undefined?

Copy link
Member Author

@jgerigmeyer jgerigmeyer Nov 27, 2017

Choose a reason for hiding this comment

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

¯\_(ツ)_/¯ It's kind of a theoretical question... this fn should never be called currently in cases where $.deparam isn't defined. (There are various pieces of JS that are only used by the search-results page, so only loaded on that page.)

}
// Add search result template to set of results
matches = matches.add(el);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not urgent, but if showResults could be decomposed into well-named things, that'd probably help. It's a bit hard to follow.

showResults(results, val);
};

Herman.getSearchData = function getSearchData() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why isn't this an anonymous function assigned to Herman.getSearchData?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call; no reason.

// Only fetch search data if on search results page with query term
if (params && params.q && hasLunr && hasNunjucks) {
const request = new XMLHttpRequest();
request.open('GET', '/search-data.json', true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we not use fetch here? Is this a backwards-compatibility thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

We're currently using the slim build of jQuery, which does not include $.ajax &c. It didn't seem worth adding for simple $.getJSON call, so I did it the old-school way.

@@ -1,5 +1,6 @@
'use strict';

const $ = require('cheerio');
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL about cheerio!

* master:
  Bump version to 1.0.0-rc.1
  Remove old sourcemaps
@jgerigmeyer jgerigmeyer merged commit 1d6da3a into master Nov 28, 2017
@jgerigmeyer jgerigmeyer deleted the new-site-search branch November 28, 2017 15:06
@jgerigmeyer jgerigmeyer mentioned this pull request Nov 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants