From 1877e43611b47697d65eef9d3d4ffe9a61527873 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Tue, 28 Jan 2025 08:14:15 -0500 Subject: [PATCH] buildSamplePostPath() --- src/questions.js | 1 - src/shared.js | 39 +++++++++++++++++++++++++++++++++++++++ src/wizard.js | 35 +++++++++++------------------------ 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/questions.js b/src/questions.js index 86da5b1..44f475d 100644 --- a/src/questions.js +++ b/src/questions.js @@ -95,7 +95,6 @@ export const all = [ value: 'none' } ], - isPathQuestion: true, prompt: inquirer.select } ]; diff --git a/src/shared.js b/src/shared.js index 0853bfe..6557462 100644 --- a/src/shared.js +++ b/src/shared.js @@ -1,3 +1,42 @@ +import * as luxon from 'luxon'; +import path from 'path'; +import * as settings from './settings.js'; + +export function buildPostPath(outputDir, type, date, slug, config) { + let dt; + if (settings.custom_date_formatting) { + dt = luxon.DateTime.fromFormat(date, settings.custom_date_formatting); + } else { + dt = luxon.DateTime.fromISO(date); + } + + // start with base output dir and post type + const pathSegments = [outputDir, 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 = 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); +} + export function getFilenameFromUrl(url) { let filename = url.split('/').slice(-1)[0]; try { diff --git a/src/wizard.js b/src/wizard.js index de22e55..e8ef5f1 100644 --- a/src/wizard.js +++ b/src/wizard.js @@ -1,8 +1,11 @@ import camelcase from 'camelcase'; import chalk from 'chalk'; import * as commander from 'commander'; +import * as luxon from 'luxon'; +import path from 'path'; import * as normalizers from './normalizers.js'; import * as questions from './questions.js'; +import * as shared from './shared.js'; // visual formatting for wizard const promptTheme = { @@ -84,7 +87,7 @@ export async function getWizardAnswers(questions, commandLineAnswers) { const answers = {}; for (const question of questions) { let answerKey = camelcase(question.name); - let normalizedAnswer; + let normalizedAnswer; // holds normalized answer value potentially returned during validation const promptConfig = { theme: promptTheme, @@ -103,7 +106,7 @@ export async function getWizardAnswers(questions, commandLineAnswers) { promptConfig.choices.forEach((choice) => { // show example path if this choice is selected config[answerKey] = choice.value; - choice.description = buildExamplePath(config); + choice.description = buildSamplePostPath(config); }); } } else { @@ -145,27 +148,11 @@ function normalize(value, type, onError) { } } -function buildExamplePath(config) { - const pathSegments = []; +export function buildSamplePostPath(config) { + const outputDir = path.sep; + const type = '' + const date = luxon.DateTime.now().toFormat('yyyy-LL-dd'); + const slug = 'my-post'; - if (config.dateFolders === 'year' || config.dateFolders === 'year-month') { - pathSegments.push('2025'); - } - - if (config.dateFolders === 'year-month') { - pathSegments.push('01'); - } - - let slugFragment = 'my-post'; - if (config.prefixDate) { - slugFragment = '2025-01-31-' + slugFragment; - } - - if (config.postFolders) { - pathSegments.push(slugFragment, 'index.md'); - } else { - pathSegments.push(slugFragment + '.md'); - } - - return '/' + pathSegments.join('/'); + return shared.buildPostPath(outputDir, type, date, slug, config); }