Barebones search working!
This commit is contained in:
parent
26db9488db
commit
205a096654
5 changed files with 58 additions and 64 deletions
|
|
@ -4,8 +4,6 @@
|
|||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" href="/assets/styles.css" media="screen">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<script src="project.js"></script>
|
||||
<script src="featured.js" defer></script>
|
||||
<title>neoBeta</title>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import lunr from 'lunr';
|
||||
import elasticlunr from 'elasticlunr';
|
||||
import fs from 'fs';
|
||||
|
||||
let allPlugins = [];
|
||||
|
|
@ -19,23 +19,19 @@ export default function (eleventyConfig) {
|
|||
};
|
||||
});
|
||||
|
||||
console.log("Collected " + result.length + " plugins for search index.");
|
||||
console.log(result);
|
||||
allPlugins = result;
|
||||
return result;
|
||||
});
|
||||
|
||||
eleventyConfig.on('afterBuild', async (eleventyConfig) => {
|
||||
lunrIndex = lunr(function () {
|
||||
this.ref('url');
|
||||
this.field('title');
|
||||
this.field('subtitle');
|
||||
eleventyConfig.on('afterBuild', () => {
|
||||
const idx = elasticlunr(function () {
|
||||
this.setRef('url');
|
||||
this.addField('title', { boost: 2 });
|
||||
this.addField('subtitle');
|
||||
|
||||
allPlugins.forEach(function (doc) {
|
||||
this.add(doc);
|
||||
}, this);
|
||||
allPlugins.forEach(doc => this.addDoc(doc));
|
||||
});
|
||||
|
||||
const indexJson = lunrIndex.toJSON();
|
||||
fs.writeFileSync('./_site/search_index.json', JSON.stringify(indexJson));
|
||||
fs.writeFileSync('./_site/search_index.json', JSON.stringify(idx));
|
||||
});
|
||||
};
|
||||
|
|
|
|||
15
package-lock.json
generated
15
package-lock.json
generated
|
|
@ -10,7 +10,7 @@
|
|||
"license": "Unlicense",
|
||||
"dependencies": {
|
||||
"@11ty/eleventy": "^3.1.2",
|
||||
"lunr": "^2.3.9"
|
||||
"elasticlunr": "^0.9.5"
|
||||
}
|
||||
},
|
||||
"node_modules/@11ty/dependency-tree": {
|
||||
|
|
@ -410,7 +410,6 @@
|
|||
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz",
|
||||
"integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"anymatch": "~3.1.2",
|
||||
"braces": "~3.0.2",
|
||||
|
|
@ -550,6 +549,12 @@
|
|||
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/elasticlunr": {
|
||||
"version": "0.9.5",
|
||||
"resolved": "https://registry.npmjs.org/elasticlunr/-/elasticlunr-0.9.5.tgz",
|
||||
"integrity": "sha512-5YM9LFQgVYfuLNEoqMqVWIBuF2UNCA+xu/jz1TyryLN/wmBcQSb+GNAwvLKvEpGESwgGN8XA1nbLAt6rKlyHYQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/encodeurl": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz",
|
||||
|
|
@ -1032,12 +1037,6 @@
|
|||
"integrity": "sha512-+dAZZ2mM+/m+vY9ezfoueVvrgnHIGi5FvgSymbIgJOFwiznWyA59mav95L+Mc6xPtL3s9gm5eNTlNtxJLbNM1g==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/lunr": {
|
||||
"version": "2.3.9",
|
||||
"resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz",
|
||||
"integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/luxon": {
|
||||
"version": "3.7.2",
|
||||
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.7.2.tgz",
|
||||
|
|
|
|||
|
|
@ -20,6 +20,6 @@
|
|||
"homepage": "https://github.com/adrianvic/neoBeta#readme",
|
||||
"dependencies": {
|
||||
"@11ty/eleventy": "^3.1.2",
|
||||
"lunr": "^2.3.9"
|
||||
"elasticlunr": "^0.9.5"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
41
search.html
41
search.html
|
|
@ -5,32 +5,33 @@ layout: "base.njk"
|
|||
<input type="text" id="search" placeholder="Search..." />
|
||||
<ul id="results"></ul>
|
||||
|
||||
<script src="https://unpkg.com/lunr/lunr.js"></script>
|
||||
<script
|
||||
type="text/javascript"
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/elasticlunr/0.9.6/elasticlunr.min.js"
|
||||
></script>
|
||||
<script>
|
||||
fetch('search.json')
|
||||
.then(response => response.json())
|
||||
fetch('/search_index.json')
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
const idx = lunr.Index.load(data.index);
|
||||
const documents = data.documents;
|
||||
|
||||
const idx = elasticlunr.Index.load(data);
|
||||
const docs = idx.documentStore.docs;
|
||||
const searchInput = document.getElementById('search');
|
||||
const out = document.getElementById('results');
|
||||
|
||||
searchInput.addEventListener('input', function () {
|
||||
const query = this.value;
|
||||
let results = [];
|
||||
|
||||
// Perform search
|
||||
if (query.length > 0) {
|
||||
results = idx.search(query).map(result => {
|
||||
const document = documents.find(doc => doc.id === result.ref);
|
||||
return `<li><a href="${document.url}">${document.title}</a></li>`;
|
||||
});
|
||||
|
||||
// Display results
|
||||
document.getElementById('results').innerHTML = results.join('');
|
||||
} else {
|
||||
document.getElementById('results').innerHTML = '';
|
||||
const q = this.value.trim();
|
||||
if (!q) {
|
||||
out.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const items = idx.search(q).map(r => {
|
||||
const doc = docs[r.ref];
|
||||
return `<li><a href="${doc.url}">${doc.title}</a></li>`;
|
||||
});
|
||||
|
||||
out.innerHTML = items.join('');
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue