From faff0ec8562bc3d0f7f7e1296a05ad1b02dda3a3 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Sun, 2 Feb 2025 12:37:49 -0500 Subject: [PATCH] filter-categories and strict-ssl questions --- src/frontmatter.js | 4 ++-- src/questions.js | 10 ++++++++++ src/writer.js | 21 ++++++++++----------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/frontmatter.js b/src/frontmatter.js index ce4522b..6b62c3b 100644 --- a/src/frontmatter.js +++ b/src/frontmatter.js @@ -8,7 +8,7 @@ export function author(post) { } // get array of decoded category names, filtered as specified in settings -export function categories(post) { +export function categories(post, config) { if (!post.data.category) { return []; } @@ -17,7 +17,7 @@ export function categories(post) { .filter(category => category.$.domain === 'category') .map(({ $: attributes }) => decodeURIComponent(attributes.nicename)); - return categories.filter(category => !settings.filter_categories.includes(category)); + return categories.filter((category) => !config.filterCategories.includes(category)); } // get cover image filename, previously decoded and set on post.meta diff --git a/src/questions.js b/src/questions.js index a2db567..b08d2df 100644 --- a/src/questions.js +++ b/src/questions.js @@ -125,4 +125,14 @@ export const all = [ type: 'boolean', default: false }, + { + name: 'filter-categories', + type: 'list', + default: ['uncategorized'] + }, + { + name: 'strict-ssl', + type: 'boolean', + default: true + } ]; diff --git a/src/writer.js b/src/writer.js index ba9fe7d..d8f8701 100644 --- a/src/writer.js +++ b/src/writer.js @@ -4,7 +4,6 @@ import fs from 'fs'; import http from 'http'; import https from 'https'; import path from 'path'; -import * as settings from './settings.js'; import * as shared from './shared.js'; export async function writeFilesPromise(posts, config) { @@ -12,11 +11,11 @@ export async function writeFilesPromise(posts, config) { await writeImageFilesPromise(posts, config); } -async function processPayloadsPromise(payloads, loadFunc) { +async function processPayloadsPromise(payloads, loadFunc, config) { const promises = payloads.map(payload => new Promise((resolve, reject) => { setTimeout(async () => { try { - const data = await loadFunc(payload.item); + const data = await loadFunc(payload.item, config); await writeFile(payload.destinationPath, data); console.log(chalk.green('[OK]') + ' ' + payload.name); resolve(); @@ -68,7 +67,7 @@ async function writeMarkdownFilesPromise(posts, config) { console.log('\nNo posts to save...'); } else { console.log(`\nSaving ${remainingCount} posts (${skipCount} already exist)...`); - await processPayloadsPromise(payloads, loadMarkdownFilePromise); + await processPayloadsPromise(payloads, loadMarkdownFilePromise, config); } } @@ -131,15 +130,15 @@ async function writeImageFilesPromise(posts, config) { console.log('\nNo images to download and save...'); } else { console.log(`\nDownloading and saving ${remainingCount} images (${skipCount} already exist)...`); - await processPayloadsPromise(payloads, loadImageFilePromise); + await processPayloadsPromise(payloads, loadImageFilePromise, config); } } -async function loadImageFilePromise(imageUrl) { +async function loadImageFilePromise(imageUrl, config) { // only encode the URL if it doesn't already have encoded characters const url = (/%[\da-f]{2}/i).test(imageUrl) ? imageUrl : encodeURI(imageUrl); - const config = { + const requestConfig = { method: 'get', url, headers: { @@ -148,15 +147,15 @@ async function loadImageFilePromise(imageUrl) { responseType: 'arraybuffer' }; - if (!settings.strict_ssl) { + if (!config.strictSsl) { // custom agents to disable SSL errors (adding both http and https, just in case) - config.httpAgent = new http.Agent({ rejectUnauthorized: false }); - config.httpsAgent = new https.Agent({ rejectUnauthorized: false }); + requestConfig.httpAgent = new http.Agent({ rejectUnauthorized: false }); + requestConfig.httpsAgent = new https.Agent({ rejectUnauthorized: false }); } let buffer; try { - const response = await axios(config); + const response = await axios(requestConfig); buffer = Buffer.from(response.data, 'binary'); } catch (ex) { if (ex.response) {