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">
|
<meta charset="UTF-8">
|
||||||
<link rel="stylesheet" href="/assets/styles.css" media="screen">
|
<link rel="stylesheet" href="/assets/styles.css" media="screen">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<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>
|
<title>neoBeta</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import lunr from 'lunr';
|
import elasticlunr from 'elasticlunr';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
||||||
let allPlugins = [];
|
let allPlugins = [];
|
||||||
|
|
@ -9,33 +9,29 @@ export default function (eleventyConfig) {
|
||||||
eleventyConfig.addPassthroughCopy("projects/**/*.jpg");
|
eleventyConfig.addPassthroughCopy("projects/**/*.jpg");
|
||||||
eleventyConfig.addPassthroughCopy("projects/**/*.jpeg");
|
eleventyConfig.addPassthroughCopy("projects/**/*.jpeg");
|
||||||
eleventyConfig.addPassthroughCopy("assets");
|
eleventyConfig.addPassthroughCopy("assets");
|
||||||
|
|
||||||
eleventyConfig.addCollection('searchIndex', (collectionApi) => {
|
eleventyConfig.addCollection('searchIndex', (collectionApi) => {
|
||||||
const result = collectionApi.getFilteredByTag("plugin").map(item => {
|
const result = collectionApi.getFilteredByTag("plugin").map(item => {
|
||||||
return {
|
return {
|
||||||
title: item.data.projectName,
|
title: item.data.projectName,
|
||||||
subtitle: item.data.projectSubtitle || "",
|
subtitle: item.data.projectSubtitle || "",
|
||||||
url: item.url
|
url: item.url
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
|
||||||
console.log("Collected " + result.length + " plugins for search index.");
|
|
||||||
console.log(result);
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
|
|
||||||
eleventyConfig.on('afterBuild', async (eleventyConfig) => {
|
|
||||||
lunrIndex = lunr(function () {
|
|
||||||
this.ref('url');
|
|
||||||
this.field('title');
|
|
||||||
this.field('subtitle');
|
|
||||||
|
|
||||||
allPlugins.forEach(function (doc) {
|
|
||||||
this.add(doc);
|
|
||||||
}, this);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const indexJson = lunrIndex.toJSON();
|
allPlugins = result;
|
||||||
fs.writeFileSync('./_site/search_index.json', JSON.stringify(indexJson));
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
eleventyConfig.on('afterBuild', () => {
|
||||||
|
const idx = elasticlunr(function () {
|
||||||
|
this.setRef('url');
|
||||||
|
this.addField('title', { boost: 2 });
|
||||||
|
this.addField('subtitle');
|
||||||
|
|
||||||
|
allPlugins.forEach(doc => this.addDoc(doc));
|
||||||
|
});
|
||||||
|
|
||||||
|
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",
|
"license": "Unlicense",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@11ty/eleventy": "^3.1.2",
|
"@11ty/eleventy": "^3.1.2",
|
||||||
"lunr": "^2.3.9"
|
"elasticlunr": "^0.9.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@11ty/dependency-tree": {
|
"node_modules/@11ty/dependency-tree": {
|
||||||
|
|
@ -410,7 +410,6 @@
|
||||||
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz",
|
||||||
"integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==",
|
"integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"peer": true,
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"anymatch": "~3.1.2",
|
"anymatch": "~3.1.2",
|
||||||
"braces": "~3.0.2",
|
"braces": "~3.0.2",
|
||||||
|
|
@ -550,6 +549,12 @@
|
||||||
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
|
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
|
||||||
"license": "MIT"
|
"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": {
|
"node_modules/encodeurl": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz",
|
||||||
|
|
@ -1032,12 +1037,6 @@
|
||||||
"integrity": "sha512-+dAZZ2mM+/m+vY9ezfoueVvrgnHIGi5FvgSymbIgJOFwiznWyA59mav95L+Mc6xPtL3s9gm5eNTlNtxJLbNM1g==",
|
"integrity": "sha512-+dAZZ2mM+/m+vY9ezfoueVvrgnHIGi5FvgSymbIgJOFwiznWyA59mav95L+Mc6xPtL3s9gm5eNTlNtxJLbNM1g==",
|
||||||
"license": "MIT"
|
"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": {
|
"node_modules/luxon": {
|
||||||
"version": "3.7.2",
|
"version": "3.7.2",
|
||||||
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.7.2.tgz",
|
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.7.2.tgz",
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,6 @@
|
||||||
"homepage": "https://github.com/adrianvic/neoBeta#readme",
|
"homepage": "https://github.com/adrianvic/neoBeta#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@11ty/eleventy": "^3.1.2",
|
"@11ty/eleventy": "^3.1.2",
|
||||||
"lunr": "^2.3.9"
|
"elasticlunr": "^0.9.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
51
search.html
51
search.html
|
|
@ -5,32 +5,33 @@ layout: "base.njk"
|
||||||
<input type="text" id="search" placeholder="Search..." />
|
<input type="text" id="search" placeholder="Search..." />
|
||||||
<ul id="results"></ul>
|
<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>
|
<script>
|
||||||
fetch('search.json')
|
fetch('/search_index.json')
|
||||||
.then(response => response.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
const idx = lunr.Index.load(data.index);
|
const idx = elasticlunr.Index.load(data);
|
||||||
const documents = data.documents;
|
const docs = idx.documentStore.docs;
|
||||||
|
const searchInput = document.getElementById('search');
|
||||||
const searchInput = document.getElementById('search');
|
const out = document.getElementById('results');
|
||||||
|
|
||||||
searchInput.addEventListener('input', function () {
|
searchInput.addEventListener('input', function () {
|
||||||
const query = this.value;
|
const q = this.value.trim();
|
||||||
let results = [];
|
if (!q) {
|
||||||
|
out.innerHTML = '';
|
||||||
// Perform search
|
return;
|
||||||
if (query.length > 0) {
|
}
|
||||||
results = idx.search(query).map(result => {
|
|
||||||
const document = documents.find(doc => doc.id === result.ref);
|
const items = idx.search(q).map(r => {
|
||||||
return `<li><a href="${document.url}">${document.title}</a></li>`;
|
const doc = docs[r.ref];
|
||||||
});
|
return `<li><a href="${doc.url}">${doc.title}</a></li>`;
|
||||||
|
|
||||||
// Display results
|
|
||||||
document.getElementById('results').innerHTML = results.join('');
|
|
||||||
} else {
|
|
||||||
document.getElementById('results').innerHTML = '';
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
out.innerHTML = items.join('');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue