From 475def08cabd8009e00f582dfd0165c351a0c84c Mon Sep 17 00:00:00 2001 From: James Walker Date: Thu, 19 May 2022 22:08:42 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20ghost=20api=20basics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eleventy.js | 38 ++++++ .gitignore | 1 + _data/metadata.json | 2 +- _includes/layouts/{base.njk => default.njk} | 3 +- _includes/layouts/post.njk | 21 ++++ feed/feed.njk | 4 +- feed/json.njk | 4 +- index.njk | 31 +++-- package-lock.json | 132 +++++++++++++++++++- package.json | 6 +- posts/index.njk | 11 ++ 11 files changed, 231 insertions(+), 22 deletions(-) rename _includes/layouts/{base.njk => default.njk} (95%) create mode 100644 _includes/layouts/post.njk create mode 100644 posts/index.njk diff --git a/.eleventy.js b/.eleventy.js index 29c2339..b971437 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -1,9 +1,47 @@ +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) => { + console.log(post, "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; + }); } \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4879021..1df8868 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules /_site +/.env \ No newline at end of file diff --git a/_data/metadata.json b/_data/metadata.json index 83b24ca..1fafb93 100644 --- a/_data/metadata.json +++ b/_data/metadata.json @@ -1,6 +1,6 @@ { "title": "walkah", - "url": "https://walkah.net/", + "url": "https://walkah.blog/", "description": "It's a new blog, just like the old blog.", "feed": { "filename": "feed.xml", diff --git a/_includes/layouts/base.njk b/_includes/layouts/default.njk similarity index 95% rename from _includes/layouts/base.njk rename to _includes/layouts/default.njk index 8489bf3..0e0e4d0 100644 --- a/_includes/layouts/base.njk +++ b/_includes/layouts/default.njk @@ -24,7 +24,8 @@
- {{ content | safe }} + {% block content %} + {% endblock content %}