better tag search (#46)

This commit is contained in:
Utsob Roy 2023-01-19 19:46:56 +06:00 committed by GitHub
parent 92d49c2c54
commit 3176a78c03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 20 deletions

View File

@ -202,6 +202,27 @@ module.exports = function (eleventyConfig) {
);
});
eleventyConfig.addFilter("searchableTags", function (str) {
let tags;
let match =
str &&
str.match(
/(^|\s|\>)(#[^\s!@#$%^&*()=+\.\/,\[{\]};:'"?><]+)(?!([^<]*>))/g
);
if (match) {
tags = match
.map((m) => {
return `"${m.split("#")[1]}"`;
})
.join(", ");
}
if (tags) {
return `${tags},`;
} else {
return "";
}
});
eleventyConfig.addTransform("callout-block", function (str) {
const parsed = parse(str);

View File

@ -1,28 +1,29 @@
const lunrjs = require('lunr');
const lunrjs = require("lunr");
const handler = async (event) => {
try {
const search = event.queryStringParameters.term;
if(!search) throw('Missing term query parameter');
if (!search) throw "Missing term query parameter";
const data = require('./data.json');
const indexJson = require('./index.json');
const data = require("./data.json");
const indexJson = require("./index.json");
const index = lunrjs.Index.load(indexJson);
console.log('index made');
console.log("index made");
let results =
let results =
search[0] == "#" && search.length > 1
? index.search(`${search.substring(1)}`)
: index.search(search+"*");
? index.search(`tags:${search.substring(1)}`)
: index.search(search + "*");
results.forEach(r => {
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
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;
});
@ -32,17 +33,17 @@ const handler = async (event) => {
// // more keys you can return:
// headers: { "headerName": "headerValue", ... },
// isBase64Encoded: true,
}
};
} catch (error) {
return { statusCode: 500, body: error.toString() }
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) + '...';
str = str.replace(/<.*?>/g, "");
if (str.length < size) return str;
return str.substring(0, size - 3) + "...";
}
module.exports = { handler }
module.exports = { handler };

View File

@ -9,6 +9,6 @@ eleventyExcludeFromCollections: true
"date":"{{ post.date }}",
"url":"{{ post.url }}",
"content": {{ post.templateContent | striptags(true) | link | jsonify | safe }},
"tags": [{% if post.data.tags %}{% for tag in post.data.tags %}"{{tag}}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}]
"tags": [{{post.templateContent | link | searchableTags | safe }} {% if post.data.tags %}{% for tag in post.data.tags %}"{{tag}}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}]
}{% if not loop.last %},{% endif %}
{% endfor %}]