-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from hyrodium/feature/search
Add search box
- Loading branch information
Showing
40 changed files
with
380 additions
and
19 deletions.
There are no files selected for viewing
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
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,103 @@ | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
var lunr = require("lunr"); | ||
var cheerio = require("cheerio"); | ||
|
||
// don't modify this, it'll be modified on the fly by lunr() in Franklin | ||
const PATH_PREPEND = ".."; | ||
|
||
const HTML_FOLDER = "../../__site"; | ||
const OUTPUT_INDEX = "lunr_index.js"; | ||
|
||
function isHtml(filename) { | ||
lower = filename.toLowerCase(); | ||
return (lower.endsWith(".htm") || lower.endsWith(".html")); | ||
} | ||
|
||
function findHtml(folder) { | ||
if (!fs.existsSync(folder)) { | ||
console.log("Could not find folder: ", folder); | ||
return; | ||
} | ||
var files = fs.readdirSync(folder); | ||
var htmls = []; | ||
for (var i = 0; i < files.length; i++) { | ||
var filename = path.join(folder, files[i]); | ||
var stat = fs.lstatSync(filename); | ||
if (stat.isDirectory()) { | ||
if (stat == "assets" || stat == "css" || stat == "libs" ) { | ||
continue | ||
} | ||
var recursed = findHtml(filename); | ||
for (var j = 0; j < recursed.length; j++) { | ||
recursed[j] = path.join(files[i], recursed[j]).replace(/\\/g, "/"); | ||
} | ||
htmls.push.apply(htmls, recursed); | ||
} | ||
else if (isHtml(filename)){ | ||
htmls.push(files[i]); | ||
}; | ||
}; | ||
return htmls; | ||
}; | ||
|
||
function readHtml(root, file, fileId) { | ||
var filename = path.join(root, file); | ||
var txt = fs.readFileSync(filename).toString(); | ||
var $ = cheerio.load(txt); | ||
var title = $("title").text(); | ||
if (typeof title == 'undefined') title = file; | ||
var body = $("body").text() | ||
if (typeof body == 'undefined') body = ""; | ||
|
||
var data = { | ||
"id": fileId, | ||
"l": filename, | ||
"t": title, | ||
"b": body | ||
} | ||
return data; | ||
} | ||
|
||
function buildIndex(docs) { | ||
var idx = lunr(function () { | ||
this.ref('id'); | ||
this.field('t'); // title | ||
this.field('b'); // body | ||
docs.forEach(function (doc) { | ||
this.add(doc); | ||
}, this); | ||
}); | ||
return idx; | ||
} | ||
|
||
function buildPreviews(docs) { | ||
var result = {}; | ||
for (var i = 0; i < docs.length; i++) { | ||
var doc = docs[i]; | ||
result[doc["id"]] = { | ||
"t": doc["t"], | ||
"l": doc["l"].replace(/^\.\.\/\.\.\/__site/gi, '/' + PATH_PREPEND) | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
function main() { | ||
files = findHtml(HTML_FOLDER); | ||
var docs = []; | ||
for (var i = 0; i < files.length; i++) { | ||
docs.push(readHtml(HTML_FOLDER, files[i], i)); | ||
} | ||
var idx = buildIndex(docs); | ||
var prev = buildPreviews(docs); | ||
var js = "const LUNR_DATA = " + JSON.stringify(idx) + ";\n" + | ||
"const PREVIEW_LOOKUP = " + JSON.stringify(prev) + ";"; | ||
fs.writeFile(OUTPUT_INDEX, js, function(err) { | ||
if(err) { | ||
return console.log(err); | ||
} | ||
}); | ||
} | ||
|
||
main(); |
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,88 @@ | ||
// This file and its minified version is adapted from https://github.com/BLE-LTER/Lunr-Index-and-Search-for-Static-Sites which is unlicensed. | ||
// | ||
|
||
"use strict"; | ||
|
||
var LUNR_CONFIG = { | ||
"resultsElementId": "searchResults", // Element to contain results | ||
"countElementId": "resultCount" // Element showing number of results | ||
}; | ||
|
||
|
||
// Get URL arguments | ||
function getParameterByName(name) { | ||
var url = window.location.href; | ||
name = name.replace(/[\[\]]/g, "\\$&"); | ||
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | ||
results = regex.exec(url); | ||
if (!results) return null; | ||
if (!results[2]) return ""; | ||
return decodeURIComponent(results[2].replace(/\+/g, " ")); | ||
} | ||
|
||
|
||
// Parse search results into HTML | ||
function parseLunrResults(results) { | ||
var html = []; | ||
for (var i = 0; i < results.length; i++) { | ||
var id = results[i]["ref"]; | ||
var item = PREVIEW_LOOKUP[id] | ||
var title = item["t"]; | ||
var preview = item["p"]; | ||
var link = item["l"].replace("__site/", ""); | ||
var result = ('<li><span class="result-title"><a href="' + link + '">' | ||
+ title + '</a></span>'); | ||
html.push(result); | ||
} | ||
if (html.length) { | ||
html.join(""); | ||
return '<ul>'+html+'</ul>' | ||
} | ||
else { | ||
return ""; | ||
} | ||
} | ||
|
||
|
||
function escapeHtml(unsafe) { | ||
return unsafe | ||
.replace(/&/g, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/"/g, """) | ||
.replace(/'/g, "'"); | ||
} | ||
|
||
|
||
function showResultCount(total) { | ||
var element = document.getElementById(LUNR_CONFIG["countElementId"]) | ||
if (element !== null) { | ||
element.innerHTML = total + "."; | ||
} | ||
} | ||
|
||
|
||
function searchLunr(query) { | ||
var idx = lunr.Index.load(LUNR_DATA); | ||
// Write results to page | ||
var results = idx.search(query); | ||
var resultHtml = parseLunrResults(results); | ||
var elementId = LUNR_CONFIG["resultsElementId"]; | ||
document.getElementById(elementId).innerHTML = resultHtml; | ||
// Write the number of results | ||
showResultCount(results.length); | ||
} | ||
|
||
|
||
// When the window loads, read query parameters and perform search | ||
window.onload = function() { | ||
var query = getParameterByName("q"); | ||
if (query != "" && query != null) { | ||
document.forms.lunrSearchForm.q.value = query; | ||
searchLunr(query); | ||
} else { | ||
// empty query: show 0 results (no query) | ||
showResultCount("0 (empty query)"); | ||
} | ||
document.getElementById("focus").focus(); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.