mirror of
https://github.com/tcsenpai/obsidiangarden_netlify.git
synced 2025-06-06 20:55:21 +00:00
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
const lunrjs = require('lunr');
|
|
|
|
const handler = async (event) => {
|
|
try {
|
|
|
|
const search = event.queryStringParameters.term;
|
|
if(!search) throw('Missing term query parameter');
|
|
|
|
const data = require('./data.json');
|
|
const indexJson = require('./index.json');
|
|
const index = lunrjs.Index.load(indexJson);
|
|
console.log('index made');
|
|
|
|
let results;
|
|
if(search[0] == "#" && search.length > 1){
|
|
results = index.search(`${search.substring(1)}`)
|
|
}else if(search.length > 2 && search[0] == ">" && search[1] == "#"){
|
|
results = index.search(`${search.substring(2)}`)
|
|
}else{
|
|
results = index.search(search+"*");
|
|
}
|
|
|
|
results.forEach(r => {
|
|
r.title = data[r.ref].title;
|
|
r.content = truncate(data[r.ref].content, 400);
|
|
r.date = data[r.ref].date;
|
|
r.url = data[r.ref].url;
|
|
r.tags = data[r.ref].tags.filter(x=>x!="gardenEntry" && x!="note");//Note is automatically added by 11ty. GardenEntry is used internally to mark the home page
|
|
|
|
delete r.ref;
|
|
});
|
|
|
|
return {
|
|
statusCode: 200,
|
|
body: JSON.stringify(results),
|
|
// // more keys you can return:
|
|
// headers: { "headerName": "headerValue", ... },
|
|
// isBase64Encoded: true,
|
|
}
|
|
} catch (error) {
|
|
return { statusCode: 500, body: error.toString() }
|
|
}
|
|
}
|
|
|
|
function truncate(str, size) {
|
|
//first, remove HTML
|
|
str = str.replace(/<.*?>/g, '');
|
|
if(str.length < size) return str;
|
|
return str.substring(0, size-3) + '...';
|
|
}
|
|
|
|
module.exports = { handler }
|