blog/.eleventy.js
2022-12-08 18:07:41 -05:00

70 lines
1.9 KiB
JavaScript

require("dotenv").config();
const GhostContentAPI = require("@tryghost/content-api");
const pluginNavigation = require("@11ty/eleventy-navigation");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const localImages = require('eleventy-plugin-local-images');
const path = require("path");
const api = new GhostContentAPI({
url: process.env.GHOST_API_URL,
key: process.env.GHOST_CONTENT_API_KEY,
version: "v3.0"
});
const stripDomain = url => {
return url.replace(process.env.GHOST_API_URL, "");
};
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginNavigation);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPassthroughCopy("css");
eleventyConfig.addPassthroughCopy("images");
eleventyConfig.addPlugin(localImages, {
distPath: '_site',
assetPath: '/assets/images',
selector: 'img',
verbose: false
});
eleventyConfig.addFilter("dateString", (dateObj) => {
return new Date(dateObj).toISOString().split("T")[0];
});
eleventyConfig.addFilter("relativePath", (pathToFilter, page) => {
if (!pathToFilter.startsWith("/")) {
return pathToFilter;
}
return path.relative(page.url, pathToFilter);
})
eleventyConfig.addCollection("posts", async function (collection) {
collection = await api.posts
.browse({
include: "tags,authors",
limit: "all",
})
.catch((err) => {
console.error(err);
});
collection.forEach((post) => {
post.url = stripDomain(post.url);
post.primary_author.url = stripDomain(post.primary_author.url);
// Convert publish date into a Date object
post.date = new Date(post.published_at);
post.published_at = new Date(post.published_at);
post.updated_at = new Date(post.updated_at);
});
// Bring featured post to the top of the list
collection.sort((post, nextPost) => nextPost.featured - post.featured);
return collection;
});
}