Skip to content

Commit

Permalink
feat: use fzf to search
Browse files Browse the repository at this point in the history
Using fzf supports CJK languages
  • Loading branch information
duskmoon314 committed Jan 24, 2024
1 parent 51efaf2 commit d04462e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn create_files(search_config: &Search, destination: &Path, book: &Book) ->
utils::fs::write_file(destination, "searcher.js", searcher::JS)?;
utils::fs::write_file(destination, "mark.min.js", searcher::MARK_JS)?;
utils::fs::write_file(destination, "elasticlunr.min.js", searcher::ELASTICLUNR_JS)?;
utils::fs::write_file(destination, "fzf.umd.min.js", searcher::FZF_JS)?;
debug!("Copying search files ✓");
}

Expand Down
1 change: 1 addition & 0 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
{{#if search_js}}
<script src="{{ path_to_root }}elasticlunr.min.js"></script>
<script src="{{ path_to_root }}mark.min.js"></script>
<script src="{{ path_to_root }}fzf.umd.min.js"></script>
<script src="{{ path_to_root }}searcher.js"></script>
{{/if}}

Expand Down
7 changes: 7 additions & 0 deletions src/theme/searcher/fzf.umd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/theme/searcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
pub static JS: &[u8] = include_bytes!("searcher.js");
pub static MARK_JS: &[u8] = include_bytes!("mark.min.js");
pub static ELASTICLUNR_JS: &[u8] = include_bytes!("elasticlunr.min.js");
pub static FZF_JS: &[u8] = include_bytes!("fzf.umd.min.js");
33 changes: 31 additions & 2 deletions src/theme/searcher/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ window.search = window.search || {};
// You can use !hasFocus() to prevent keyhandling in your key
// event handlers while the user is typing their search.

if (!Mark || !elasticlunr) {
const Fzf = window.fzf.Fzf;

if (!Mark || !elasticlunr || !Fzf) {
return;
}

Expand Down Expand Up @@ -252,12 +254,39 @@ window.search = window.search || {};
return teaser_split.join('');
}

function fzfLoad(index) {
const docs = index.documentStore.docs;
const fzf = new Fzf(Object.keys(docs), {
selector: (id) => {
const doc = docs[id];
return `${doc.title} ${doc.breadcrumbs} ${doc.body}`
}
});

return {
search: (term, _options) => {
const entries = fzf.find(term);
const res = entries.map((entry) => {
// TODO: use score to rank results
const { item, _score } = entry;
return {
doc: docs[item],
ref: item,
}
});
return res;
}
}
}

function init(config) {
results_options = config.results_options;
search_options = config.search_options;
searchbar_outer = config.searchbar_outer;
doc_urls = config.doc_urls;
searchindex = elasticlunr.Index.load(config.index);
// searchindex = elasticlunr.Index.load(config.index);
searchindex = fzfLoad(config.index);


// Set up events
searchicon.addEventListener('click', function(e) { searchIconClickHandler(); }, false);
Expand Down

0 comments on commit d04462e

Please sign in to comment.