2024-11-24
This commit is contained in:
86
.eleventy.js
86
.eleventy.js
@@ -6,6 +6,7 @@ const handlebarsHelpers = require('handlebars-helpers')
|
||||
const markdownit = require('markdown-it')
|
||||
const md = markdownit()
|
||||
const htmlmin = require("html-minifier");
|
||||
const { DateTime } = require("luxon");
|
||||
require('dotenv').config();
|
||||
|
||||
module.exports = function(eleventyConfig) {
|
||||
@@ -20,27 +21,19 @@ module.exports = function(eleventyConfig) {
|
||||
eleventyConfig.addPlugin(handlebarsPlugin);
|
||||
eleventyConfig.addPlugin(pluginRss);
|
||||
|
||||
// handlebars helpers
|
||||
handlebars.registerHelper("formatSeasonEpisode", function(season, episode) {
|
||||
// Convert strings to integers and pad with zeros
|
||||
const seasonNumber = parseInt(season, 10).toString().padStart(2, '0');
|
||||
const episodeNumber = parseInt(episode, 10).toString().padStart(2, '0');
|
||||
|
||||
// Return the formatted string
|
||||
return `<span class="season">S${seasonNumber}</span><span class="episode">E${episodeNumber}</span>`;
|
||||
// return `S${seasonNumber}E${episodeNumber}`;
|
||||
});
|
||||
|
||||
handlebarsHelpers({
|
||||
handlebars
|
||||
})
|
||||
|
||||
eleventyConfig.addFilter("seasonEpisodeFormat", function (season, episode, separator="") {
|
||||
const seasonNumber = parseInt(season, 10).toString().padStart(2, '0');
|
||||
const episodeNumber = parseInt(episode, 10).toString().padStart(2, '0');
|
||||
return [seasonNumber, episodeNumber].join(separator)
|
||||
return value;
|
||||
});
|
||||
eleventyConfig.addFilter(
|
||||
"seasonEpisodeFormat",
|
||||
require("./filters/utils").seasonEpisodeFormat
|
||||
);
|
||||
|
||||
eleventyConfig.addFilter("formatDate", (date, format = "MMMM d, yyyy") => {
|
||||
return DateTime.fromJSDate(new Date(date)).toFormat(format);
|
||||
});
|
||||
|
||||
|
||||
eleventyConfig.addFilter("episodeNumber", function (s, episode) {
|
||||
return episode ? Number(episode) : Number(s.replace(/[^0-9]/,''))
|
||||
@@ -50,6 +43,47 @@ module.exports = function(eleventyConfig) {
|
||||
return episode ? Number(episode) : Number(s.replace(/[^0-9]/,''))
|
||||
});
|
||||
|
||||
eleventyConfig.addFilter("extractUniqueTags", function (object, tagPrefix = "") {
|
||||
// Flatten all pages into a single array of items
|
||||
let allItems
|
||||
if (object.pages) {allItems = object.pages.flat()} else { object }
|
||||
|
||||
// Extract the desired property from each item
|
||||
const extractedValues = allItems
|
||||
.map(item => item.data.tags)
|
||||
.flat(); // Flatten arrays if the property contains arrays, like tags
|
||||
|
||||
// Filter for unique values with the "campaign" prefix
|
||||
const uniqueValues = [...new Set(extractedValues)]
|
||||
.filter(tag => tag.startsWith(tagPrefix));
|
||||
|
||||
return uniqueValues;
|
||||
});
|
||||
|
||||
eleventyConfig.addFilter("findPageByTag", function (tag, collections) {
|
||||
// Split the tag to get the collection name and slug
|
||||
const [collectionName, slug] = tag.split(":");
|
||||
|
||||
if (!collectionName || !slug) {
|
||||
console.error(`Invalid tag format: "${tag}". Expected format "collection:slug".`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Ensure the collection exists
|
||||
const collection = collections[collectionName];
|
||||
if (!collection) {
|
||||
console.error(`Collection "${collectionName}" not found in collections.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Search for the page in the inferred collection
|
||||
// TO-DO: this logic will break once we get into season 10, since season 1 will match 1 and 10
|
||||
const matchingPage = collection.find(item => item.fileSlug.includes(slug));
|
||||
|
||||
// Return the matching page (or null if not found)
|
||||
return matchingPage || null;
|
||||
});
|
||||
|
||||
// Shortcodes
|
||||
eleventyConfig.addPairedShortcode(
|
||||
"prologue",
|
||||
@@ -83,15 +117,6 @@ module.exports = function(eleventyConfig) {
|
||||
return content;
|
||||
});
|
||||
|
||||
// Register Helpers
|
||||
handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
|
||||
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
|
||||
});
|
||||
|
||||
handlebars.registerHelper('ifIncludes', function(set, candidate, options) {
|
||||
return (set.includes(candidate)) ? options.fn(this) : options.inverse(this);
|
||||
});
|
||||
|
||||
// Creates the extension for use
|
||||
eleventyConfig.addTemplateFormats("scss");
|
||||
eleventyConfig.addExtension("scss", {
|
||||
@@ -107,15 +132,6 @@ module.exports = function(eleventyConfig) {
|
||||
},
|
||||
});
|
||||
|
||||
eleventyConfig.addNunjucksFilter("podcastUrl", function(post) {
|
||||
// Convert the season and episode to zero-padded numbers
|
||||
const seasonNumber = parseInt(post.data.season, 10).toString().padStart(2, '0');
|
||||
const episodeNumber = parseInt(post.data.episode, 10).toString().padStart(2, '0');
|
||||
|
||||
// Construct the podcast URL
|
||||
return `/episodes/s${seasonNumber}/s${seasonNumber}e${episodeNumber}.mp3`;
|
||||
});
|
||||
|
||||
return {
|
||||
pathPrefix:"blog",
|
||||
dir: {
|
||||
|
||||
Reference in New Issue
Block a user