mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-07-19 14:43:58 +09:00
Refactor turndown service init
This commit is contained in:
+3
-6
@@ -52,15 +52,12 @@ function getItemsOfType(channelData, type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function collectPosts(channelData, postTypes) {
|
function collectPosts(channelData, postTypes) {
|
||||||
// this is passed into getPostContent() for the markdown conversion
|
|
||||||
const turndownService = translator.initTurndownService();
|
|
||||||
|
|
||||||
let allPosts = [];
|
let allPosts = [];
|
||||||
postTypes.forEach(postType => {
|
postTypes.forEach(postType => {
|
||||||
const postsForType = getItemsOfType(channelData, postType)
|
const postsForType = getItemsOfType(channelData, postType)
|
||||||
.filter(postData => postData.status[0] !== 'trash')
|
.filter(postData => postData.status[0] !== 'trash')
|
||||||
.filter(postData => !(postType === 'page' && postData.post_name[0] === 'sample-page'))
|
.filter(postData => !(postType === 'page' && postData.post_name[0] === 'sample-page'))
|
||||||
.map(postData => buildPost(postData, turndownService));
|
.map(postData => buildPost(postData));
|
||||||
|
|
||||||
if (postsForType.length > 0) {
|
if (postsForType.length > 0) {
|
||||||
console.log(`${postsForType.length} posts of type "${postType}" found.`);
|
console.log(`${postsForType.length} posts of type "${postType}" found.`);
|
||||||
@@ -72,13 +69,13 @@ function collectPosts(channelData, postTypes) {
|
|||||||
return allPosts;
|
return allPosts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildPost(data, turndownService) {
|
function buildPost(data) {
|
||||||
return {
|
return {
|
||||||
// full raw post data
|
// full raw post data
|
||||||
data,
|
data,
|
||||||
|
|
||||||
// contents of the post in markdown
|
// contents of the post in markdown
|
||||||
content: translator.getPostContent(data, turndownService),
|
content: translator.getPostContent(data.encoded[0]),
|
||||||
|
|
||||||
// these are not written to file, but help with other things
|
// these are not written to file, but help with other things
|
||||||
type: data.post_type[0],
|
type: data.post_type[0],
|
||||||
|
|||||||
+5
-4
@@ -2,7 +2,10 @@ import turndownPluginGfm from '@guyplusplus/turndown-plugin-gfm';
|
|||||||
import turndown from 'turndown';
|
import turndown from 'turndown';
|
||||||
import * as shared from './shared.js';
|
import * as shared from './shared.js';
|
||||||
|
|
||||||
export function initTurndownService() {
|
// init single reusable turndown service object upon import
|
||||||
|
const turndownService = initTurndownService();
|
||||||
|
|
||||||
|
function initTurndownService() {
|
||||||
const turndownService = new turndown({
|
const turndownService = new turndown({
|
||||||
headingStyle: 'atx',
|
headingStyle: 'atx',
|
||||||
bulletListMarker: '-',
|
bulletListMarker: '-',
|
||||||
@@ -95,9 +98,7 @@ export function initTurndownService() {
|
|||||||
return turndownService;
|
return turndownService;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPostContent(postData, turndownService) {
|
export function getPostContent(content) {
|
||||||
let content = postData.encoded[0];
|
|
||||||
|
|
||||||
// insert an empty div element between double line breaks
|
// insert an empty div element between double line breaks
|
||||||
// this nifty trick causes turndown to keep adjacent paragraphs separated
|
// this nifty trick causes turndown to keep adjacent paragraphs separated
|
||||||
// without mucking up content inside of other elements (like <code> blocks)
|
// without mucking up content inside of other elements (like <code> blocks)
|
||||||
|
|||||||
Reference in New Issue
Block a user