Skip to content

Commit

Permalink
fix: fixing journal title date formatting support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanyeung committed Feb 14, 2022
1 parent 3def810 commit 5da910a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 28 deletions.
26 changes: 26 additions & 0 deletions packages/common-all/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
StrictConfigV4,
} from "./types/intermediateConfigs";
import { isWebUri } from "./util/regex";
import { DateTime } from ".";

/**
* Dendron utilities
Expand Down Expand Up @@ -881,3 +882,28 @@ export class Wrap {
return setTimeout(callback, ms, ...args);
}
}

/**
* Gets the appropriately formatted title for a journal note, given the full
* note name and the configured date format.
* @param noteName note name like 'daily.journal.2021.01.01'
* @param dateFormat - should be gotten from Journal Config's 'dateFormat'
* @returns formatted title, or undefined if the journal title could not be parsed.
*/
export function getJournalTitle(
noteName: string,
dateFormat: string
): string | undefined {
let title = noteName.split(".");

while (title.length > 0) {
const attemptedParse = DateTime.fromFormat(title.join("."), dateFormat);
if (attemptedParse.isValid) {
return title.join("-");
}

title = title.length > 1 ? title.slice(1) : [];
}

return undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getJournalTitle } from "@dendronhq/common-all";

/**
* Tests the behavior around generating the note title for a journal note
*/
describe("GIVEN journal note title generation", () => {
describe("WHEN a journal note with the default format like journal.2021.01.01", () => {
test("THEN note title should be formatted properly", () => {
const result = getJournalTitle("journal.2021.01.01", "y.MM.dd");
expect(result).toEqual("2021-01-01");
});
});

describe("WHEN a journal note with a note name like double.prefix.2021.01.01", () => {
test("THEN note title should be formatted properly", () => {
const result = getJournalTitle("double.prefix.2021.01.01", "y.MM.dd");
expect(result).toEqual("2021-01-01");
});
});

describe("WHEN a journal note with a note name like journal.12.01 without the year", () => {
test("THEN note title should be formatted properly", () => {
const result = getJournalTitle("journal.12.01", "MM.dd");
expect(result).toEqual("12-01");
});
});

describe("WHEN a journal note with a configured date format of MM-dd like journal.12-01 with dashes", () => {
test("THEN note title should be formatted properly", () => {
const result = getJournalTitle("journal.12-01", "MM-dd");
expect(result).toEqual("12-01");
});
});

describe("WHEN a journal note does not match the configured date format ", () => {
test("THEN note title override should return undefined", () => {
const result = getJournalTitle("journal.12.01", "MM-dd");
expect(result).toBeFalsy();
});
});

describe("WHEN a journal note has a suffix like journal.2021.01.01.suffix", () => {
test("THEN note title override should return undefined", () => {
const result = getJournalTitle("journal.2021.01.01.suffix", "y.MM.dd");
expect(result).toBeFalsy();
});
});
});
41 changes: 16 additions & 25 deletions packages/plugin-core/src/commands/NoteLookupCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
VaultUtils,
VSCodeEvents,
SchemaTemplate,
getJournalTitle,
} from "@dendronhq/common-all";
import { getDurationMilliseconds } from "@dendronhq/common-server";
import { HistoryService, MetadataService } from "@dendronhq/engine-server";
Expand Down Expand Up @@ -389,11 +390,25 @@ export class NoteLookupCommand
try {
const { quickpick, selectedItems } = opts;
const selected = this.getSelected({ quickpick, selectedItems });

const extension = ExtensionProvider.getExtension();
const ws = extension.getDWorkspace();

const journalDateFormat = ConfigUtils.getJournal(ws.config).dateFormat;

const out = await Promise.all(
selected.map((item) => {
// If we're in journal mode, then apply title and trait overrides
if (this.isJournalButtonPressed()) {
const journalModifiedTitle = this.journalTitleOverride(item.fname);
/**
* this is a hacky title override for journal notes.
* TODO: remove this once we implement a more general way to override note titles.
* this is a hacky title override for journal notes.
*/
const journalModifiedTitle = getJournalTitle(
item.fname,
journalDateFormat
);

if (journalModifiedTitle) {
item.title = journalModifiedTitle;
Expand Down Expand Up @@ -663,30 +678,6 @@ export class NoteLookupCommand
return vault;
}

/**
* this is a hacky title override for journal notes.
* TODO: remove this once we implement a more general way to override note titles.
* this is a hacky title override for journal notes.
* This only works when the journal note modifier was explicitly pressed
* and when the date portion is the last bit of the hierarchy.
* e.g.) if the picker value is journal.2021.08.13.some-stuff, we don't override (title is some-stuff)
*/

journalTitleOverride(input: string): string | undefined {
if (/.*\d{4}\.\d{2}\.\d{2}$/g.test(input)) {
const [...maybeDatePortion] = input.split(".").slice(-3);
// we only override y.MM.dd
if (maybeDatePortion.length === 3) {
const maybeTitleOverride = maybeDatePortion.join("-");
if (maybeTitleOverride.match(/\d\d\d\d-\d\d-\d\d$/)) {
return maybeTitleOverride;
}
}
}

return;
}

private isJournalButtonPressed() {
const journalBtn = _.find(this.controller.state.buttons, (btn) => {
return btn.type === LookupNoteTypeEnum.journal;
Expand Down
11 changes: 10 additions & 1 deletion packages/plugin-core/src/test/MockDendronExtension.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
CONSTANTS,
DendronTreeViewKey,
DEngineClient,
DVault,
DWorkspaceV2,
WorkspaceSettings,
WorkspaceType,
} from "@dendronhq/common-all";
import { readJSONWithCommentsSync } from "@dendronhq/common-server";
import { IWorkspaceService, WorkspaceService } from "@dendronhq/engine-server";
import path from "path";
import {
Disposable,
ExtensionContext,
Expand Down Expand Up @@ -176,7 +179,13 @@ export class MockDendronExtension implements IDendronExtension {
}

getWorkspaceConfig(): WorkspaceConfiguration {
throw new Error("Method not implemented in MockDendronExtension.");
if (!this._wsRoot) {
throw new Error("wsRoot not configured in MockDendronExtension.");
}
const wsConfig = readJSONWithCommentsSync(
path.join(this._wsRoot, CONSTANTS.DENDRON_WS_NAME)
);
return wsConfig;
}

getTreeView(_key: DendronTreeViewKey): WebviewViewProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ suite("Scratch Notes", function () {
// The note title should be in the format yyyy-MM-dd
expect(/\d{4}-\d{2}-\d{2}$/g.test(newNote.title)).toBeTruthy();

// @ts-ignore
const traits = newNote.traitIds;
// TODO: traits isn't exposed in newNote props here because in the test
//we extract noteProps via `getNoteFromTextEditor` instead of the
//engine. So for now, test via the raw traitIds that should have been
//added to the note.
const traits = (newNote as any).traitIds;

expect(
traits.length === 1 && traits[0] === "journalNote"
).toBeTruthy();
Expand Down

0 comments on commit 5da910a

Please sign in to comment.