From 5bd2a95fbdce648f5d5aade2136d21cc464f465a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bryxi=CC=81?= Date: Sat, 17 Aug 2019 10:49:03 +0100 Subject: [PATCH] feat: Initial implementation of tumblr importer Fixes: #47 --- lib/import.js | 2 +- lib/importers/tumblr.js | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 lib/importers/tumblr.js diff --git a/lib/import.js b/lib/import.js index e7258ec4..b5928ff8 100644 --- a/lib/import.js +++ b/lib/import.js @@ -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)){ diff --git a/lib/importers/tumblr.js b/lib/importers/tumblr.js new file mode 100644 index 00000000..9eb37622 --- /dev/null +++ b/lib/importers/tumblr.js @@ -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 + }; +};