-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from empress/import-scripts
Add Import scripts for Ghost, WordPress, and Tumblr
- Loading branch information
Showing
9 changed files
with
499 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs'); | ||
const { safeDump } = require('js-yaml'); | ||
|
||
const importers = require('./importers'); | ||
|
||
const supportedTypes = ['wordpress', 'ghost', 'tumblr']; | ||
|
||
function ensureFolder(dir) { | ||
if (!existsSync(dir)){ | ||
mkdirSync(dir); | ||
} | ||
} | ||
|
||
function dump(data) { | ||
return safeDump(data, { skipInvalid: true }); | ||
} | ||
|
||
module.exports = { | ||
name: 'empress-blog:import', | ||
description: 'Imports data from other blog systems', | ||
works: 'insideProject', | ||
|
||
async run(commandOptions, rawArgs) { | ||
if (!commandOptions.type || !supportedTypes.includes(commandOptions.type)) { | ||
throw new Error(`You must run this command with the '--type=' parameter. Supported types are: ${supportedTypes}`) | ||
} | ||
|
||
const fileName = rawArgs[0]; | ||
|
||
if (!fileName) { | ||
throw new Error('You must pass in the export file from your blog as the first argument'); | ||
} | ||
|
||
if (!existsSync(fileName)) { | ||
throw new Error(`File "${fileName}" does not exits`); | ||
} | ||
|
||
let data = await importers[commandOptions.type](readFileSync(fileName)); | ||
|
||
if(data.tags) { | ||
ensureFolder('tag'); | ||
|
||
data.tags.forEach((tag) => { | ||
writeFileSync(`tag/${tag.id}.md`, `--- | ||
${dump({ | ||
name: tag.name, | ||
image: '', | ||
})}--- | ||
${tag.description}`) | ||
}) | ||
} | ||
|
||
if(data.authors) { | ||
ensureFolder('author'); | ||
|
||
data.authors.forEach((author) => { | ||
writeFileSync(`author/${author.id}.md`, `--- | ||
${dump({ | ||
name: author.name, | ||
id: author.id, | ||
image: author.image, | ||
coverImage: author.coverImage, | ||
website: author.website, | ||
twitter: author.twitter, | ||
facebook: author.facebook, | ||
location: author.location, | ||
})}--- | ||
${author.bio}`) | ||
}) | ||
} | ||
|
||
if(data.content) { | ||
ensureFolder('content'); | ||
|
||
data.content.forEach((content) => { | ||
writeFileSync(`content/${content.id}.md`, `--- | ||
${dump({ | ||
title: content.title, | ||
image: content.image, | ||
authors: content.authors, | ||
date: content.date, | ||
tags: content.tags, | ||
})}--- | ||
${content.content}`) | ||
}) | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const _ = require('lodash'); | ||
|
||
module.exports = async function ghostImport(fileContents) { | ||
|
||
const data = await JSON.parse(fileContents).db[0].data; | ||
|
||
let authorMap = {}; | ||
|
||
let authors = data.users.map((author) => { | ||
authorMap[author.id] = author.slug; | ||
|
||
return { | ||
id: author.slug, | ||
name: author.name || '', | ||
image: author.profile_image || '', | ||
coverImage: author.cover_image || '', | ||
website: author.website || '', | ||
twitter: author.twitter || '', | ||
facebook: author.facebook || '', | ||
location: author.location || '', | ||
bio: author.bio || '', | ||
} | ||
}) | ||
|
||
let tagMap = {}; | ||
|
||
let tags = data.tags.map((tag) => { | ||
tagMap[tag.id] = tag.slug; | ||
|
||
return { | ||
id: tag.slug, | ||
name: tag.name, | ||
image: tag.feature_image || '', | ||
description: tag.description || '' | ||
} | ||
}); | ||
|
||
let content = _.chain(data.posts) | ||
.map((post) => { | ||
// console.log(post); | ||
|
||
// don't import draft posts | ||
if(post.status !== 'published') { | ||
return; | ||
} | ||
|
||
let result = { | ||
id: post.slug, | ||
title: post.title || '', | ||
image: post.feature_image || '', | ||
date: post.created_at, | ||
content: post.plaintext, | ||
tags: _.chain(data.posts_tags) | ||
.filter(item => item.post_id === post.id) | ||
.map(item => tagMap[item.tag_id]) | ||
.value(), | ||
authors: _.chain(data.posts_authors) | ||
.filter(item => item.post_id === post.id) | ||
.map(item => authorMap[item.author_id]) | ||
.value(), | ||
} | ||
|
||
return result; | ||
}) | ||
.compact() | ||
.value(); | ||
|
||
return { | ||
tags, | ||
authors, | ||
content, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
var requireDirectory = require('require-directory'); | ||
module.exports = requireDirectory(module); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { promisify } = require("util"); | ||
const { parseString } = require("xml2js"); | ||
const _ = require("lodash"); | ||
|
||
const parse = promisify(parseString); | ||
|
||
module.exports = async function tumblrImport(fileContents) { | ||
const data = await parse(fileContents); | ||
|
||
let channel = data.tumblr.posts[0]; | ||
|
||
let allAuthors = []; | ||
let allTags = []; | ||
|
||
let allContent = _.chain(channel.post) | ||
.map(post => { | ||
let postType = post.$.type; | ||
let id = post.$.slug; | ||
let date = new Date(post.$.date); | ||
let author = post.$.tumblelog; | ||
let tag = post.$.type; | ||
let title, content; | ||
|
||
switch (postType) { | ||
case "regular": | ||
title = post["regular-title"][0]; | ||
content = post["regular-body"][0]; | ||
break; | ||
case "quote": | ||
title = post["quote-text"][0]; | ||
content = post["quote-text"][0]; | ||
break; | ||
case "conversation": | ||
title = post["conversation-title"][0]; | ||
content = post["conversation-text"][0]; | ||
} | ||
|
||
if (!allAuthors.find(existingAuthor => existingAuthor.id === author)) { | ||
allAuthors.push({ | ||
id: author, | ||
name: author | ||
}); | ||
} | ||
|
||
if (!allTags.find(existingTag => existingTag.id === tag)) { | ||
allTags.push({ | ||
id: tag, | ||
name: tag | ||
}); | ||
} | ||
|
||
|
||
let result = { | ||
id, | ||
title, | ||
authors: [author], | ||
date, | ||
tags: [tag], | ||
content | ||
}; | ||
|
||
return result; | ||
}) | ||
.compact() | ||
.value(); | ||
|
||
return { | ||
tags: allTags, | ||
authors: allAuthors, | ||
content: allContent | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { promisify } = require('util'); | ||
const { parseString } = require('xml2js'); | ||
const _ = require('lodash'); | ||
|
||
const parse = promisify(parseString); | ||
|
||
module.exports = async function worpressImport(fileContents) { | ||
|
||
const data = await parse(fileContents); | ||
|
||
let channel = data.rss.channel[0]; | ||
|
||
let authors = channel['wp:author'].map((author) => { | ||
return { | ||
name: author['wp:author_display_name'][0].trim(), | ||
id: author['wp:author_login'][0].trim(), | ||
} | ||
}) | ||
|
||
let postTags = {}; | ||
|
||
let content = _.chain(channel.item) | ||
.map((post) => { | ||
if(post['wp:post_type'][0].trim() !== 'post') { | ||
return; | ||
} | ||
|
||
// don't import draft posts | ||
if(post['wp:status'][0].trim() !== 'publish') { | ||
return; | ||
} | ||
|
||
let result = { | ||
id: post['wp:post_name'][0].trim(), | ||
title: post.title[0], | ||
authors: [post['dc:creator'][0].trim()], | ||
date: (new Date(post['wp:post_date_gmt'][0])), | ||
tags: (post.category || []).map((category) => { | ||
// collect tags from the posts because wordpress exports don't quite | ||
// include tags correctly | ||
postTags[category.$.nicename] = category._.trim(); | ||
return category.$.nicename | ||
}), | ||
content: post['content:encoded'][0].trim(), | ||
} | ||
|
||
return result; | ||
}) | ||
.compact() | ||
.value(); | ||
|
||
let tags = _.map(postTags, (name, id) => { | ||
return { | ||
id, | ||
name, | ||
} | ||
}) | ||
|
||
return { | ||
tags, | ||
authors, | ||
content, | ||
} | ||
} |
Oops, something went wrong.