forked from getzola/zola
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vincent Prouillet
committed
Mar 20, 2018
1 parent
f1abbd0
commit ddf8970
Showing
14 changed files
with
212 additions
and
1,739 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,5 @@ members = [ | |
"components/taxonomies", | ||
"components/templates", | ||
"components/utils", | ||
"components/search", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "search" | ||
version = "0.1.0" | ||
authors = ["Vincent Prouillet <[email protected]>"] | ||
|
||
[dependencies] | ||
elasticlunr-rs = "1" | ||
ammonia = "1" | ||
lazy_static = "1" | ||
|
||
errors = { path = "../errors" } | ||
content = { path = "../content" } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
extern crate elasticlunr; | ||
#[macro_use] | ||
extern crate lazy_static; | ||
extern crate ammonia; | ||
|
||
extern crate errors; | ||
extern crate content; | ||
|
||
use std::collections::{HashMap, HashSet}; | ||
use std::path::PathBuf; | ||
|
||
use elasticlunr::Index; | ||
use content::Section; | ||
|
||
|
||
pub const ELASTICLUNR_JS: &'static str = include_str!("elasticlunr.min.js"); | ||
|
||
lazy_static! { | ||
static ref AMMONIA: ammonia::Builder<'static> = { | ||
let mut clean_content = HashSet::new(); | ||
clean_content.insert("script"); | ||
clean_content.insert("style"); | ||
let mut builder = ammonia::Builder::new(); | ||
builder | ||
.tags(HashSet::new()) | ||
.tag_attributes(HashMap::new()) | ||
.generic_attributes(HashSet::new()) | ||
.link_rel(None) | ||
.allowed_classes(HashMap::new()) | ||
.clean_content_tags(clean_content); | ||
builder | ||
}; | ||
} | ||
|
||
|
||
/// Returns the generated JSON index with all the documents of the site added | ||
/// TODO: is making `in_search_index` apply to subsections of a `false` section useful? | ||
pub fn build_index(sections: &HashMap<PathBuf, Section>) -> String { | ||
let mut index = Index::new(&["title", "body"]); | ||
|
||
for section in sections.values() { | ||
add_section_to_index(&mut index, section); | ||
} | ||
|
||
index.to_json() | ||
} | ||
|
||
fn add_section_to_index(index: &mut Index, section: &Section) { | ||
if !section.meta.in_search_index { | ||
return; | ||
} | ||
|
||
// Don't index redirecting sections | ||
if section.meta.redirect_to.is_none() { | ||
index.add_doc( | ||
§ion.permalink, | ||
&[§ion.meta.title.clone().unwrap_or(String::new()), &AMMONIA.clean(§ion.content).to_string()], | ||
); | ||
} | ||
|
||
for page in §ion.pages { | ||
if !page.meta.in_search_index { | ||
continue; | ||
} | ||
|
||
index.add_doc( | ||
&page.permalink, | ||
&[&page.meta.title.clone().unwrap_or(String::new()), &AMMONIA.clean(&page.content).to_string()], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.search-results { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ $link-color: #007CBC; | |
@import "index"; | ||
@import "docs"; | ||
@import "themes"; | ||
@import "search"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
function formatSearchResultHeader(term, count) { | ||
if (count === 0) { | ||
return "No search results for '" + term + "'."; | ||
} | ||
|
||
return count + " search result" + count > 1 ? "s" : "" + " for '" + term + "':"; | ||
} | ||
|
||
function formatSearchResultItem(term, item) { | ||
console.log(item); | ||
return '<div class="search-results__item">' | ||
+ item | ||
+ '</div>'; | ||
} | ||
|
||
function initSearch() { | ||
var $searchInput = document.getElementById("search"); | ||
var $searchResults = document.querySelector(".search-results"); | ||
var $searchResultsHeader = document.querySelector(".search-results__headers"); | ||
var $searchResultsItems = document.querySelector(".search-results__items"); | ||
|
||
var options = { | ||
bool: "AND", | ||
expand: true, | ||
teaser_word_count: 30, | ||
limit_results: 30, | ||
fields: { | ||
title: {boost: 2}, | ||
body: {boost: 1}, | ||
} | ||
}; | ||
var currentTerm = ""; | ||
var index = elasticlunr.Index.load(window.searchIndex); | ||
|
||
$searchInput.addEventListener("keyup", function() { | ||
var term = $searchInput.value.trim(); | ||
if (!index || term === "" || term === currentTerm) { | ||
return; | ||
} | ||
$searchResults.style.display = term === "" ? "block" : "none"; | ||
$searchResultsItems.innerHTML = ""; | ||
var results = index.search(term, options); | ||
currentTerm = term; | ||
$searchResultsHeader.textContent = searchResultText(term, results.length); | ||
for (var i = 0; i < results.length; i++) { | ||
var item = document.createElement("li"); | ||
item.innerHTML = formatSearchResult(results[i], term); | ||
$searchResultsItems.appendChild(item); | ||
} | ||
}); | ||
} | ||
|
||
|
||
if (document.readyState === "complete" || | ||
(document.readyState !== "loading" && !document.documentElement.doScroll) | ||
) { | ||
initSearch(); | ||
} else { | ||
document.addEventListener("DOMContentLoaded", initSearch); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters