Skip to content

Instantly share code, notes, and snippets.

@daviddarnes daviddarnes/gulpfile.js
Last active Aug 22, 2019

Embed
What would you like to do?
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: "v2"
});
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
You can’t perform that action at this time.