Skip to content

Commit

Permalink
add extra daily notes formats
Browse files Browse the repository at this point in the history
  • Loading branch information
laughedelic committed Nov 12, 2024
1 parent 3c2f8a3 commit 0e77745
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions outbreak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface ObsidianAppConfig {

interface MigrationConfig {
useNamespaces: boolean;
extraDailyNotesFormats: string[];
journalDateFormat: string;
ignoredPaths: string[];
dryRun: boolean;
Expand Down Expand Up @@ -56,17 +57,21 @@ async function readDailyNotesConfig(
// Function to check if a file matches the daily notes format
function isDailyNote(
filePath: string,
config: ObsidianDailyNotesConfig,
dailyNotesConfig: ObsidianDailyNotesConfig,
migrationConfig: MigrationConfig,
): boolean {
const format = config.format || "YYYY-MM-DD";
const folder = config.folder || ""; // Default to root if not specified
const format = dailyNotesConfig.format || "YYYY-MM-DD";
const folder = dailyNotesConfig.folder || ""; // Default to root if not specified

// Construct full format including the folder
const fullFormat = folder ? `[${folder}]/${format}` : format;
// Drop the extension
const notePath = filePath.slice(0, -extname(filePath).length);
// Try to parse it with moment.js (strict mode)
const parsedMoment = moment(notePath, fullFormat, true);
const parsedMoment = moment(notePath, [
fullFormat,
...migrationConfig.extraDailyNotesFormats,
], true);
return parsedMoment.isValid();
}

Expand Down Expand Up @@ -110,13 +115,16 @@ function planFileMigration(
): MigrationPlan {
const relativePath = relative(inputDir, inputPath);

if (isDailyNote(relativePath, dailyNotesConfig)) {
if (isDailyNote(relativePath, dailyNotesConfig, config)) {
const inputFilename = basename(relativePath, extname(relativePath));
const inputFormat = basename(dailyNotesConfig.format || "YYYY-MM-DD");
const outputFormat = config.journalDateFormat;

// Reformat the date string according to the configured format
const parsedDate = moment(inputFilename, inputFormat);
const parsedDate = moment(inputFilename, [
inputFormat,
...config.extraDailyNotesFormats.map((f) => basename(f)),
]);
const reformattedDate = parsedDate.isValid()
? parsedDate.format(outputFormat)
: inputFilename;
Expand Down Expand Up @@ -269,10 +277,15 @@ async function executeMigrationPlan(

const defaultConfig: MigrationConfig = {
useNamespaces: false,
extraDailyNotesFormats: [
"[journal]/YYYY/YYYY-MM-DD",
"[journal]/YYYY/MM/YYYY-MM-DD",
],
journalDateFormat: "YYYY-MM-DD",
ignoredPaths: [
"archive/**",
// "archive/**",
"**/*.txt",
"**/*.json",
".*/**", // any hidden directories in the root
"**/.*", // hidden files (anywhere)
],
Expand Down

0 comments on commit 0e77745

Please sign in to comment.