64 lines
2.6 KiB
HTML
64 lines
2.6 KiB
HTML
|
|
<ul>
|
||
|
|
<div id="linkGen"></div>
|
||
|
|
</ul>
|
||
|
|
<script type="module">
|
||
|
|
import Fuse from 'https://cdn.jsdelivr.net/npm/fuse.js@7.0.0/dist/fuse.mjs'
|
||
|
|
// This page assumes that the user is already in a versioned subtree
|
||
|
|
// It is no longer the root 404 handler and is instead delagated to.
|
||
|
|
var localUri = window.location.pathname;
|
||
|
|
var hostUrl = window.location.protocol + "//" + window.location.host
|
||
|
|
|
||
|
|
let params = new URLSearchParams(document.location.search);
|
||
|
|
|
||
|
|
var rootPath = localUri.replace(/\/404_helper\.html/, "")
|
||
|
|
|
||
|
|
var rootUrl = hostUrl + rootPath
|
||
|
|
var pagelistUrl = rootUrl + "/pagelist.txt"
|
||
|
|
|
||
|
|
// // Branch is versioned if it leads with `unstable` or `vX.Y.Z`
|
||
|
|
// var isBranchVersioned = localUri.match(/^\/cccl\/(unstable|v\d+\.\d+\.\d+)/);
|
||
|
|
|
||
|
|
// // If the branch is not versioned, we redirect to a versioned branch keeping the URI.
|
||
|
|
// if (!isBranchVersioned) {
|
||
|
|
// window.location.href = hostUrl + '/' + localUri.replace(/^cccl\//g, "cccl/unstable");
|
||
|
|
// }
|
||
|
|
|
||
|
|
var searchString = params.get("path");
|
||
|
|
|
||
|
|
if (searchString === "/" || searchString === null) {
|
||
|
|
window.location.href = rootUrl + "/index.html"
|
||
|
|
}
|
||
|
|
|
||
|
|
if (searchString !== null) {
|
||
|
|
fetch(pagelistUrl)
|
||
|
|
.then((response) => response.text())
|
||
|
|
.then((text) => {
|
||
|
|
const pagelist = text.split(",")
|
||
|
|
|
||
|
|
const options = {
|
||
|
|
includeScore: true,
|
||
|
|
threshold: 0.8,
|
||
|
|
ignoreLocation: false
|
||
|
|
}
|
||
|
|
|
||
|
|
// cccl/device_vector.html => 'cccl 'device 'vector
|
||
|
|
console.log("query:" + searchString)
|
||
|
|
const fuse = new Fuse(pagelist, options)
|
||
|
|
// Include the ending token in the search to prioritize short matches
|
||
|
|
const result = fuse.search(searchString + "(end)", {limit: 10})
|
||
|
|
|
||
|
|
const listItem = '<li><a href=\"{2}{0}\">{0}</a> - Score: {1}</li>'.replace(/\{2\}/g, rootUrl)
|
||
|
|
document.getElementById('linkGen').innerHTML = ""
|
||
|
|
result.forEach(element => {
|
||
|
|
const targetUri = element.item.replace("(end)", "")
|
||
|
|
const targetScore = element.score.toFixed(2)
|
||
|
|
document.getElementById('linkGen').innerHTML += listItem.replace(/\{0\}/g, targetUri).replace(/\{1\}/g, targetScore)
|
||
|
|
// The match is near perfect, just link directly to it.
|
||
|
|
if (targetScore <= 0.02 ) {
|
||
|
|
window.location.href = rootUrl + targetUri
|
||
|
|
}
|
||
|
|
});
|
||
|
|
})
|
||
|
|
}
|
||
|
|
</script>
|