Skip to content

Commit

Permalink
Move filters to filters.js file
Browse files Browse the repository at this point in the history
Signed-off-by: Anneke Sinnema <[email protected]>
  • Loading branch information
anneke committed Dec 31, 2024
1 parent 89efc55 commit 1bf4b4d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 54 deletions.
53 changes: 0 additions & 53 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ module.exports = function (eleventyConfig) {
eleventyConfig.ignores.add("src/nl/vereniging/bestuur/notulen");
}

// Custom date filter
eleventyConfig.addFilter("localizedDate", function (dateObj, locale = "en") {
return DateTime.fromJSDate(dateObj)
.setLocale(locale)
.toFormat("d LLLL yyyy");
});

/* Add id to heading elements */
eleventyConfig.addPlugin(pluginAddIdToHeadings);

Expand Down Expand Up @@ -185,41 +178,6 @@ module.exports = function (eleventyConfig) {
})
);


eleventyConfig.addFilter("readablePostDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, {
zone: "Europe/Amsterdam",
}).setLocale('en').toLocaleString(DateTime.DATE_FULL);
});

eleventyConfig.addFilter("postDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, {
zone: "Europe/Amsterdam",
}).setLocale('en').toISODate();
});

eleventyConfig.addFilter('splitlines', function (input) {
const parts = input.split(' ');
const lines = parts.reduce(function (prev, current) {

if (!prev.length) {
return [current];
}

let lastOne = prev[prev.length - 1];

if (lastOne.length + current.length > 23) {
return [...prev, current];
}

prev[prev.length - 1] = lastOne + ' ' + current;

return prev;
}, []);

return lines;
});

eleventyConfig.on('afterBuild', async () => {
async function convertSvgToJpeg(inputDir, outputDir) {
const browser = await puppeteer.launch();
Expand Down Expand Up @@ -278,17 +236,6 @@ module.exports = function (eleventyConfig) {
await convertSvgToJpeg(inputDir, outputDir);
});

// Allows you to debug a json object in eleventy templates data | stringify
eleventyConfig.addFilter("stringify", (data) => {
return JSON.stringify(data, null, "\t");
});

eleventyConfig.addFilter("customSlug", function (value) {
if (!value) return "fallback-title"; // Fallback for empty titles
return slugify(value, {
lower: true, // Convert to lowercase
remove: /[^\w\s-]/g // Remove all non-word characters except spaces and dashes
}).replace(/\s+/g, '-'); // Replace spaces with dashes (extra safety)
});

// https://www.11ty.dev/docs/permalinks/#remove-trailing-slashes
Expand Down
48 changes: 47 additions & 1 deletion utils/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,50 @@ module.exports = {
});
},

};
readablePostDate(dateObj) {
return DateTime.fromJSDate(dateObj, {
zone: "Europe/Amsterdam",
}).setLocale('en').toLocaleString(DateTime.DATE_FULL);
},

postDate(dateObj) {
return DateTime.fromJSDate(dateObj, {
zone: "Europe/Amsterdam",
}).setLocale('en').toISODate();
},

splitlines(input) {
const parts = input.split(' ');
const lines = parts.reduce(function (prev, current) {
if (!prev.length) {
return [current];
}

let lastOne = prev[prev.length - 1];

if (lastOne.length + current.length > 23) {
return [...prev, current];
}

prev[prev.length - 1] = lastOne + ' ' + current;
return prev;
}, []);

return lines;
},

stringify(data) {
return JSON.stringify(data, null, "\t");
},

customSlug(value) {
if (!value) return "fallback-title"; // Fallback for empty titles
return strToSlug(value).replace(/\s+/g, '-'); // Replace spaces with dashes
},

localizedDate(dateObj, locale = "en") {
return DateTime.fromJSDate(dateObj)
.setLocale(locale)
.toFormat("d LLLL yyyy");
}
};

0 comments on commit 1bf4b4d

Please sign in to comment.