From f6197d1d70cf8e1e1399a67c21f01c1189197f76 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Sat, 1 Feb 2025 08:27:58 -0500 Subject: [PATCH] First list and integer questions, spaces to tabs --- src/frontmatter.js | 30 +++++++++++------------ src/normalizers.js | 59 +++++++++++++++++++++++++++++----------------- src/parser.js | 6 ++--- src/questions.js | 26 ++++++++++++++------ src/settings.js | 16 ++++++------- src/writer.js | 2 +- 6 files changed, 84 insertions(+), 55 deletions(-) diff --git a/src/frontmatter.js b/src/frontmatter.js index 390815a..029832b 100644 --- a/src/frontmatter.js +++ b/src/frontmatter.js @@ -9,15 +9,15 @@ export function getAuthor(post) { // get array of decoded category names, filtered as specified in settings export function getCategories(post) { - if (!post.data.category) { - return []; - } + if (!post.data.category) { + return []; + } - const categories = post.data.category - .filter(category => category.$.domain === 'category') - .map(({ $: attributes }) => decodeURIComponent(attributes.nicename)); + const categories = post.data.category + .filter(category => category.$.domain === 'category') + .map(({ $: attributes }) => decodeURIComponent(attributes.nicename)); - return categories.filter(category => !settings.filter_categories.includes(category)); + return categories.filter(category => !settings.filter_categories.includes(category)); } // get cover image filename, previously decoded and set on post.meta @@ -29,15 +29,15 @@ export function getCoverImage(post) { // get post date, optionally formatted as specified in settings // this value is also used for year/month folders, date prefixes, etc. as needed export function getDate(post) { - const dateTime = luxon.DateTime.fromRFC2822(post.data.pubDate[0], { zone: settings.custom_date_timezone }); + const dateTime = luxon.DateTime.fromRFC2822(post.data.pubDate[0], { zone: settings.custom_date_timezone }); - if (settings.custom_date_formatting) { - return dateTime.toFormat(settings.custom_date_formatting); - } else if (settings.include_time_with_date) { - return dateTime.toISO(); - } else { - return dateTime.toISODate(); - } + if (settings.custom_date_formatting) { + return dateTime.toFormat(settings.custom_date_formatting); + } else if (settings.include_time_with_date) { + return dateTime.toISO(); + } else { + return dateTime.toISODate(); + } } // get excerpt, not decoded, newlines collapsed diff --git a/src/normalizers.js b/src/normalizers.js index 5234646..b6674e0 100644 --- a/src/normalizers.js +++ b/src/normalizers.js @@ -2,31 +2,48 @@ import fs from 'fs'; import path from 'path'; export function boolean(value) { - if (typeof value === 'boolean') { - return value; - } else if (value === 'true') { - return true; - } else if (value === 'false') { - return false; - } + if (typeof value === 'boolean') { + return value; + } else if (value === 'true') { + return true; + } else if (value === 'false') { + return false; + } - throw new Error('Must be true or false.'); + throw new Error('Must be true or false.'); } export function filePath(value) { - const unwrapped = value.replace(/"(.*?)"/, '$1'); - const absolute = path.resolve(unwrapped); + const unwrapped = value.replace(/"(.*?)"/, '$1'); + const absolute = path.resolve(unwrapped); - let fileExists; - try { - fileExists = fs.existsSync(absolute) && fs.statSync(absolute).isFile(); - } catch (ex) { - fileExists = false; - } + let fileExists; + try { + fileExists = fs.existsSync(absolute) && fs.statSync(absolute).isFile(); + } catch (ex) { + fileExists = false; + } - if (fileExists) { - return absolute; - } else { - throw new Error('File not found at ' + absolute + '.'); - } + if (fileExists) { + return absolute; + } + + throw new Error('File not found at ' + absolute + '.'); +} + +export function list(value) { + if (Array.isArray(value)) { + return value; + } else { + return value.trim().split(/\s*,\s*/); + } +} + +export function integer(value) { + const int = parseInt(value); + if (!Number.isNaN(int) && int >= 0) { + return int; + } + + throw new Error('Must be an integer >= 0.'); } diff --git a/src/parser.js b/src/parser.js index 08775ad..0109dda 100644 --- a/src/parser.js +++ b/src/parser.js @@ -26,7 +26,7 @@ export async function parseFilePromise(config) { } mergeImagesIntoPosts(images, posts); - populateFrontmatter(posts); + populateFrontmatter(posts, config); return posts; } @@ -162,10 +162,10 @@ function mergeImagesIntoPosts(images, posts) { }); } -function populateFrontmatter(posts) { +function populateFrontmatter(posts, config) { posts.forEach(post => { post.frontmatter = {}; - settings.frontmatter_fields.forEach(field => { + config.frontmatterFields.forEach(field => { const [key, alias] = field.split(':'); let frontmatterGetter = frontmatter['get' + key.replace(/^./, (match) => match.toUpperCase())]; diff --git a/src/questions.js b/src/questions.js index 6269338..4a7f1de 100644 --- a/src/questions.js +++ b/src/questions.js @@ -1,6 +1,14 @@ import * as inquirer from '@inquirer/prompts'; +// questions with a description are displayed in command line help +// questions with a prompt are included in the wizard (if not set on the command line) export const all = [ + { + name: 'wizard', + type: 'boolean', + description: 'Use wizard', + default: true + }, { name: 'input', type: 'file-path', @@ -91,16 +99,20 @@ export const all = [ ], prompt: inquirer.select }, - { - name: 'wizard', - type: 'boolean', - description: 'Use wizard', - default: true - }, { name: 'output', type: 'folder-path', description: 'Path to output folder', default: 'output' - } + }, + { + name: 'frontmatter-fields', + type: 'list', + default: ['title', 'date', 'categories', 'tags', 'coverImage'] + }, + { + name: 'image-file-request-delay', + type: 'integer', + default: 500 + }, ]; diff --git a/src/settings.js b/src/settings.js index 58e67b3..df0247c 100644 --- a/src/settings.js +++ b/src/settings.js @@ -2,17 +2,17 @@ // Order is preserved. If a field has an empty value, it will not be included. You can rename a // field by providing an alias after a ':'. For example, 'date:created' will include 'date' in // frontmatter, but renamed to 'created'. -export const frontmatter_fields = [ - 'title', - 'date', - 'categories', - 'tags', - 'coverImage' -]; +// export const frontmatter_fields = [ +// 'title', +// 'date', +// 'categories', +// 'tags', +// 'coverImage' +// ]; // Time in ms to wait between requesting image files. Increase this if you see timeouts or // server errors. -export const image_file_request_delay = 500; +// export const image_file_request_delay = 500; // Time in ms to wait between saving Markdown files. Increase this if your file system becomes // overloaded. diff --git a/src/writer.js b/src/writer.js index 6a902eb..e54903e 100644 --- a/src/writer.js +++ b/src/writer.js @@ -120,7 +120,7 @@ async function writeImageFilesPromise(posts, config) { destinationPath, delay }; - delay += settings.image_file_request_delay; + delay += config.imageFileRequestDelay; return [payload]; } });