Skip to content

Commit

Permalink
feat: Initial implementation of tumblr importer
Browse files Browse the repository at this point in the history
Fixes: #47
  • Loading branch information
Michal Bryxí authored and mansona committed Apr 27, 2021
1 parent ff2f961 commit 5bd2a95
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { safeDump } = require('js-yaml');

const importers = require('./importers');

const supportedTypes = ['wordpress', 'ghost'];
const supportedTypes = ['wordpress', 'ghost', 'tumblr'];

function ensureFolder(dir) {
if (!existsSync(dir)){
Expand Down
60 changes: 60 additions & 0 deletions lib/importers/tumblr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 authors = [];
let tags = [];

let content = _.chain(channel.post)
.map(post => {
let postType = post.$.type;
let id = post.$.id;
let date = new Date(post.$.date);
let author = post.$.tumblelog;
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;
}

if (!authors.find(existingAuthor => existingAuthor.id === author)) {
authors.push({
id: author,
name: author
});
}

let result = {
id,
title,
authors,
date,
tags,
content
};

return result;
})
.compact()
.value();

return {
tags,
authors,
content
};
};

0 comments on commit 5bd2a95

Please sign in to comment.