Package Exports
- eleventy-plugin-ghost
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eleventy-plugin-ghost) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
eleventy-plugin-ghost
Import your Ghost content directly into Eleventy as global data.
Note: This plugin currently uses a development version of Eleventy which includes addGlobalData(), tread carefully
See the live demo and the demo directory in the repo to see it all in action.
Installation
Install plugin using npm:
npm install eleventy-plugin-ghostAdd plugin to your
.eleventy.jsconfig, ensuring to add your Ghost url and Content API key. Check out the Ghost docs for how to create a Content API key:const pluginGhost = require("eleventy-plugin-ghost"); require("dotenv").config(); const { GHOST_URL, GHOST_KEY } = process.env; module.exports = (eleventyConfig) => { eleventyConfig.addPlugin(pluginGhost, { url: GHOST_URL, key: GHOST_KEY, version: "v3", }); };
The example above is using
dotenvwith a.envfile to ensure credentials are not stored in the source code. Here's an example of the.envfile:GHOST_URL=https://demo.ghost.io GHOST_KEY=22444f78447824223cefc48062
Run your Eleventy project and use the global
ghostdata variable to accessposts,pages,tagsandauthors.
Usage
The plugin comes with a custom filter called filterPosts, this can be used to filter posts by attributes such as authors, tags and internalTags. For example, if I want to create tag pages for all my tags and show posts assigned to them I can iterate over the tags with eleventy pagination and filter the posts by that tag:
---
pagination:
data: ghost.tags
size: 1
alias: tag
permalink: "/{{ tag.slug }}/"
---
{% for post in ghost.posts | filterPosts("tags", tag.slug) %}
<li><a href="/{{ post.slug }}/">{{ post.title }}</a></li>
{% endfor %}Plain text values also work:
{% for post in ghost.posts | filterPosts("tags", "blog") %}
<li><a href="/{{ post.slug }}/">{{ post.title }}</a></li>
{% endfor %}As well as filtering out by a certain attribute by prefixing the value with !:
{% for post in ghost.posts | filterPosts("tags", "!portfolio") %}
<li><a href="/{{ post.slug }}/">{{ post.title }}</a></li>
{% endfor %}To handle internal tags within Ghost better posts have tags populated with just public tags and internalTags populated with just internal tags. This allows for post filtering when a post has, or hasn't, been assigned with a certain internal tag:
{% for post in ghost.posts | filterPosts("internalTags", "hash-internal-tag") %}
<li><a href="/{{ post.slug }}/">{{ post.title }}</a></li>
{% endfor %}
<!-- or -->
{% for post in ghost.posts | filterPosts("internalTags", "!hash-internal-tag") %}
<li><a href="/{{ post.slug }}/">{{ post.title }}</a></li>
{% endfor %}Check out the demo directory of this project for more extensive examples.
Development
Create a
.envfile inside ofdemowith the following credentials:GHOST_URL=https://demo.ghost.io GHOST_KEY=22444f78447824223cefc48062
Amend the
.eleventy.jsfile withindemoso it points to the source code in the parent directory:// const pluginGhost = require("../"); const pluginGhost = require("eleventy-plugin-ghost");
Install the demo dependencies:
cd demo npm install
Run the demo locally:
npm run dev