Visit https://www.simonfacciol.com/squarespace-to-ghost/ for a guide on how to use this script.
Simon 👋🏻
Visit https://www.simonfacciol.com/squarespace-to-ghost/ for a guide on how to use this script.
Simon 👋🏻
| 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" | |
| } | |
| } |