From 108dafd100e3bfa4c0177d981d29b081946b3e61 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Sat, 26 Dec 2020 16:27:16 -0500 Subject: [PATCH] Add as a wizard question, automatically gather post types --- src/parser.js | 21 ++++++++++++++++++--- src/settings.js | 4 ---- src/wizard.js | 9 +++++++-- src/writer.js | 4 ++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/parser.js b/src/parser.js index 11c7db0..8afa320 100644 --- a/src/parser.js +++ b/src/parser.js @@ -29,6 +29,20 @@ async function parseFilePromise(config) { return posts; } +function getItemTypes(data, config) { + if (config.includeOtherTypes) { + // search export file for all post types minus some default types we don't want + // effectively this will be 'post', 'page', and custom post types + const types = data.rss.channel[0].item + .map(item => item.post_type[0]) + .filter(type => !['attachment', 'revision', 'nav_menu_item'].includes(type)); + return [...new Set(types)]; // remove duplicates + } else { + // just plain old vanilla "post" posts + return ['post']; + } +} + function getItemsOfType(data, type) { return data.rss.channel[0].item.filter(item => item.post_type[0] === type); } @@ -37,8 +51,9 @@ function collectPosts(data, config) { // this is passed into getPostContent() for the markdown conversion const turndownService = translator.initTurndownService(); + const types = getItemTypes(data, config); let allPosts = []; - settings.post_types.forEach(postType => { + types.forEach(postType => { const postsForType = getItemsOfType(data, postType) .filter(post => post.status[0] !== 'trash' && post.status[0] !== 'draft') .map(post => ({ @@ -59,14 +74,14 @@ function collectPosts(data, config) { content: translator.getPostContent(post, turndownService, config) })); - if (settings.post_types.length > 1) { + if (types.length > 1) { console.log(`${postsForType.length} "${postType}" posts found.`); } allPosts.push(...postsForType); }); - if (settings.post_types.length === 1) { + if (types.length === 1) { console.log(allPosts.length + ' posts found.'); } return allPosts; diff --git a/src/settings.js b/src/settings.js index 78e90e1..9425ce9 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1,7 +1,3 @@ -// the post types you want to export (the default is plain old vanilla "post") -// if you specify multiple post types, then a folder will be created for each within the output folder -exports.post_types = ['post']; - // time in ms to wait between requesting image files // increase this if you see timeouts or server errors exports.image_file_request_delay = 500; diff --git a/src/wizard.js b/src/wizard.js index e3e87dc..5285b8d 100644 --- a/src/wizard.js +++ b/src/wizard.js @@ -3,11 +3,10 @@ const commander = require('commander'); const fs = require('fs'); const inquirer = require('inquirer'); const path = require('path'); -const process = require('process'); const package = require('../package.json'); -// all user options for command line and wizard are declard here +// all user options for command line and wizard are declared here const options = [ // wizard must always be first { @@ -69,6 +68,12 @@ const options = [ type: 'boolean', description: 'Save images scraped from post body content', default: true + }, + { + name: 'include-other-types', + type: 'boolean', + description: 'Include custom post types and pages', + default: false } ]; diff --git a/src/writer.js b/src/writer.js index 9d4362e..fad06a3 100644 --- a/src/writer.js +++ b/src/writer.js @@ -132,8 +132,8 @@ function getPostPath(post, config) { // start with base output dir const pathSegments = [config.output]; - // create fragment for post type, if there's more than one - if (settings.post_types.length > 1) { + // create fragment for post type if we're dealing with more than just "post" + if (config.includeOtherTypes) { pathSegments.push(post.meta.type); }