Skip to content

Instantly share code, notes, and snippets.

@daviddarnes
Last active June 10, 2023 01:17
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daviddarnes/eb956c1a8b57f4249ea57516b06ca89e to your computer and use it in GitHub Desktop.
Save daviddarnes/eb956c1a8b57f4249ea57516b06ca89e to your computer and use it in GitHub Desktop.
Import your Ghost posts into a Jekyll project using Gulp
const File = require('vinyl');
const gulp = require("gulp");
const Handlebars = require('handlebars');
const streamArray = require('stream-array');
const ghostContentAPI = require("@tryghost/content-api");
const api = new ghostContentAPI({
url: 'https://demo.ghost.io',
key: '22444f78447824223cefc48062',
version: "v4"
});
const template = `
---
title: {{ title }}
excerpt: {{{ excerpt }}}
feature_image: {{ feature_image }}
tags:
{{#each tags}}
- {{ this.slug }}
{{/each}}
---
{{{ html }}}
`;
const templateFunction = Handlebars.compile(template.trim());
gulp.task('ghost', async function() {
const posts = await api.posts
.browse({
include: "tags,authors",
limit: "all"
})
.catch(err => {
console.error(err);
});
const files = posts.map(post => {
const { published_at, slug } = post;
return new File({
path: `${published_at.slice(0,10)}-${slug}.md`,
contents: Buffer.from(templateFunction(post))
});
});
// Gulp specific stream stuff
return streamArray(files)
.pipe(gulp.dest('./_posts'));
});
{
"name": "ghost-to-jekyll",
"version": "1.0.0",
"description": "Import Ghost into a Jekyll site",
"main": "gulpfile.js",
"scripts": {
"test": "gulp ghost"
},
"author": "David Darnes",
"license": "MIT",
"dependencies": {
"@tryghost/content-api": "^1.2.7",
"gulp": "^4.0.2",
"handlebars": "^4.1.2",
"stream-array": "^1.1.2",
"vinyl": "^2.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment