mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-06-05 15:09:59 +09:00
Filterting for categories, omit empty lists in frontmatter
This commit is contained in:
+7
-5
@@ -51,7 +51,7 @@ function collectPosts(data, config) {
|
|||||||
title: getPostTitle(post),
|
title: getPostTitle(post),
|
||||||
date: getPostDate(post),
|
date: getPostDate(post),
|
||||||
categories: getCategories(post),
|
categories: getCategories(post),
|
||||||
tags: getTags(post),
|
tags: getTags(post)
|
||||||
},
|
},
|
||||||
content: translator.getPostContent(post, turndownService, config)
|
content: translator.getPostContent(post, turndownService, config)
|
||||||
}));
|
}));
|
||||||
@@ -95,20 +95,22 @@ function getPostDate(post) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCategories(post) {
|
function getCategories(post) {
|
||||||
return processCategoryTags(post, "category");
|
const categories = processCategoryTags(post, 'category');
|
||||||
|
return categories.filter(category => !settings.filter_categories.includes(category));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTags(post) {
|
function getTags(post) {
|
||||||
return processCategoryTags(post, "post_tag");
|
return processCategoryTags(post, 'post_tag');
|
||||||
}
|
}
|
||||||
|
|
||||||
function processCategoryTags(post, domain) {
|
function processCategoryTags(post, domain) {
|
||||||
if (!post.category) {
|
if (!post.category) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return post.category
|
return post.category
|
||||||
.filter(c => c["$"].domain === domain)
|
.filter(category => category.$.domain === domain)
|
||||||
.map(({ $: c }) => c.nicename);
|
.map(({ $: attributes }) => attributes.nicename);
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectAttachedImages(data) {
|
function collectAttachedImages(data) {
|
||||||
|
|||||||
@@ -14,3 +14,7 @@ exports.include_time_with_date = false;
|
|||||||
// tokens are documented here: https://moment.github.io/luxon/docs/manual/formatting.html#table-of-tokens
|
// tokens are documented here: https://moment.github.io/luxon/docs/manual/formatting.html#table-of-tokens
|
||||||
// if set, this takes precedence over include_time_with_date
|
// if set, this takes precedence over include_time_with_date
|
||||||
exports.custom_date_formatting = '';
|
exports.custom_date_formatting = '';
|
||||||
|
|
||||||
|
// categories to be excluded from post frontmatter
|
||||||
|
// this does not filter out posts themselves, just the categories listed in their frontmatter
|
||||||
|
exports.filter_categories = ['uncategorized'];
|
||||||
|
|||||||
+19
-7
@@ -56,14 +56,26 @@ async function writeMarkdownFilesPromise(posts, config ) {
|
|||||||
|
|
||||||
async function loadMarkdownFilePromise(post) {
|
async function loadMarkdownFilePromise(post) {
|
||||||
let output = '---\n';
|
let output = '---\n';
|
||||||
Object.entries(post.frontmatter).forEach(pair => {
|
|
||||||
const key = pair[0];
|
Object.entries(post.frontmatter).forEach(([key, value]) => {
|
||||||
const value = Array.isArray(pair[1])
|
let outputValue;
|
||||||
? (pair[1].length === 0 ? "" : "\n - \"" + pair[1].join("\"\n - \"") + "\"")
|
if (Array.isArray(value)) {
|
||||||
: '"' + (pair[1] || '').replace(/"/g, '\\"') +'"';
|
if (value.length > 0) {
|
||||||
output += key + ': ' + value + '\n';
|
// array of one or more strings
|
||||||
|
outputValue = value.reduce((list, item) => `${list}\n - "${item}"`, '');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// single string value
|
||||||
|
const escapedValue = (value || '').replace(/"/g, '\\"');
|
||||||
|
outputValue = `"${escapedValue}"`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (outputValue !== undefined) {
|
||||||
|
output += `${key}: ${outputValue}\n`;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
output += '---\n\n' + post.content + '\n';
|
|
||||||
|
output += `---\n\n${post.content}\n`;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user