lewisdale.dev/config/filters/excerpt.js
Lewis Dale 5eab7bcb9d
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 2m13s
Fixing JSON feed
2024-04-29 19:15:50 +01:00

15 lines
512 B
JavaScript

const createExcerpt = (post, limit ) => {
const withoutTags = post.content.replace(/(<([^>]+)>)/gi, "")
.replace(/[^\x20-\x7E]/gmi, " ")
.replace(/\s+/g, " ").trim();
if (withoutTags.length > limit) {
return withoutTags.slice(0, limit) + " […]";
}
return withoutTags;
}
module.exports = function (eleventyConfig) {
eleventyConfig.addFilter('excerpt', (post, limit = 250) => {
return post.data.excerpt || createExcerpt(post, limit);
});
}