mirror of
https://github.com/tcsenpai/obsidiangarden_netlify.git
synced 2025-06-08 05:35:20 +00:00
better tag search (#46)
This commit is contained in:
parent
92d49c2c54
commit
3176a78c03
21
.eleventy.js
21
.eleventy.js
@ -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) {
|
eleventyConfig.addTransform("callout-block", function (str) {
|
||||||
const parsed = parse(str);
|
const parsed = parse(str);
|
||||||
|
|
||||||
|
@ -1,28 +1,29 @@
|
|||||||
const lunrjs = require('lunr');
|
const lunrjs = require("lunr");
|
||||||
|
|
||||||
const handler = async (event) => {
|
const handler = async (event) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const search = event.queryStringParameters.term;
|
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 data = require("./data.json");
|
||||||
const indexJson = require('./index.json');
|
const indexJson = require("./index.json");
|
||||||
const index = lunrjs.Index.load(indexJson);
|
const index = lunrjs.Index.load(indexJson);
|
||||||
console.log('index made');
|
console.log("index made");
|
||||||
|
|
||||||
let results =
|
let results =
|
||||||
search[0] == "#" && search.length > 1
|
search[0] == "#" && search.length > 1
|
||||||
? index.search(`${search.substring(1)}`)
|
? index.search(`tags:${search.substring(1)}`)
|
||||||
: index.search(search+"*");
|
: index.search(search + "*");
|
||||||
|
|
||||||
results.forEach(r => {
|
results.forEach((r) => {
|
||||||
r.title = data[r.ref].title;
|
r.title = data[r.ref].title;
|
||||||
r.content = truncate(data[r.ref].content, 400);
|
r.content = truncate(data[r.ref].content, 400);
|
||||||
r.date = data[r.ref].date;
|
r.date = data[r.ref].date;
|
||||||
r.url = data[r.ref].url;
|
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;
|
delete r.ref;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -32,17 +33,17 @@ const handler = async (event) => {
|
|||||||
// // more keys you can return:
|
// // more keys you can return:
|
||||||
// headers: { "headerName": "headerValue", ... },
|
// headers: { "headerName": "headerValue", ... },
|
||||||
// isBase64Encoded: true,
|
// isBase64Encoded: true,
|
||||||
}
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { statusCode: 500, body: error.toString() }
|
return { statusCode: 500, body: error.toString() };
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function truncate(str, size) {
|
function truncate(str, size) {
|
||||||
//first, remove HTML
|
//first, remove HTML
|
||||||
str = str.replace(/<.*?>/g, '');
|
str = str.replace(/<.*?>/g, "");
|
||||||
if(str.length < size) return str;
|
if (str.length < size) return str;
|
||||||
return str.substring(0, size-3) + '...';
|
return str.substring(0, size - 3) + "...";
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { handler }
|
module.exports = { handler };
|
||||||
|
@ -9,6 +9,6 @@ eleventyExcludeFromCollections: true
|
|||||||
"date":"{{ post.date }}",
|
"date":"{{ post.date }}",
|
||||||
"url":"{{ post.url }}",
|
"url":"{{ post.url }}",
|
||||||
"content": {{ post.templateContent | striptags(true) | link | jsonify | safe }},
|
"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 %}
|
}{% if not loop.last %},{% endif %}
|
||||||
{% endfor %}]
|
{% endfor %}]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user