Add page indexer script
This commit is contained in:
parent
8b469eeadd
commit
4d0799668f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.hugo_build.lock
|
||||
resources/_gen/
|
||||
static/js/lunr/PagesIndex.json
|
||||
|
||||
node_modules/
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import clickableMaker from "./clickable-maker";
|
||||
import _ from "./lunr";
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
clickableMaker();
|
||||
|
||||
21
assets/js/lunr.js
Normal file
21
assets/js/lunr.js
Normal file
@ -0,0 +1,21 @@
|
||||
const lunr = require('lunr');
|
||||
|
||||
fetch("/js/lunr/PagesIndex.json")
|
||||
.then(pagesIndex => {
|
||||
const lunrIndex = lunr(function() {
|
||||
this.field('title', {
|
||||
boost: 10,
|
||||
});
|
||||
this.field('tags', {
|
||||
boost: 5,
|
||||
});
|
||||
this.field('content');
|
||||
|
||||
this.ref('href');
|
||||
});
|
||||
|
||||
pagesIndex.forEach(page => {
|
||||
lunrIndex.add(page);
|
||||
});
|
||||
})
|
||||
;
|
||||
76
docker/prod-host/search-indexer.js
Normal file
76
docker/prod-host/search-indexer.js
Normal file
@ -0,0 +1,76 @@
|
||||
const fs = require('fs');
|
||||
const S = require('string');
|
||||
const path = require('path');
|
||||
|
||||
const CONTENT_PATH_PREFIX = 'content/'
|
||||
const OUTPUT_PATH = "static/js/lunr/PagesIndex.json"
|
||||
|
||||
const getTree = dir => {
|
||||
const allFiles = [];
|
||||
|
||||
const items = fs.readdirSync(dir);
|
||||
items.forEach(item => {
|
||||
const itemPath = path.join(dir, item);
|
||||
const stats = fs.statSync(itemPath);
|
||||
if (stats.isDirectory()) {
|
||||
allFiles.push(...getTree(itemPath));
|
||||
} else {
|
||||
allFiles.push(itemPath);
|
||||
}
|
||||
});
|
||||
return allFiles;
|
||||
}
|
||||
|
||||
const pageIndices = getTree(CONTENT_PATH_PREFIX)
|
||||
.map(file => {
|
||||
const href = S(file).chompLeft(CONTENT_PATH_PREFIX).chompRight('.md').s;
|
||||
|
||||
if (href.startsWith('_')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(file).toString();
|
||||
|
||||
const splitContent = content.split('---')
|
||||
const frontMatter = {};
|
||||
try {
|
||||
const frontMatterRaw = splitContent[1].trim();
|
||||
|
||||
for (const line of frontMatterRaw.split('\n')) {
|
||||
try {
|
||||
const parts = line.split(':');
|
||||
const key = parts[0].trim()
|
||||
const value = JSON.parse(parts[1]);
|
||||
|
||||
frontMatter[key] = value;
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (frontMatter.draft !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pageIndex = {}
|
||||
|
||||
pageIndex['title'] = frontMatter.title;
|
||||
pageIndex['href'] = href;
|
||||
pageIndex['content'] = S(splitContent[2]).trim().stripTags().stripPunctuation().s;
|
||||
|
||||
if (frontMatter.tags) {
|
||||
pageIndex['tags'] = frontMatter.tags;
|
||||
}
|
||||
|
||||
return pageIndex;
|
||||
})
|
||||
.filter(e => e)
|
||||
;
|
||||
|
||||
fs.mkdirSync(path.dirname(OUTPUT_PATH), {
|
||||
recursive: true,
|
||||
});
|
||||
fs.writeFileSync(OUTPUT_PATH, JSON.stringify(pageIndices));
|
||||
@ -3,6 +3,8 @@
|
||||
"name": "personal-site",
|
||||
"version": "1.0.0",
|
||||
"devDependencies": {
|
||||
"@fortawesome/fontawesome-free": "^6.1.1"
|
||||
"@fortawesome/fontawesome-free": "^6.1.1",
|
||||
"lunr": "^2.3.9",
|
||||
"string": "^3.3.3"
|
||||
}
|
||||
}
|
||||
|
||||
10
yarn.lock
10
yarn.lock
@ -6,3 +6,13 @@
|
||||
version "6.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free/-/fontawesome-free-6.1.1.tgz#bf5d45611ab74890be386712a0e5d998c65ee2a1"
|
||||
integrity sha512-J/3yg2AIXc9wznaVqpHVX3Wa5jwKovVF0AMYSnbmcXTiL3PpRPfF58pzWucCwEiCJBp+hCNRLWClTomD8SseKg==
|
||||
|
||||
lunr@^2.3.9:
|
||||
version "2.3.9"
|
||||
resolved "https://registry.yarnpkg.com/lunr/-/lunr-2.3.9.tgz#18b123142832337dd6e964df1a5a7707b25d35e1"
|
||||
integrity sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==
|
||||
|
||||
string@^3.3.3:
|
||||
version "3.3.3"
|
||||
resolved "https://registry.yarnpkg.com/string/-/string-3.3.3.tgz#5ea211cd92d228e184294990a6cc97b366a77cb0"
|
||||
integrity sha512-LbvprpPZT/39QKfNrlPX9vXtS7If80vqbPQ7clnHQb5oVOM5hz/cs3iQCCZjvQDwsAWl+HpLQX3gRgN6IC8t3g==
|
||||
|
||||
Loading…
Reference in New Issue
Block a user