Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ module.exports = function (eleventyConfig) {
);
});

eleventyConfig.addFilter("stripForSearch", function(content) {
return content
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, ' ')
.trim()
.substring(0, 500);
});

eleventyConfig.addFilter("searchableTags", function (str) {
let tags;
let match = str && str.match(tagRegex);
Expand Down Expand Up @@ -526,6 +534,22 @@ module.exports = function (eleventyConfig) {
return content;
});

eleventyConfig.addTransform("jsonMinifier", async (content, outputPath) => {
if (
(process.env.NODE_ENV === "production" || process.env.ELEVENTY_ENV === "prod") &&
outputPath &&
outputPath.endsWith(".json")
) {
try {
return JSON.stringify(JSON.parse(content));
} catch {
// If the JSON minifying fails for some reason due to malformed JSON, just return the content as is.
return content;
}
}
return content;
});

eleventyConfig.addPassthroughCopy("src/site/img");
eleventyConfig.addPassthroughCopy("src/site/scripts");
eleventyConfig.addPassthroughCopy("src/site/styles/_theme.*.css");
Expand Down
3 changes: 1 addition & 2 deletions src/site/search-index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ eleventyExcludeFromCollections: true
[{% for post in collections.note %}
{
"title": {% if post.data.title %}{{post.data.title | jsonify | safe }}{% else %}{{post.fileSlug | jsonify | safe }}{% endif %},
"date":"{{ post.date }}",
"url":"{{ post.url }}",
"content": {{ post.templateContent | striptags(true) | link | jsonify | safe }},
"content": {{ post.templateContent | link | stripForSearch | jsonify | safe }},
"tags": [{{post.templateContent | link | searchableTags | safe }} {% if post.data.tags %}{% for tag in post.data.tags %}"{{tag|validJson}}"{% if not loop.last %},{% endif %}{% endfor %}{% endif %}]
}{% if not loop.last %},{% endif %}
{% endfor %}]