From 896eaadb4fc55607a47180b4924314ed86627c40 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Tue, 28 Jan 2025 16:54:27 -0500 Subject: [PATCH] Have writer use shared.buildPostPath() --- src/intake.js | 1 + src/writer.js | 43 ++++++++----------------------------------- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/src/intake.js b/src/intake.js index e8ef5f1..16c15c9 100644 --- a/src/intake.js +++ b/src/intake.js @@ -75,6 +75,7 @@ function getCommandLineAnswers(questions) { // normalize and validate default answer const question = questions.find((question) => camelcase(question.name) === key); answers[key] = normalize(value, question.type, (errorMessage) => { + // this is formatted to match how commander displays other errors commander.program.error(`error: option '--${question.name} <${question.type}>' argument '${value}' is invalid. ${errorMessage}`); }); } diff --git a/src/writer.js b/src/writer.js index 7a642c9..52af1de 100644 --- a/src/writer.js +++ b/src/writer.js @@ -3,7 +3,6 @@ import chalk from 'chalk'; import fs from 'fs'; import http from 'http'; import https from 'https'; -import * as luxon from 'luxon'; import path from 'path'; import * as settings from './settings.js'; import * as shared from './shared.js'; @@ -47,7 +46,7 @@ async function writeMarkdownFilesPromise(posts, config) { let skipCount = 0; let delay = 0; const payloads = posts.flatMap(post => { - const destinationPath = getPostPath(post, config); + const destinationPath = buildPostPath(post, config); if (checkFile(destinationPath)) { // already exists, don't need to save again skipCount++; @@ -105,7 +104,7 @@ async function writeImageFilesPromise(posts, config) { let skipCount = 0; let delay = 0; const payloads = posts.flatMap(post => { - const postPath = getPostPath(post, config); + const postPath = buildPostPath(post, config); const imagesDir = path.join(path.dirname(postPath), 'images'); return post.meta.imageUrls.flatMap(imageUrl => { const filename = shared.getFilenameFromUrl(imageUrl); @@ -171,39 +170,13 @@ async function loadImageFilePromise(imageUrl) { return buffer; } -function getPostPath(post, config) { - let dt; - if (settings.custom_date_formatting) { - dt = luxon.DateTime.fromFormat(post.frontmatter.date, settings.custom_date_formatting); - } else { - dt = luxon.DateTime.fromISO(post.frontmatter.date); - } +function buildPostPath(post, config) { + const outputDir = settings.output_directory; + const type = post.meta.type; + const date = post.frontmatter.date; + const slug = post.meta.slug; - // start with base output dir and post type - const pathSegments = [settings.output_directory, post.meta.type]; - - if (config.dateFolders === 'year' || config.dateFolders === 'year-month') { - pathSegments.push(dt.toFormat('yyyy')); - } - - if (config.dateFolders === 'year-month') { - pathSegments.push(dt.toFormat('LL')); - } - - // create slug fragment, possibly date prefixed - let slugFragment = post.meta.slug; - if (config.prefixDate) { - slugFragment = dt.toFormat('yyyy-LL-dd') + '-' + slugFragment; - } - - // use slug fragment as folder or filename as specified - if (config.postFolders) { - pathSegments.push(slugFragment, 'index.md'); - } else { - pathSegments.push(slugFragment + '.md'); - } - - return path.join(...pathSegments); + return shared.buildPostPath(outputDir, type, date, slug, config); } function checkFile(path) {