2024-11-27
This commit is contained in:
67
utils/filters.js
Normal file
67
utils/filters.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const { DateTime } = require("luxon");
|
||||
|
||||
module.exports = {
|
||||
seasonEpisodeFormat: (_, {
|
||||
episode,
|
||||
season,
|
||||
episodePrefix = "E",
|
||||
seasonPrefix = "S",
|
||||
separator = "",
|
||||
zeroPadding = 2,
|
||||
}) => {
|
||||
const episodeNumber = episode ? parseInt(episode, 10)
|
||||
.toString()
|
||||
.padStart(zeroPadding, '0') : '';
|
||||
const seasonNumber = season ? parseInt(season, 10)
|
||||
.toString()
|
||||
.padStart(zeroPadding, '0') : '';
|
||||
return `${seasonPrefix}${seasonNumber}${separator}${episodePrefix}${episodeNumber}`
|
||||
},
|
||||
|
||||
findPageByTag: (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;
|
||||
},
|
||||
|
||||
extractUniqueTags: (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;
|
||||
},
|
||||
formatDate: (date, format = "MMMM d, yyyy") => {
|
||||
return DateTime.fromJSDate(new Date(date)).toFormat(format);
|
||||
},
|
||||
episodeNumber: (s, episode) => {
|
||||
return episode ? Number(episode) : Number(s.replace(/[^0-9]/,''))
|
||||
}
|
||||
}
|
||||
16
utils/plugin.js
Normal file
16
utils/plugin.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const filters = require("./filters");
|
||||
const {shortcodes, pairedShortcodes} = require("./shortcodes");
|
||||
|
||||
module.exports = (eleventyConfig, options = {}) => {
|
||||
Object.keys(filters).forEach((filterName) => {
|
||||
eleventyConfig.addFilter(filterName, filters[filterName]);
|
||||
})
|
||||
|
||||
Object.keys(shortcodes).forEach((shortcodeName) => {
|
||||
eleventyConfig.addShortcode(shortcodeName, shortcodes[shortcodeName]);
|
||||
})
|
||||
|
||||
Object.keys(pairedShortcodes).forEach((shortcodeName) => {
|
||||
eleventyConfig.addPairedShortcode(shortcodeName, pairedShortcodes[shortcodeName]);
|
||||
})
|
||||
}
|
||||
24
utils/shortcodes.js
Normal file
24
utils/shortcodes.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const markdownit = require('markdown-it')
|
||||
const md = markdownit({html: true})
|
||||
const { DateTime } = require("luxon");
|
||||
|
||||
module.exports = {
|
||||
shortcodes: {
|
||||
timeTag: (datetime, format = "MMMM d, yyyy") => {
|
||||
dt = DateTime.fromJSDate(new Date(datetime))
|
||||
return `<time>${dt.toFormat(format)}</time>`
|
||||
}
|
||||
},
|
||||
|
||||
pairedShortcodes: {
|
||||
prologue: (content) =>{
|
||||
const html = md.render(content)
|
||||
return `<h1>Prologue</h1><hr><section class="prologue"><div>${html}</div></section>`
|
||||
},
|
||||
|
||||
alternateTitles: (content) => { return `<section class="alternate-titles"><h1>Alternate Titles</h1><hr>${md.render(content)}</section>` },
|
||||
|
||||
summary: (content) => { return `<section class="summary"><h1>Summary</h1><hr>${md.render(content)}</section>`
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user