First list and integer questions, spaces to tabs

This commit is contained in:
Will Boyd
2025-02-01 08:27:58 -05:00
parent cf338813f1
commit f6197d1d70
6 changed files with 84 additions and 55 deletions
+15 -15
View File
@@ -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
+38 -21
View File
@@ -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.');
}
+3 -3
View File
@@ -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())];
+19 -7
View File
@@ -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
},
];
+8 -8
View File
@@ -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.
+1 -1
View File
@@ -120,7 +120,7 @@ async function writeImageFilesPromise(posts, config) {
destinationPath,
delay
};
delay += settings.image_file_request_delay;
delay += config.imageFileRequestDelay;
return [payload];
}
});