2019-12-15 13:44:04 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const luxon = require('luxon');
|
|
|
|
|
const xml2js = require('xml2js');
|
|
|
|
|
|
|
|
|
|
const shared = require('./shared');
|
2019-12-17 13:52:09 -05:00
|
|
|
const translator = require('./translator');
|
2019-12-15 13:44:04 -05:00
|
|
|
|
2019-12-17 14:03:58 -05:00
|
|
|
async function parseFilePromise(config) {
|
2020-01-14 10:26:50 -05:00
|
|
|
console.log('\nParsing...');
|
2019-12-21 15:57:25 -05:00
|
|
|
const content = await fs.promises.readFile(config.input, 'utf8');
|
2019-12-18 16:36:43 -05:00
|
|
|
const data = await xml2js.parseStringPromise(content, {
|
|
|
|
|
trim: true,
|
|
|
|
|
tagNameProcessors: [xml2js.processors.stripPrefix]
|
|
|
|
|
});
|
2019-12-15 13:44:04 -05:00
|
|
|
|
2020-01-19 11:42:17 -05:00
|
|
|
const posts = collectPosts(data, config);
|
2019-12-19 13:17:43 -05:00
|
|
|
|
2020-01-19 11:42:17 -05:00
|
|
|
const images = [];
|
2020-01-12 09:03:32 -05:00
|
|
|
if (config.saveAttachedImages) {
|
2019-12-19 12:35:33 -05:00
|
|
|
images.push(...collectAttachedImages(data));
|
|
|
|
|
}
|
2020-01-12 09:03:32 -05:00
|
|
|
if (config.saveScrapedImages) {
|
2019-12-19 12:35:33 -05:00
|
|
|
images.push(...collectScrapedImages(data));
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
mergeImagesIntoPosts(images, posts);
|
2019-12-17 14:03:58 -05:00
|
|
|
|
2019-12-21 15:57:25 -05:00
|
|
|
return posts;
|
2019-12-15 13:44:04 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-19 13:17:43 -05:00
|
|
|
function getItemsOfType(data, type) {
|
|
|
|
|
return data.rss.channel[0].item.filter(item => item.post_type[0] === type);
|
2019-12-15 13:44:04 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-12 09:03:32 -05:00
|
|
|
function collectPosts(data, config) {
|
2019-12-15 13:44:04 -05:00
|
|
|
// this is passed into getPostContent() for the markdown conversion
|
2020-01-19 11:42:17 -05:00
|
|
|
const turndownService = translator.initTurndownService();
|
2019-12-15 13:44:04 -05:00
|
|
|
|
2020-01-19 11:42:17 -05:00
|
|
|
const posts = getItemsOfType(data, 'post')
|
2020-01-20 13:52:21 -05:00
|
|
|
.filter(post => post.status[0] !== 'trash' && post.status[0] !== 'draft')
|
2019-12-15 13:44:04 -05:00
|
|
|
.map(post => ({
|
|
|
|
|
// meta data isn't written to file, but is used to help with other things
|
|
|
|
|
meta: {
|
|
|
|
|
id: getPostId(post),
|
|
|
|
|
slug: getPostSlug(post),
|
2019-12-19 12:35:33 -05:00
|
|
|
coverImageId: getPostCoverImageId(post),
|
|
|
|
|
imageUrls: []
|
2019-12-15 13:44:04 -05:00
|
|
|
},
|
|
|
|
|
frontmatter: {
|
|
|
|
|
title: getPostTitle(post),
|
2020-06-23 22:25:12 -04:00
|
|
|
date: getPostDate(post),
|
|
|
|
|
categories: getCategories(post),
|
|
|
|
|
tags: getTags(post),
|
2019-12-15 13:44:04 -05:00
|
|
|
},
|
2019-12-17 13:52:09 -05:00
|
|
|
content: translator.getPostContent(post, turndownService, config)
|
2019-12-15 13:44:04 -05:00
|
|
|
}));
|
|
|
|
|
|
2019-12-19 13:17:43 -05:00
|
|
|
console.log(posts.length + ' posts found.');
|
|
|
|
|
return posts;
|
2019-12-15 13:44:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPostId(post) {
|
|
|
|
|
return post.post_id[0];
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 13:50:50 -05:00
|
|
|
function getPostSlug(post) {
|
|
|
|
|
return post.post_name[0];
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
function getPostCoverImageId(post) {
|
2020-01-12 13:50:50 -05:00
|
|
|
if (post.postmeta === undefined) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-19 11:42:17 -05:00
|
|
|
const postmeta = post.postmeta.find(postmeta => postmeta.meta_key[0] === '_thumbnail_id');
|
|
|
|
|
const id = postmeta ? postmeta.meta_value[0] : undefined;
|
2019-12-15 13:44:04 -05:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPostTitle(post) {
|
2019-12-21 15:57:25 -05:00
|
|
|
return post.title[0];
|
2019-12-15 13:44:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPostDate(post) {
|
2020-06-23 22:25:12 -04:00
|
|
|
return luxon.DateTime.fromRFC2822(post.pubDate[0], { zone: 'utc' }).toISO();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCategories(post) {
|
|
|
|
|
return processCategoryTags(post, "category");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTags(post) {
|
|
|
|
|
return processCategoryTags(post, "post_tag");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function processCategoryTags(post, domain) {
|
2020-06-28 11:21:04 -04:00
|
|
|
if (!post.category) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2020-06-23 22:25:12 -04:00
|
|
|
return post.category
|
|
|
|
|
.filter(c => c["$"].domain === domain)
|
2020-06-28 11:21:04 -04:00
|
|
|
.map(({ $: c }) => c.nicename);
|
2019-12-15 13:44:04 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-19 13:17:43 -05:00
|
|
|
function collectAttachedImages(data) {
|
2020-01-19 11:42:17 -05:00
|
|
|
const images = getItemsOfType(data, 'attachment')
|
2019-12-19 13:17:43 -05:00
|
|
|
// filter to certain image file types
|
|
|
|
|
.filter(attachment => (/\.(gif|jpe?g|png)$/i).test(attachment.attachment_url[0]))
|
|
|
|
|
.map(attachment => ({
|
|
|
|
|
id: attachment.post_id[0],
|
|
|
|
|
postId: attachment.post_parent[0],
|
|
|
|
|
url: attachment.attachment_url[0]
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
console.log(images.length + ' attached images found.');
|
|
|
|
|
return images;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function collectScrapedImages(data) {
|
2020-01-19 11:42:17 -05:00
|
|
|
const images = [];
|
2019-12-19 13:17:43 -05:00
|
|
|
getItemsOfType(data, 'post').forEach(post => {
|
2020-01-19 11:42:17 -05:00
|
|
|
const postId = post.post_id[0];
|
|
|
|
|
const postContent = post.encoded[0];
|
|
|
|
|
const postLink = post.link[0];
|
2019-12-19 13:17:43 -05:00
|
|
|
|
2020-01-19 11:42:17 -05:00
|
|
|
const matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png))"[^>]*>/gi)];
|
2019-12-19 13:17:43 -05:00
|
|
|
matches.forEach(match => {
|
|
|
|
|
// base the matched image URL relative to the post URL
|
2020-01-19 11:42:17 -05:00
|
|
|
const url = new URL(match[1], postLink).href;
|
2019-12-19 13:17:43 -05:00
|
|
|
|
|
|
|
|
images.push({
|
|
|
|
|
id: -1,
|
|
|
|
|
postId: postId,
|
|
|
|
|
url: url
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(images.length + ' images scraped from post body content.');
|
|
|
|
|
return images;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
function mergeImagesIntoPosts(images, posts) {
|
|
|
|
|
images.forEach(image => {
|
2020-12-20 13:30:44 -05:00
|
|
|
posts.forEach(post => {
|
|
|
|
|
let shouldAttach = false;
|
|
|
|
|
|
|
|
|
|
// this image was uploaded as an attachment to this post
|
|
|
|
|
if (image.postId === post.meta.id) {
|
|
|
|
|
shouldAttach = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this image was set as the featured image for this post
|
2019-12-15 13:44:04 -05:00
|
|
|
if (image.id === post.meta.coverImageId) {
|
2020-12-20 13:30:44 -05:00
|
|
|
shouldAttach = true;
|
2019-12-15 13:44:04 -05:00
|
|
|
post.frontmatter.coverImage = shared.getFilenameFromUrl(image.url);
|
|
|
|
|
}
|
2020-12-20 13:30:44 -05:00
|
|
|
|
|
|
|
|
if (shouldAttach && !post.meta.imageUrls.includes(image.url)) {
|
2019-12-19 12:35:33 -05:00
|
|
|
post.meta.imageUrls.push(image.url);
|
|
|
|
|
}
|
2020-12-20 13:30:44 -05:00
|
|
|
});
|
2019-12-15 13:44:04 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 13:52:09 -05:00
|
|
|
exports.parseFilePromise = parseFilePromise;
|