buildSamplePostPath()

This commit is contained in:
Will Boyd
2025-01-28 08:14:15 -05:00
parent 8d4e7a03fc
commit 1877e43611
3 changed files with 50 additions and 25 deletions
-1
View File
@@ -95,7 +95,6 @@ export const all = [
value: 'none'
}
],
isPathQuestion: true,
prompt: inquirer.select
}
];
+39
View File
@@ -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 {
+11 -24
View File
@@ -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);
}