Polish caniuse search code

This commit is contained in:
Folyd 2020-10-23 13:56:56 +08:00
parent 0a9b113e28
commit 3903ba6577
2 changed files with 7 additions and 7 deletions

View file

@ -197,10 +197,10 @@ omnibox.addPrefixQueryEvent("?", {
let description;
if (query.startsWith("??")) {
content = `https://github.com/rust-lang/rfcs/pull/${feat.rfc}`;
description = `RFC: ${c.match(feat.match)} - ${feat.rest}`
description = `RFC: ${c.match(c.escape(feat.match))} - ${c.escape(feat.rest)}`
} else {
content = `https://caniuse.rs/features/${feat.slug}`;
description = `Can I use: ${c.match(feat.match)} - ${feat.rest}`
description = `Can I use: ${c.match(c.escape(feat.match))} - ${c.escape(feat.rest)}`
}
return {
content,

View file

@ -1,6 +1,6 @@
// `caniuseIndex` is an array of `feat`: [feat]
// where each `feat` is an ordered array of:
// - Rust version that the feature is stablized
// - Rust version that the feature is stabilized
// - The slug used in caniuse.rs URL
// - (null-able) The flag needed to use the feature in (unstable) Rust
// - A title that describes the feature
@ -10,24 +10,24 @@ function CaniuseSearch(index) {
this.feats = {};
index.forEach(([ver, slug, flag, title, rfc]) => {
// `match` is for highlighting
let match = flag ?? title;
let match = flag || title;
// `rest` contains other information in description
let rest = ((flag == null) ? [ver] : [title, ver]).join(" - ");
let searchTerm = `${match}${rest}`.replace(/[-_\s#\?]/ig, "");
let searchTerm = `${match}${rest}`.replace(/[-_\s#?]/ig, "");
this.feats[searchTerm] = { slug, match, rest, rfc };
});
this.searchTerms = Object.keys(this.feats);
}
CaniuseSearch.prototype.search = function (rawKeyword) {
let keyword = rawKeyword.replace(/[-_\s#\?]/ig, "");
let keyword = rawKeyword.toLowerCase().replace(/[-_\s#?]/ig, "");
let result = [];
for (let searchTerm of this.searchTerms) {
if (searchTerm.length < keyword.length) continue;
let foundAt = searchTerm.indexOf(keyword);
let foundAt = searchTerm.toLowerCase().indexOf(keyword);
if (foundAt > -1) {
let rfc = this.feats[searchTerm].rfc;
// skip those without RFC when searching for RFCs