mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-07-15 20:53:46 +09:00
Added linting, minor code tweaks, removed getCommand()
This commit is contained in:
+15
-15
@@ -13,9 +13,9 @@ async function parseFilePromise(config) {
|
||||
tagNameProcessors: [xml2js.processors.stripPrefix]
|
||||
});
|
||||
|
||||
let posts = collectPosts(data, config);
|
||||
const posts = collectPosts(data, config);
|
||||
|
||||
let images = [];
|
||||
const images = [];
|
||||
if (config.saveAttachedImages) {
|
||||
images.push(...collectAttachedImages(data));
|
||||
}
|
||||
@@ -34,9 +34,9 @@ function getItemsOfType(data, type) {
|
||||
|
||||
function collectPosts(data, config) {
|
||||
// this is passed into getPostContent() for the markdown conversion
|
||||
turndownService = translator.initTurndownService();
|
||||
const turndownService = translator.initTurndownService();
|
||||
|
||||
let posts = getItemsOfType(data, 'post')
|
||||
const posts = getItemsOfType(data, 'post')
|
||||
.map(post => ({
|
||||
// meta data isn't written to file, but is used to help with other things
|
||||
meta: {
|
||||
@@ -69,8 +69,8 @@ function getPostCoverImageId(post) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let postmeta = post.postmeta.find(postmeta => postmeta.meta_key[0] === '_thumbnail_id');
|
||||
let id = postmeta ? postmeta.meta_value[0] : undefined;
|
||||
const postmeta = post.postmeta.find(postmeta => postmeta.meta_key[0] === '_thumbnail_id');
|
||||
const id = postmeta ? postmeta.meta_value[0] : undefined;
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ function getPostDate(post) {
|
||||
}
|
||||
|
||||
function collectAttachedImages(data) {
|
||||
let images = getItemsOfType(data, 'attachment')
|
||||
const images = getItemsOfType(data, 'attachment')
|
||||
// filter to certain image file types
|
||||
.filter(attachment => (/\.(gif|jpe?g|png)$/i).test(attachment.attachment_url[0]))
|
||||
.map(attachment => ({
|
||||
@@ -97,16 +97,16 @@ function collectAttachedImages(data) {
|
||||
}
|
||||
|
||||
function collectScrapedImages(data) {
|
||||
let images = [];
|
||||
const images = [];
|
||||
getItemsOfType(data, 'post').forEach(post => {
|
||||
let postId = post.post_id[0];
|
||||
let postContent = post.encoded[0];
|
||||
let postLink = post.link[0];
|
||||
const postId = post.post_id[0];
|
||||
const postContent = post.encoded[0];
|
||||
const postLink = post.link[0];
|
||||
|
||||
let matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png))"[^>]*>/gi)];
|
||||
const matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png))"[^>]*>/gi)];
|
||||
matches.forEach(match => {
|
||||
// base the matched image URL relative to the post URL
|
||||
let url = new URL(match[1], postLink).href;
|
||||
const url = new URL(match[1], postLink).href;
|
||||
|
||||
images.push({
|
||||
id: -1,
|
||||
@@ -122,13 +122,13 @@ function collectScrapedImages(data) {
|
||||
|
||||
function mergeImagesIntoPosts(images, posts) {
|
||||
// create lookup table for quicker traversal
|
||||
let postsLookup = posts.reduce((lookup, post) => {
|
||||
const postsLookup = posts.reduce((lookup, post) => {
|
||||
lookup[post.meta.id] = post;
|
||||
return lookup;
|
||||
}, {});
|
||||
|
||||
images.forEach(image => {
|
||||
let post = postsLookup[image.postId];
|
||||
const post = postsLookup[image.postId];
|
||||
if (post) {
|
||||
if (image.id === post.meta.coverImageId) {
|
||||
// save cover image filename to frontmatter
|
||||
|
||||
+6
-6
@@ -1,7 +1,7 @@
|
||||
const turndown = require('turndown');
|
||||
|
||||
function initTurndownService() {
|
||||
let turndownService = new turndown({
|
||||
const turndownService = new turndown({
|
||||
headingStyle: 'atx',
|
||||
bulletListMarker: '-',
|
||||
codeBlockStyle: 'fenced'
|
||||
@@ -20,13 +20,13 @@ function initTurndownService() {
|
||||
// but this series of checks should find the commonalities
|
||||
return (
|
||||
['P', 'DIV'].includes(node.nodeName) &&
|
||||
node.attributes['data-slug-hash'] &&
|
||||
node.attributes['data-slug-hash'] &&
|
||||
node.getAttribute('class') === 'codepen'
|
||||
);
|
||||
},
|
||||
replacement: (content, node) => '\n\n' + node.outerHTML
|
||||
});
|
||||
|
||||
|
||||
// preserve embedded scripts (for tweets, codepens, gists, etc.)
|
||||
turndownService.addRule('script', {
|
||||
filter: 'script',
|
||||
@@ -36,7 +36,7 @@ function initTurndownService() {
|
||||
// keep twitter and codepen <script> tags snug with the element above them
|
||||
before = '\n';
|
||||
}
|
||||
let html = node.outerHTML.replace('async=""', 'async');
|
||||
const html = node.outerHTML.replace('async=""', 'async');
|
||||
return before + html + '\n\n';
|
||||
}
|
||||
});
|
||||
@@ -45,7 +45,7 @@ function initTurndownService() {
|
||||
turndownService.addRule('iframe', {
|
||||
filter: 'iframe',
|
||||
replacement: (content, node) => {
|
||||
let html = node.outerHTML.replace('allowfullscreen=""', 'allowfullscreen');
|
||||
const html = node.outerHTML.replace('allowfullscreen=""', 'allowfullscreen');
|
||||
return '\n\n' + html + '\n\n';
|
||||
}
|
||||
});
|
||||
@@ -64,7 +64,7 @@ function getPostContent(post, turndownService, config) {
|
||||
if (config.saveScrapedImages) {
|
||||
// writeImageFile() will save all content images to a relative /images
|
||||
// folder so update references in post content to match
|
||||
content = content.replace(/(<img[^>]*src=").*?([^\/"]+\.(?:gif|jpe?g|png))("[^>]*>)/gi, '$1images/$2$3');
|
||||
content = content.replace(/(<img[^>]*src=").*?([^/"]+\.(?:gif|jpe?g|png))("[^>]*>)/gi, '$1images/$2$3');
|
||||
}
|
||||
|
||||
// this is a hack to make <iframe> nodes non-empty by inserting a "." which
|
||||
|
||||
+3
-19
@@ -1,5 +1,4 @@
|
||||
const camelcase = require('camelcase');
|
||||
const chalk = require('chalk');
|
||||
const commander = require('commander');
|
||||
const fs = require('fs');
|
||||
const inquirer = require('inquirer');
|
||||
@@ -66,14 +65,14 @@ const options = [
|
||||
}
|
||||
];
|
||||
|
||||
async function getConfig() {
|
||||
async function getConfig(argv) {
|
||||
extendOptionsData();
|
||||
const program = parseCommandLine(process.argv);
|
||||
const program = parseCommandLine(argv);
|
||||
|
||||
let answers;
|
||||
if (program.wizard) {
|
||||
console.log('\nStarting wizard...');
|
||||
let questions = options.map(option => ({
|
||||
const questions = options.map(option => ({
|
||||
when: option.name !== 'wizard' && !option.isProvided,
|
||||
name: camelcase(option.name),
|
||||
type: option.prompt,
|
||||
@@ -160,19 +159,4 @@ function validateFile(value) {
|
||||
return isValid ? true : 'Unable to find file: ' + path.resolve(value);
|
||||
}
|
||||
|
||||
function getCommand(config) {
|
||||
let command = 'node index.js --wizard=false';
|
||||
options.forEach(option => {
|
||||
if (option.name !== 'wizard') {
|
||||
let configKey = camelcase(option.name);
|
||||
let configValue = config[configKey];
|
||||
if (configValue !== option.default) {
|
||||
command += ' --' + option.name + '=' + configValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
return command;
|
||||
}
|
||||
|
||||
exports.getConfig = getConfig;
|
||||
exports.getCommand = getCommand;
|
||||
|
||||
+2
-2
@@ -109,10 +109,10 @@ async function loadImageFilePromise(imageUrl) {
|
||||
}
|
||||
|
||||
function getPostPath(post, config) {
|
||||
let dt = luxon.DateTime.fromISO(post.frontmatter.date);
|
||||
const dt = luxon.DateTime.fromISO(post.frontmatter.date);
|
||||
|
||||
// start with base output dir
|
||||
let pathSegments = [config.output];
|
||||
const pathSegments = [config.output];
|
||||
|
||||
if (config.yearFolders) {
|
||||
pathSegments.push(dt.toFormat('yyyy'));
|
||||
|
||||
Reference in New Issue
Block a user