mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-07-19 22:54:11 +09:00
Split out frontmatter getters
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
const settings = require('../settings');
|
||||||
|
|
||||||
|
module.exports = (post) => {
|
||||||
|
if (!post.data.category) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const categories = post.data.category
|
||||||
|
.filter(category => category.$.domain === 'category')
|
||||||
|
.map(({ $: attributes }) => decodeURIComponent(attributes.nicename));
|
||||||
|
|
||||||
|
return categories.filter(category => !settings.filter_categories.includes(category));
|
||||||
|
};
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (post) => {
|
||||||
|
return post.meta.coverImage;
|
||||||
|
};
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
const luxon = require('luxon');
|
||||||
|
|
||||||
|
const settings = require('../settings');
|
||||||
|
|
||||||
|
module.exports = (post) => {
|
||||||
|
const dateTime = luxon.DateTime.fromRFC2822(post.data.pubDate[0], { zone: 'utc' });
|
||||||
|
|
||||||
|
if (settings.custom_date_formatting) {
|
||||||
|
return dateTime.toFormat(settings.custom_date_formatting);
|
||||||
|
} else if (settings.include_time_with_date) {
|
||||||
|
return dateTime.toISO();
|
||||||
|
} else {
|
||||||
|
return dateTime.toISODate();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
const settings = require('../settings');
|
||||||
|
|
||||||
|
module.exports = (post) => {
|
||||||
|
if (!post.data.category) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const categories = post.data.category
|
||||||
|
.filter(category => category.$.domain === 'post_tag')
|
||||||
|
.map(({ $: attributes }) => decodeURIComponent(attributes.nicename));
|
||||||
|
|
||||||
|
return categories;
|
||||||
|
};
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (post) => {
|
||||||
|
return post.data.title[0];
|
||||||
|
};
|
||||||
+13
-47
@@ -1,11 +1,17 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const luxon = require('luxon');
|
|
||||||
const xml2js = require('xml2js');
|
const xml2js = require('xml2js');
|
||||||
|
|
||||||
const shared = require('./shared');
|
const shared = require('./shared');
|
||||||
const settings = require('./settings');
|
|
||||||
const translator = require('./translator');
|
const translator = require('./translator');
|
||||||
|
|
||||||
|
const frontmatter = {
|
||||||
|
title: require('./frontmatter/title'),
|
||||||
|
date: require('./frontmatter/date'),
|
||||||
|
categories: require('./frontmatter/categories'),
|
||||||
|
tags: require('./frontmatter/tags'),
|
||||||
|
coverImage: require('./frontmatter/coverImage'),
|
||||||
|
};
|
||||||
|
|
||||||
async function parseFilePromise(config) {
|
async function parseFilePromise(config) {
|
||||||
console.log('\nParsing...');
|
console.log('\nParsing...');
|
||||||
const content = await fs.promises.readFile(config.input, 'utf8');
|
const content = await fs.promises.readFile(config.input, 'utf8');
|
||||||
@@ -104,45 +110,6 @@ function getPostCoverImageId(postData) {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPostTitle(post) {
|
|
||||||
return post.data.title[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPostDate(post) {
|
|
||||||
const dateTime = luxon.DateTime.fromRFC2822(post.data.pubDate[0], { zone: 'utc' });
|
|
||||||
|
|
||||||
if (settings.custom_date_formatting) {
|
|
||||||
return dateTime.toFormat(settings.custom_date_formatting);
|
|
||||||
} else if (settings.include_time_with_date) {
|
|
||||||
return dateTime.toISO();
|
|
||||||
} else {
|
|
||||||
return dateTime.toISODate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCategories(post) {
|
|
||||||
const categories = processCategoryTags(post.data, 'category');
|
|
||||||
return categories.filter(category => !settings.filter_categories.includes(category));
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTags(post) {
|
|
||||||
return processCategoryTags(post.data, 'post_tag');
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCoverImage(post) {
|
|
||||||
return post.meta.coverImage;
|
|
||||||
}
|
|
||||||
|
|
||||||
function processCategoryTags(postData, domain) {
|
|
||||||
if (!postData.category) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return postData.category
|
|
||||||
.filter(category => category.$.domain === domain)
|
|
||||||
.map(({ $: attributes }) => decodeURIComponent(attributes.nicename));
|
|
||||||
}
|
|
||||||
|
|
||||||
function collectAttachedImages(channelData) {
|
function collectAttachedImages(channelData) {
|
||||||
const images = getItemsOfType(channelData, 'attachment')
|
const images = getItemsOfType(channelData, 'attachment')
|
||||||
// filter to certain image file types
|
// filter to certain image file types
|
||||||
@@ -207,13 +174,12 @@ function mergeImagesIntoPosts(images, posts) {
|
|||||||
|
|
||||||
function populateFrontmatter(posts) {
|
function populateFrontmatter(posts) {
|
||||||
posts.forEach(post => {
|
posts.forEach(post => {
|
||||||
console.log(post);
|
|
||||||
post.frontmatter = {
|
post.frontmatter = {
|
||||||
title: getPostTitle(post),
|
title: frontmatter.title(post),
|
||||||
date: getPostDate(post),
|
date: frontmatter.date(post),
|
||||||
categories: getCategories(post),
|
categories: frontmatter.categories(post),
|
||||||
tags: getTags(post),
|
tags: frontmatter.tags(post),
|
||||||
coverImage: getCoverImage(post)
|
coverImage: frontmatter.coverImage(post)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,11 @@
|
|||||||
|
exports.frontmatter_fields = [
|
||||||
|
'title',
|
||||||
|
'date',
|
||||||
|
'categories',
|
||||||
|
'tags',
|
||||||
|
'coverImage'
|
||||||
|
];
|
||||||
|
|
||||||
// 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user