diff --git a/index.js b/index.js index b3dbc24..47f061a 100644 --- a/index.js +++ b/index.js @@ -187,7 +187,6 @@ function initTurndownService() { } }); - return turndownService; } @@ -220,9 +219,14 @@ function getPostDate(post) { function getPostContent(post, turndownService) { let content = post.encoded[0].trim(); + // insert an empty div element between double line breaks + // this nifty trick causes turndown to keep adjacent paragraphs separated + // without mucking up content inside of other elemnts (like blocks) + content = content.replace(/(\r?\n){2}/g, '\n
\n'); + if (argv.addcontentimages) { - // writeImageFile() will save all content images to a relative /images folder - // so update references in post content to match + // writeImageFile() will save all content images to a relative /images + // folder so update references in post content to match content = content.replace(/(]*src=").*?([^\/"]+\.(?:gif|jpg|png))("[^>]*>)/gi, '$1images/$2$3'); } @@ -231,10 +235,13 @@ function getPostContent(post, turndownService) { // (using turndown's blankRule() and keep() solution did not work for me) content = content.replace(/(<\/iframe>)/gi, '.$1'); - content = turndownService.turndown(content) - .replace(/-\s+/g, '- '); // clean up extra spaces + // use turndown to convert HTML to Markdown + content = turndownService.turndown(content); - // clean up the "." from the hack above + // clean up extra spaces in list items + content = content.replace(/- +/g, '- '); + + // clean up the "." from the iframe hack above content = content.replace(/\.(<\/iframe>)/gi, '$1'); return content;