add ghost api basics

This commit is contained in:
James Walker 2022-05-19 22:08:42 -04:00
parent 5fb0d147ed
commit 475def08ca
Signed by: walkah
GPG Key ID: 3C127179D6086E93
11 changed files with 231 additions and 22 deletions

View File

@ -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;
});
}

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/node_modules
/_site
/.env

View File

@ -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",

View File

@ -24,7 +24,8 @@
</header>
<main>
{{ content | safe }}
{% block content %}
{% endblock content %}
</main>
<footer>

View File

@ -0,0 +1,21 @@
{% extends 'layouts/default.njk' %}
{% set title = post.title %}
{% set codeinjection_head = post.codeinjection_head %}
{% set codeinjection_foot = post.codeinjection_foot %}
{% block content %}
<article class="content">
{% if post.feature_image %}
<figure class="post-feature-image">
<img src="{{ post.feature_image }}" alt="{{ title }}">
</figure>
{% endif %}
<section class="post-full-content">
<h1 class="content-title">{{ title }}</h1>
<section class="content-body load-external-scripts">
{{ content | safe }}
</section>
</section>
</article>
{% endblock %}

View File

@ -18,11 +18,11 @@ eleventyExcludeFromCollections: true
{%- for post in collections.posts | reverse %}
{% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset %}
<entry>
<title>{{ post.data.title }}</title>
<title>{{ post.title }}</title>
<link href="{{ absolutePostUrl }}"/>
<updated>{{ post.date | rssDate }}</updated>
<id>{{ absolutePostUrl }}</id>
<content type="html">{{ post.templateContent | htmlToAbsoluteUrls(absolutePostUrl) }}</content>
<content type="html">{{ post.html | htmlToAbsoluteUrls(absolutePostUrl) }}</content>
</entry>
{%- endfor %}
</feed>

View File

@ -18,8 +18,8 @@ eleventyExcludeFromCollections: true
{
"id": "{{ absolutePostUrl }}",
"url": "{{ absolutePostUrl }}",
"title": "{{ post.data.title }}",
"content_html": {% if post.templateContent %}{{ post.templateContent | dump | safe }}{% else %}""{% endif %},
"title": "{{ post.title }}",
"content_html": {% if post.html %}{{ post.html | dump | safe }}{% else %}""{% endif %},
"date_published": "{{ post.date | rssDate }}"
}
{%- if not loop.last -%}

View File

@ -1,16 +1,21 @@
---
layout: layouts/base.njk
pagination:
data: collections.posts
size: 6
alias: posts
---
{% extends 'layouts/default.njk' %}
{% block content %}
{% set postslist = collections.posts %}
{% set postslist = collections.posts %}
{% for post in postslist | reverse %}
<article class="h-entry">
<h2 class="p-name">
<a href="{{ post.url }}">{{ post.data.title }}</a>
</h2>
<div class="e-content">
{{ post.templateContent | safe }}
</div>
</article>
{% endfor %}
{% for post in postslist | reverse %}
<article class="h-entry">
<h2 class="p-name">
<a href="{{ post.url }}">{{ post.title }}</a>
</h2>
<div class="e-content">
{{ post.excerpt | safe }}
</div>
</article>
{% endfor %}
{% endblock %}

132
package-lock.json generated
View File

@ -11,7 +11,9 @@
"devDependencies": {
"@11ty/eleventy": "^1.0.1",
"@11ty/eleventy-navigation": "^0.3.3",
"@11ty/eleventy-plugin-rss": "^1.0.9"
"@11ty/eleventy-plugin-rss": "^1.0.9",
"@tryghost/content-api": "^1.9.4",
"dotenv": "^16.0.1"
}
},
"node_modules/@11ty/dependency-tree": {
@ -238,6 +240,25 @@
"integrity": "sha512-+9jVqKhRSpsc591z5vX+X5Yyw+he/HCB4iQ/RYxw35CEPaY1gnsNE43nf9n9AaYjAQrTiI/mOwKUKdUs9vf7Xg==",
"dev": true
},
"node_modules/@tryghost/content-api": {
"version": "1.9.4",
"resolved": "https://registry.npmjs.org/@tryghost/content-api/-/content-api-1.9.4.tgz",
"integrity": "sha512-GHAA9iDjAmuEpbX1FRYjxOkUW7+feUMHFzPR066Z5rgMJ/uLDRTBcSQ7SBTFHlrh9MOUcSBs4urNn/mbH0M2xQ==",
"dev": true,
"dependencies": {
"axios": "^0.27.0"
}
},
"node_modules/@tryghost/content-api/node_modules/axios": {
"version": "0.27.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
"dev": true,
"dependencies": {
"follow-redirects": "^1.14.9",
"form-data": "^4.0.0"
}
},
"node_modules/@types/component-emitter": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.11.tgz",
@ -420,6 +441,12 @@
"node": ">=0.8.0"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=",
"dev": true
},
"node_modules/axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
@ -681,6 +708,18 @@
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dev": true,
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/commander": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz",
@ -840,6 +879,15 @@
}
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=",
"dev": true,
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@ -943,6 +991,15 @@
"url": "https://github.com/fb55/domutils?sponsor=1"
}
},
"node_modules/dotenv": {
"version": "16.0.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
"integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==",
"dev": true,
"engines": {
"node": ">=12"
}
},
"node_modules/easy-extender": {
"version": "2.3.4",
"resolved": "https://registry.npmjs.org/easy-extender/-/easy-extender-2.3.4.tgz",
@ -1284,6 +1341,20 @@
}
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dev": true,
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
@ -3793,6 +3864,27 @@
"integrity": "sha512-+9jVqKhRSpsc591z5vX+X5Yyw+he/HCB4iQ/RYxw35CEPaY1gnsNE43nf9n9AaYjAQrTiI/mOwKUKdUs9vf7Xg==",
"dev": true
},
"@tryghost/content-api": {
"version": "1.9.4",
"resolved": "https://registry.npmjs.org/@tryghost/content-api/-/content-api-1.9.4.tgz",
"integrity": "sha512-GHAA9iDjAmuEpbX1FRYjxOkUW7+feUMHFzPR066Z5rgMJ/uLDRTBcSQ7SBTFHlrh9MOUcSBs4urNn/mbH0M2xQ==",
"dev": true,
"requires": {
"axios": "^0.27.0"
},
"dependencies": {
"axios": {
"version": "0.27.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
"dev": true,
"requires": {
"follow-redirects": "^1.14.9",
"form-data": "^4.0.0"
}
}
}
},
"@types/component-emitter": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.11.tgz",
@ -3939,6 +4031,12 @@
"integrity": "sha1-dhfBkXQB/Yykooqtzj266Yr+tDI=",
"dev": true
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=",
"dev": true
},
"axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
@ -4153,6 +4251,15 @@
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dev": true,
"requires": {
"delayed-stream": "~1.0.0"
}
},
"commander": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz",
@ -4284,6 +4391,12 @@
"ms": "2.1.2"
}
},
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=",
"dev": true
},
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
@ -4357,6 +4470,12 @@
"domhandler": "^4.2.0"
}
},
"dotenv": {
"version": "16.0.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
"integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==",
"dev": true
},
"easy-extender": {
"version": "2.3.4",
"resolved": "https://registry.npmjs.org/easy-extender/-/easy-extender-2.3.4.tgz",
@ -4623,6 +4742,17 @@
"integrity": "sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==",
"dev": true
},
"form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dev": true,
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
},
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",

View File

@ -15,6 +15,8 @@
"devDependencies": {
"@11ty/eleventy": "^1.0.1",
"@11ty/eleventy-navigation": "^0.3.3",
"@11ty/eleventy-plugin-rss": "^1.0.9"
"@11ty/eleventy-plugin-rss": "^1.0.9",
"@tryghost/content-api": "^1.9.4",
"dotenv": "^16.0.1"
}
}
}

11
posts/index.njk Normal file
View File

@ -0,0 +1,11 @@
---
pagination:
data: collections.posts
size: 1
alias: post
addAllPagesToCollections: true
layout: 'layouts/post.njk'
permalink: '{{ post.url }}'
---
{{ post.html | safe if post.html else "Post content not found" }}