Skip to content

Commit

Permalink
better tag search (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
uroybd authored Jan 19, 2023
1 parent 92d49c2 commit 3176a78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
21 changes: 21 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
39 changes: 20 additions & 19 deletions netlify/functions/search/search.js
Original file line number Diff line number Diff line change
@@ -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;
});

Expand All @@ -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 };
2 changes: 1 addition & 1 deletion src/site/lunr.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}]

0 comments on commit 3176a78

Please sign in to comment.