mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-06-05 15:09:59 +09:00
Add as a wizard question, automatically gather post types
This commit is contained in:
+18
-3
@@ -29,6 +29,20 @@ async function parseFilePromise(config) {
|
|||||||
return posts;
|
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) {
|
function getItemsOfType(data, type) {
|
||||||
return data.rss.channel[0].item.filter(item => item.post_type[0] === 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
|
// this is passed into getPostContent() for the markdown conversion
|
||||||
const turndownService = translator.initTurndownService();
|
const turndownService = translator.initTurndownService();
|
||||||
|
|
||||||
|
const types = getItemTypes(data, config);
|
||||||
let allPosts = [];
|
let allPosts = [];
|
||||||
settings.post_types.forEach(postType => {
|
types.forEach(postType => {
|
||||||
const postsForType = getItemsOfType(data, postType)
|
const postsForType = getItemsOfType(data, postType)
|
||||||
.filter(post => post.status[0] !== 'trash' && post.status[0] !== 'draft')
|
.filter(post => post.status[0] !== 'trash' && post.status[0] !== 'draft')
|
||||||
.map(post => ({
|
.map(post => ({
|
||||||
@@ -59,14 +74,14 @@ function collectPosts(data, config) {
|
|||||||
content: translator.getPostContent(post, turndownService, config)
|
content: translator.getPostContent(post, turndownService, config)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (settings.post_types.length > 1) {
|
if (types.length > 1) {
|
||||||
console.log(`${postsForType.length} "${postType}" posts found.`);
|
console.log(`${postsForType.length} "${postType}" posts found.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
allPosts.push(...postsForType);
|
allPosts.push(...postsForType);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (settings.post_types.length === 1) {
|
if (types.length === 1) {
|
||||||
console.log(allPosts.length + ' posts found.');
|
console.log(allPosts.length + ' posts found.');
|
||||||
}
|
}
|
||||||
return allPosts;
|
return allPosts;
|
||||||
|
|||||||
@@ -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
|
// time in ms to wait between requesting image files
|
||||||
// increase this if you see timeouts or server errors
|
// increase this if you see timeouts or server errors
|
||||||
exports.image_file_request_delay = 500;
|
exports.image_file_request_delay = 500;
|
||||||
|
|||||||
+7
-2
@@ -3,11 +3,10 @@ const commander = require('commander');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const inquirer = require('inquirer');
|
const inquirer = require('inquirer');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const process = require('process');
|
|
||||||
|
|
||||||
const package = require('../package.json');
|
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 = [
|
const options = [
|
||||||
// wizard must always be first
|
// wizard must always be first
|
||||||
{
|
{
|
||||||
@@ -69,6 +68,12 @@ const options = [
|
|||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
description: 'Save images scraped from post body content',
|
description: 'Save images scraped from post body content',
|
||||||
default: true
|
default: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'include-other-types',
|
||||||
|
type: 'boolean',
|
||||||
|
description: 'Include custom post types and pages',
|
||||||
|
default: false
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -132,8 +132,8 @@ function getPostPath(post, config) {
|
|||||||
// start with base output dir
|
// start with base output dir
|
||||||
const pathSegments = [config.output];
|
const pathSegments = [config.output];
|
||||||
|
|
||||||
// create fragment for post type, if there's more than one
|
// create fragment for post type if we're dealing with more than just "post"
|
||||||
if (settings.post_types.length > 1) {
|
if (config.includeOtherTypes) {
|
||||||
pathSegments.push(post.meta.type);
|
pathSegments.push(post.meta.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user