diff --git a/format.ts b/format.ts new file mode 100644 index 0000000..0104094 --- /dev/null +++ b/format.ts @@ -0,0 +1,51 @@ +import { moment } from "obsidian"; +import { adjectives } from "adjectives.js"; +import { capitalize, sample } from "lodash-es"; +import { nouns } from "nouns.js"; + +interface FormatOptions { + ext?: string; +} + +export function format(str: string, options?: FormatOptions): string { + let formatted = str.trim(); + + let extension = options?.ext ?? ""; + if (extension.length > 0 && !extension.startsWith(".")) { + extension = `.${extension}`; + } + + // Replace {N} with a random noun + formatted = formatted.replace(/{N}/g, () => { + return capitalize(sample(nouns)); + }); + + // Replace {n} with a random noun (lower case) + formatted = formatted.replace(/{n}/g, () => { + return sample(nouns); + }); + + // Replace {A} with a random adjective + formatted = formatted.replace(/{A}/g, () => { + return capitalize(sample(adjectives)); + }); + + // Replace {a} with a random adjective (lower case) + formatted = formatted.replace(/{a}/g, () => { + return sample(adjectives); + }); + + // Replace {MW} with the current month week + const monthWeek = `${Math.ceil(new Date().getDate() / 7)}`; + formatted = formatted.replace(/{MW}/g, monthWeek); + + // Replace {time } with the moment-formatted string + formatted = formatted.replace( + /{time (.+?)}/g, + (_: string, pattern: string) => { + return moment().format(pattern); + } + ); + + return formatted + extension; +} diff --git a/main.ts b/main.ts index 25bfd96..61da81c 100644 --- a/main.ts +++ b/main.ts @@ -3,13 +3,10 @@ import { Plugin, PluginSettingTab, Setting, - moment, Vault, Notice, } from "obsidian"; -import { nouns } from "./nouns.js"; -import { adjectives } from "./adjectives.js"; -import { capitalize, sample } from "lodash-es"; +import { format } from "./format.js"; interface ScrapsPluginSettings { scrapsRootDir: string; @@ -19,16 +16,10 @@ interface ScrapsPluginSettings { const DEFAULT_SETTINGS: ScrapsPluginSettings = { scrapsRootDir: "_Scraps", - scrapsPathFormat: "MM MMM/R", - scrapsFileName: "ddd MMM DD hhmmssa", + scrapsPathFormat: "{time YYYY} Week {time WW MMM}", + scrapsFileName: "{time DDDD} {A} {N}", }; -function getFormattedDate(format: string): string { - const momentFormatted = moment().format(format); - const currentWeek = Math.ceil(moment().date() / 7); - return momentFormatted.replace("R", `Week ${currentWeek}`); -} - async function mkdirp(vault: Vault, folderPath: string): Promise { const pathParts = folderPath.split("/"); @@ -63,15 +54,13 @@ export default class ScrapsPlugin extends Plugin { } getScrapDir(): string { - return `${this.settings.scrapsRootDir}/${getFormattedDate( + return `${this.settings.scrapsRootDir}/${format( this.settings.scrapsPathFormat )}`; } getScrapFileName(): string { - const adj = capitalize(sample(adjectives)); - const noun = capitalize(sample(nouns)); - return `Scrap ${adj} ${noun}.md`; + return format(this.settings.scrapsFileName, { ext: "md" }); } getScrapFilePath(): string { @@ -197,30 +186,44 @@ class ScrapsSettingTab extends PluginSettingTab { }) ); - new Setting(containerEl) + const pathFormatSetting = new Setting(containerEl) .setName("Scraps Path Format") - .setDesc("The format of the path to the scrap") + .setDesc( + `Preview: ${format(this.plugin.settings.scrapsPathFormat)}` + ) .addText((text) => text .setPlaceholder("Enter format") .setValue(this.plugin.settings.scrapsPathFormat) .onChange(async (value) => { this.plugin.settings.scrapsPathFormat = value; + pathFormatSetting.setDesc(`Preview: ${format(value)}`); await this.plugin.saveSettings(); }) ); - new Setting(containerEl) + const fileFormatSetting = new Setting(containerEl) .setName("Scraps File Name Format") - .setDesc("The format of the filename") + .setDesc( + `Preview: ${format(this.plugin.settings.scrapsFileName, { + ext: "md", + })}` + ) .addText((text) => text .setPlaceholder("Enter format") .setValue(this.plugin.settings.scrapsFileName) .onChange(async (value) => { this.plugin.settings.scrapsFileName = value; + fileFormatSetting.setDesc( + `Preview: ${format(value, { ext: "md" })}` + ); await this.plugin.saveSettings(); }) ); + + new Setting(containerEl) + .setName("Formatting Help") + .setDesc("Tokens: {time }, {A}, {N}, {MW}"); } }