blog/.eleventy.js

46 lines
1.2 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 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.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);
});
// Bring featured post to the top of the list
collection.sort((post, nextPost) => nextPost.featured - post.featured);
return collection;
});
}