Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 2 additions & 39 deletions assets/js/search_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ layout: nil
importScripts("/assets/js/lunr.min.js");
importScripts("/assets/js/utils.js");
importScripts("/assets/js/search_functions.js");
function unaccent(e) { return e.update(utils.unaccented); }
lunr.Pipeline.registerFunction(unaccent, 'unaccent');

// IMPORTANT! Keep this up-to-date with the version in /_tests/tags.md
const OBU_STEMMER = function (token, i, tokens) {
// Don't stem tags
if (UNSTEMMED_WORDS.has(token.toString().toLowerCase())) {
return token;
}
return lunr.stemmer(token, i, tokens);
};
importScripts("/assets/js/search_worker_init.js");

{%- assign ccurly = "}" -%}
{%- assign ocurly = "{" -%}
Expand Down Expand Up @@ -44,31 +34,4 @@ var store = { {% assign all = site.documents | concat: site.pages %}
{% endfor %}
};

var idx = lunr(function () {
this.ref('id'); this.field('title', { boost: 10 });
this.field('author', { boost: 2 }); this.field('content');
this.field('translator'); this.field('format');
this.field('tag', { boost: 4 }); this.field('description', { boost: 2 });
this.field('in', { boost: 4 }); this.field('type', { boost: 0.3 });
this.field('is', { boost: 4 });
this.metadataWhitelist = ['position']
// this.pipeline.add(unaccent); // commented out as we're doing the unaccenting manually above
this.pipeline.remove(lunr.stemmer);
this.searchPipeline.remove(lunr.stemmer);
this.pipeline.add(OBU_STEMMER);
this.searchPipeline.add(OBU_STEMMER);
for (var key in store) {
var v = store[key];
this.add({
'id': key, 'title': utils.unaccented(v.title),
'author': v.authors.map(utils.unaccented).join(' '), 'content': utils.unaccented(v.content),
'translator': utils.unaccented(v.translator), 'format': v.formats.join(' '),
'tag': v.tags.join(' '), 'description': utils.unaccented(v.description),
'in': v.category, 'is': v.subcategory, 'type': v.type
}, {boost: v.boost});
}
});

self.onmessage = function(e) {
self.postMessage(handleSearchMessage(e.data, idx.search.bind(idx)));
}
initSearchWorker(store);
44 changes: 44 additions & 0 deletions assets/js/search_worker_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function unaccent(e) { return e.update(utils.unaccented); }
lunr.Pipeline.registerFunction(unaccent, 'unaccent');

// IMPORTANT! Keep this up-to-date with the version in /_tests/tags.md
const OBU_STEMMER = function (token, i, tokens) {
// Don't stem tags
if (UNSTEMMED_WORDS.has(token.toString().toLowerCase())) {
return token;
}
return lunr.stemmer(token, i, tokens);
};

function buildSearchIndex(store) {
return lunr(function () {
this.ref('id'); this.field('title', { boost: 10 });
this.field('author', { boost: 2 }); this.field('content');
this.field('translator'); this.field('format');
this.field('tag', { boost: 4 }); this.field('description', { boost: 2 });
this.field('in', { boost: 4 }); this.field('type', { boost: 0.3 });
this.field('is', { boost: 4 });
this.metadataWhitelist = ['position']
this.pipeline.remove(lunr.stemmer);
this.searchPipeline.remove(lunr.stemmer);
this.pipeline.add(OBU_STEMMER);
this.searchPipeline.add(OBU_STEMMER);
for (var key in store) {
var v = store[key];
this.add({
'id': key, 'title': utils.unaccented(v.title),
'author': v.authors.map(utils.unaccented).join(' '), 'content': utils.unaccented(v.content),
'translator': utils.unaccented(v.translator), 'format': v.formats.join(' '),
'tag': v.tags.join(' '), 'description': utils.unaccented(v.description),
'in': v.category, 'is': v.subcategory, 'type': v.type
}, {boost: v.boost});
}
});
}

function initSearchWorker(store) {
var idx = buildSearchIndex(store);
self.onmessage = function(e) {
self.postMessage(handleSearchMessage(e.data, idx.search.bind(idx)));
}
}
2 changes: 2 additions & 0 deletions scripts/minify.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ npx cleancss --batch --batch-suffix '' -O2 \
echo "Minifying Search JS..."
npx uglify-js $BUILD_DIR/assets/js/search_index.js -o $BUILD_DIR/assets/js/search_index.min.js -c -m
mv $BUILD_DIR/assets/js/search_index.min.js $BUILD_DIR/assets/js/search_index.js
npx uglify-js $BUILD_DIR/assets/js/search_worker_init.js -o $BUILD_DIR/assets/js/search_worker_init.min.js -c -m
mv $BUILD_DIR/assets/js/search_worker_init.min.js $BUILD_DIR/assets/js/search_worker_init.js
# The other js files are too small to need minification

echo "Done!"