begin rewrite
This commit is contained in:
2255
js/fuse.js
Normal file
2255
js/fuse.js
Normal file
File diff suppressed because it is too large
Load Diff
75
js/indexsearch.js
Normal file
75
js/indexsearch.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const HEADERS = "headers"
|
||||
const PATH = "path"
|
||||
const TAGS = "tags"
|
||||
const TITLE = "title"
|
||||
|
||||
const SEARCH_TIMEOUT_MS = 100
|
||||
var SEARCH_TIMEOUT_ID = -1
|
||||
|
||||
const fuse = new Fuse(data, {
|
||||
keys: [ 'title' ],
|
||||
ignoreLocation: true,
|
||||
threshhold: 0.4,
|
||||
minMatchCharLength: 0,
|
||||
})
|
||||
|
||||
const RESULTS_MAX = 15
|
||||
|
||||
const searchBar = document.getElementById('search')
|
||||
const resultsDiv = document.getElementById('searchResults')
|
||||
|
||||
var results = []
|
||||
|
||||
function updateResultsWithTimeout() {
|
||||
console.log("clearing timeout")
|
||||
if (SEARCH_TIMEOUT_ID) SEARCH_TIMEOUT_ID = clearTimeout(SEARCH_TIMEOUT_ID)
|
||||
SEARCH_TIMEOUT_ID = setTimeout(updateResults, SEARCH_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
function updateResults() {
|
||||
console.log("updating results")
|
||||
resultsDiv.innerHTML = ''
|
||||
if (searchBar.value) results = fuse.search(searchBar.value, { limit: RESULTS_MAX }).map(r => r.item)
|
||||
else results = data
|
||||
|
||||
results.forEach(r => {
|
||||
wrapper = document.createElement('li')
|
||||
wrapper.className = "article"
|
||||
|
||||
atag = document.createElement('a')
|
||||
atag.href = r.path
|
||||
|
||||
ptag = document.createElement('p')
|
||||
ptag.innerHTML = r.title + (r.isdirectory ? '/' : '')
|
||||
|
||||
atag.appendChild(ptag)
|
||||
wrapper.appendChild(atag)
|
||||
resultsDiv.appendChild(wrapper)
|
||||
})
|
||||
}
|
||||
|
||||
searchBar.addEventListener('keyup', e => {
|
||||
console.log(e)
|
||||
// if user pressed enter
|
||||
if (e.keyCode === 13) {
|
||||
if (e.shiftKey) {
|
||||
window.open(results[0].path, '_blank')
|
||||
} else {
|
||||
window.location.href = results[0].path
|
||||
}
|
||||
return
|
||||
}
|
||||
updateResultsWithTimeout()
|
||||
})
|
||||
|
||||
searchBar.addEventListener('change', updateResultsWithTimeout)
|
||||
|
||||
const searchParams = new URL(window.location.href).searchParams;
|
||||
searchBar.value = searchParams.get('q');
|
||||
updateResults();
|
||||
|
||||
console.log(results)
|
||||
|
||||
if (searchParams.has('lucky')) {
|
||||
window.location.href = results[0].path;
|
||||
}
|
8
js/permalink.js
Normal file
8
js/permalink.js
Normal file
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const MANUAL_REDIRECT = document.getElementById('manual_redirect');
|
||||
|
||||
const newLocation = data[new URLSearchParams(window.location.search).get('uuid')];
|
||||
|
||||
MANUAL_REDIRECT.href = newLocation;
|
||||
window.location = newLocation;
|
135
js/search.js
Normal file
135
js/search.js
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* search.js expects an array `data` containing objects with the following keys:
|
||||
* headers: [string]
|
||||
* path: string
|
||||
* tags: [string]
|
||||
* title: string
|
||||
* uuid: string
|
||||
*/
|
||||
|
||||
|
||||
const HEADERS = "headers"
|
||||
const PATH = "path"
|
||||
const TAGS = "tags"
|
||||
const TITLE = "title"
|
||||
|
||||
const SEARCH_TIMEOUT_MS = 100
|
||||
var SEARCH_TIMEOUT_ID = -1
|
||||
|
||||
const fuse = new Fuse(search_data, {
|
||||
keys: [
|
||||
{
|
||||
name: HEADERS,
|
||||
weight: 1
|
||||
},
|
||||
{
|
||||
name: PATH,
|
||||
weight: 0.1
|
||||
},
|
||||
{
|
||||
name: TAGS,
|
||||
weight: 0.5
|
||||
},
|
||||
{
|
||||
name: TITLE,
|
||||
weight: 2
|
||||
}
|
||||
],
|
||||
includeMatches: true,
|
||||
useExtendedSearch: true,
|
||||
ignoreLocation: true,
|
||||
threshhold: 0.4,
|
||||
})
|
||||
|
||||
const RESULTS_MAX = 5
|
||||
|
||||
const searchBar = document.getElementById('search')
|
||||
const resultsDiv = document.getElementById('results')
|
||||
|
||||
var results = []
|
||||
|
||||
function updateResultsWithTimeout() {
|
||||
console.log("clearing timeout")
|
||||
if (SEARCH_TIMEOUT_ID) SEARCH_TIMEOUT_ID = clearTimeout(SEARCH_TIMEOUT_ID)
|
||||
SEARCH_TIMEOUT_ID = setTimeout(updateResults, SEARCH_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
function updateResults() {
|
||||
console.log("updating results")
|
||||
resultsDiv.innerHTML = ''
|
||||
results = fuse.search(searchBar.value, { limit: RESULTS_MAX })
|
||||
results.forEach(r => {
|
||||
wrapper = document.createElement('div')
|
||||
wrapper.className = "article"
|
||||
|
||||
display_matches = {}
|
||||
display_matches[HEADERS] = []
|
||||
display_matches[PATH] = []
|
||||
display_matches[TAGS] = []
|
||||
display_matches[TITLE] = []
|
||||
|
||||
r.matches.every(match => {
|
||||
if (display_matches[match.key].length > 3) {
|
||||
display_matches[match.key].push('...')
|
||||
return false
|
||||
}
|
||||
|
||||
display_match = match.value
|
||||
if (match.indices.length >= 1) {
|
||||
match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0]))
|
||||
indexPair = match.indices[0]
|
||||
matching_slice = match.value.slice(indexPair[0], indexPair[1]+1)
|
||||
display_match = match.value.replace(
|
||||
matching_slice,
|
||||
'<span class="matchHighlight">' + matching_slice + '</span>'
|
||||
)
|
||||
}
|
||||
display_matches[match.key].push(display_match)
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
content = document.createElement('a')
|
||||
content.innerHTML = r.item.title
|
||||
content.href = r.item.path
|
||||
|
||||
wrapper.appendChild(content)
|
||||
|
||||
Object.keys(display_matches).forEach(key => {
|
||||
if (display_matches[key].length < 1) return
|
||||
|
||||
p = document.createElement('p')
|
||||
p.className = "smallText"
|
||||
p.innerHTML += key + ": [" + display_matches[key].join(', ') + ']'
|
||||
wrapper.appendChild(p)
|
||||
})
|
||||
|
||||
resultsDiv.appendChild(wrapper)
|
||||
})
|
||||
}
|
||||
|
||||
searchBar.addEventListener('keyup', e => {
|
||||
console.log(e)
|
||||
// if user pressed enter
|
||||
if (e.keyCode === 13) {
|
||||
if (e.shiftKey) {
|
||||
window.open(results[0].item.path, '_blank')
|
||||
} else {
|
||||
window.location.href = results[0].item.path
|
||||
}
|
||||
return
|
||||
}
|
||||
updateResultsWithTimeout()
|
||||
})
|
||||
|
||||
searchBar.addEventListener('change', updateResultsWithTimeout)
|
||||
|
||||
const searchParams = new URL(window.location.href).searchParams;
|
||||
searchBar.value = searchParams.get('q');
|
||||
updateResults();
|
||||
|
||||
console.log(results)
|
||||
|
||||
if (searchParams.has('lucky')) {
|
||||
window.location.href = results[0].item.path;
|
||||
}
|
94
js/toc_search.js
Normal file
94
js/toc_search.js
Normal file
@@ -0,0 +1,94 @@
|
||||
'use strict';
|
||||
|
||||
var raw_html_tree = document.getElementById('toc').firstChild.cloneNode(true);
|
||||
|
||||
function createSearchable(el) {
|
||||
var par = el.parentElement;
|
||||
var obj = el.cloneNode(true);
|
||||
|
||||
while(par != raw_html_tree) {
|
||||
var clone_parent = par.cloneNode(true);
|
||||
|
||||
while (clone_parent.firstChild != clone_parent.lastChild) {
|
||||
clone_parent.removeChild(clone_parent.lastChild);
|
||||
}
|
||||
console.log("obj.innerHTML: " + obj.innerHTML);
|
||||
console.log("clone_parent.firstChild.innerHTML: " + clone_parent.firstChild.innerHTML);
|
||||
if (obj.innerHTML != clone_parent.firstChild.innerHTML)
|
||||
clone_parent.appendChild(obj);
|
||||
|
||||
obj = clone_parent;
|
||||
par = par.parentElement;
|
||||
}
|
||||
|
||||
return {
|
||||
searchable: el.innerHTML,
|
||||
obj: obj
|
||||
};
|
||||
}
|
||||
|
||||
var searchables = [];
|
||||
Array(...raw_html_tree.getElementsByTagName('a'))
|
||||
.forEach(el => searchables.push(createSearchable(el)));
|
||||
|
||||
var fuse = new Fuse(searchables, { keys: [ 'searchable' ], includeMatches: true});
|
||||
var searchBar = document.getElementById('search');
|
||||
var resultsDiv = document.getElementById('toc');
|
||||
|
||||
function updateResults() {
|
||||
var ul = document.createElement('ul');
|
||||
resultsDiv.innerHTML = '';
|
||||
if (searchBar.value == '') {
|
||||
resultsDiv.appendChild(raw_html_tree);
|
||||
return;
|
||||
}
|
||||
var results = fuse.search(searchBar.value);
|
||||
|
||||
|
||||
results.forEach(r => {
|
||||
console.log(r)
|
||||
var content = r.item.obj
|
||||
var last_a = Array.from(r.item.obj.getElementsByTagName('a')).pop()
|
||||
|
||||
r.matches.reverse().every(match => {
|
||||
var display_match = match.value;
|
||||
if (match.indices.length >= 1) {
|
||||
match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0]));
|
||||
const indexPair = match.indices[0];
|
||||
const matching_slice = match.value.slice(indexPair[0], indexPair[1]+1);
|
||||
last_a.innerHTML = match.value.replace(
|
||||
matching_slice,
|
||||
'<span class="matchHighlight">' + matching_slice + '</span>'
|
||||
);
|
||||
}
|
||||
return true;
|
||||
})
|
||||
|
||||
ul.appendChild(content);
|
||||
ul.appendChild(document.createElement('br'));
|
||||
})
|
||||
resultsDiv.appendChild(ul);
|
||||
}
|
||||
|
||||
searchBar.addEventListener('keyup', e => {
|
||||
// if user pressed enter
|
||||
if (e.keyCode === 13) {
|
||||
if (e.shiftKey) {
|
||||
window.open(results[0].item.path, '_blank');
|
||||
} else {
|
||||
window.location.href = results[0].item.path;
|
||||
}
|
||||
return;
|
||||
}
|
||||
updateResults();
|
||||
})
|
||||
|
||||
searchBar.addEventListener('change', updateResults);
|
||||
|
||||
const searchParams = new URL(window.location.href).searchParams;
|
||||
searchBar.value = searchParams.get('q');
|
||||
updateResults();
|
||||
|
||||
if (searchParams.has('lucky')) {
|
||||
window.location.href = results[0].item.path;
|
||||
}
|
Reference in New Issue
Block a user