Skip to content

Instantly share code, notes, and snippets.

@simofacc
Created February 15, 2020 19:19
Show Gist options
  • Save simofacc/3d61ae74ba706bdbbc8534a43a52f550 to your computer and use it in GitHub Desktop.
Save simofacc/3d61ae74ba706bdbbc8534a43a52f550 to your computer and use it in GitHub Desktop.
Squarespace.com to Ghost export/import
import GhostAdminAPI from '@tryghost/admin-api';
import fs from 'fs';
import xml2js from 'xml2js';
const api = new GhostAdminAPI({
url: 'YOUR_GHOST_INTEGRATION_API_URL',
key: 'YOUR_GHOST_INTEGRATION_ADMIN_KEY',
version: 'v3'
});
const createMobiledoc = () => {
const template = {
version: '0.3.1',
atoms: [],
cards: [],
markups: [],
sections: []
};
return { ...template };
};
const parser = new xml2js.Parser();
fs.readFile(__dirname + '/squarespace.xml', function(err, data) {
parser.parseString(data, async (err, result) => {
const items = result.rss.channel[0].item;
const posts = items.filter(i => i['wp:post_type'].includes('post'));
for (let index = 0; index < posts.length; index++) {
const p = posts[index];
const tags = p.category ? p.category.map(c => c['$'].nicename) : [];
const post = {
title: p.title[0],
tags,
status: 'published',
published_at: p.pubDate ? `${new Date(p.pubDate).toISOString()}` : ''
};
const mobiledoc = createMobiledoc();
mobiledoc.cards = [['markdown', { cardName: 'markdown', markdown: p['content:encoded'][0] }]];
mobiledoc.sections = [[10, 0]];
post.mobiledoc = JSON.stringify(mobiledoc);
await api.posts
.add(post)
.then(res => console.log)
.catch(err => console.error(err));
}
});
});
{
"name": "squarespace2ghost",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"create": "node -r esm index.js"
},
"dependencies": {
"@tryghost/admin-api": "^1.0.2",
"@tryghost/html-to-mobiledoc": "^0.6.3",
"esm": "^3.2.25",
"request": "^2.88.0",
"xml2js": "^0.4.23"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment