From 965041cc540674e92e9c363024161060804a9339 Mon Sep 17 00:00:00 2001 From: Gilles Gagniard Date: Thu, 11 Jun 2026 16:20:13 +0200 Subject: [PATCH] Add week parsing and suggestion features - Implemented week parsing in getParseCommand and added getCurrentWeekCommand. - Enhanced NLDParser to recognize "week" in date strings. - Updated settings to include weekFormat and additionalSuggestions. - Modified DateSuggest to provide suggestions for weeks. --- src/commands.ts | 10 ++++++++++ src/main.ts | 19 +++++++++++++++++++ src/parser.ts | 8 ++++---- src/settings.ts | 32 ++++++++++++++++++++++++++++++++ src/suggest/date-suggest.ts | 26 ++++++++++++++++++-------- 5 files changed, 83 insertions(+), 12 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index a5f1b9b..b6cfc10 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -37,6 +37,10 @@ export function getParseCommand(plugin: NaturalLanguageDates, mode: string): voi const time = plugin.parseTime(selectedText); newStr = `${time.formattedString}`; + } else if (mode == "week") { + const week = plugin.parseWeek(selectedText); + + newStr = `${week.formattedString}`; } editor.replaceSelection(newStr); @@ -76,3 +80,9 @@ export function getCurrentTimeCommand(plugin: NaturalLanguageDates): void { const date = new Date(); insertMomentCommand(plugin, date, format); } + +export function getCurrentWeekCommand(plugin: NaturalLanguageDates): void { + const format = plugin.settings.weekFormat; + const date = new Date(); + insertMomentCommand(plugin, date, format); +} diff --git a/src/main.ts b/src/main.ts index b7d017b..073cf52 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,6 +8,7 @@ import { getParseCommand, getCurrentDateCommand, getCurrentTimeCommand, + getCurrentWeekCommand, getNowCommand, } from "./commands"; import { getFormattedDate, getOrCreateDailyNote, parseTruthy } from "./utils"; @@ -47,6 +48,13 @@ export default class NaturalLanguageDates extends Plugin { hotkeys: [], }); + this.addCommand({ + id: "nlp-parse-week", + name: "Parse natural language week", + callback: () => getParseCommand(this, "week"), + hotkeys: [], + }); + this.addCommand({ id: "nlp-now", name: "Insert the current date and time", @@ -68,6 +76,13 @@ export default class NaturalLanguageDates extends Plugin { hotkeys: [], }); + this.addCommand({ + id: "nlp-week", + name: "Insert the current week", + callback: () => getCurrentWeekCommand(this), + hotkeys: [], + }); + this.addCommand({ id: "nlp-picker", name: "Date picker", @@ -133,6 +148,10 @@ export default class NaturalLanguageDates extends Plugin { return this.parse(dateString, this.settings.timeFormat); } + parseWeek(dateString: string): NLDResult { + return this.parse(dateString, this.settings.weekFormat); + } + async actionHandler(params: ObsidianProtocolData): Promise { const { workspace } = this.app; diff --git a/src/parser.ts b/src/parser.ts index 2ffe700..2d0395d 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -83,17 +83,17 @@ export default class NLDParser { ? window.moment().weekday(0).toDate() : new Date(); - if (thisDateMatch && thisDateMatch[1] === "week") { + if (thisDateMatch && thisDateMatch[1].toLowerCase() === "week") { return parser.parseDate(`this ${weekStart}`, referenceDate); } - if (nextDateMatch && nextDateMatch[1] === "week") { + if (nextDateMatch && nextDateMatch[1].toLowerCase() === "week") { return parser.parseDate(`next ${weekStart}`, referenceDate, { forwardDate: true, }); } - if (nextDateMatch && nextDateMatch[1] === "month") { + if (nextDateMatch && nextDateMatch[1].toLowerCase() === "month") { const thisMonth = parser.parseDate("this month", new Date(), { forwardDate: true, }); @@ -102,7 +102,7 @@ export default class NLDParser { }); } - if (nextDateMatch && nextDateMatch[1] === "year") { + if (nextDateMatch && nextDateMatch[1].toLowerCase() === "year") { const thisYear = parser.parseDate("this year", new Date(), { forwardDate: true, }); diff --git a/src/settings.ts b/src/settings.ts index a7696aa..4844ced 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -19,12 +19,15 @@ export interface NLDSettings { format: string; timeFormat: string; + weekFormat: string; separator: string; weekStart: DayOfWeek; modalToggleTime: boolean; modalToggleLink: boolean; modalMomentFormat: string; + + additionalSuggestions: string[]; } export const DEFAULT_SETTINGS: NLDSettings = { @@ -34,12 +37,15 @@ export const DEFAULT_SETTINGS: NLDSettings = { format: "YYYY-MM-DD", timeFormat: "HH:mm", + weekFormat: "GGGG-[W]WW", separator: " ", weekStart: "locale-default", modalToggleTime: false, modalToggleLink: false, modalMomentFormat: "YYYY-MM-DD HH:mm", + + additionalSuggestions: [], }; const weekdays = [ @@ -87,6 +93,19 @@ export class NLDSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); }) ); + + new Setting(containerEl) + .setName("Week format") + .setDesc("Output format for parsed dates as weeks") + .addMomentFormat((text) => + text + .setDefaultFormat("GGGG-[W]WW") + .setValue(this.plugin.settings.weekFormat) + .onChange(async (value) => { + this.plugin.settings.weekFormat = value || "GGGG-[W]WW"; + await this.plugin.saveSettings(); + }) + ); new Setting(containerEl) .setName("Week starts on") @@ -177,5 +196,18 @@ export class NLDSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); }) ); + + new Setting(containerEl) + .setName("Additional Suggestions") + .setDesc("Additional default date autosuggests (comma separated list)") + .addText((text) => + text + .setPlaceholder("") + .setValue(this.plugin.settings.additionalSuggestions.join(',')) + .onChange(async (value) => { + this.plugin.settings.additionalSuggestions = value.split(',').filter(s => s); + await this.plugin.saveSettings(); + }) + ); } } diff --git a/src/suggest/date-suggest.ts b/src/suggest/date-suggest.ts index 615108c..75630a9 100644 --- a/src/suggest/date-suggest.ts +++ b/src/suggest/date-suggest.ts @@ -46,10 +46,15 @@ export default class DateSuggest extends EditorSuggest { } getDateSuggestions(context: EditorSuggestContext): IDateCompletion[] { - if (context.query.match(/^time/)) { + if (context.query.match(/tim|min|hou|now/i)) { return ["now", "+15 minutes", "+1 hour", "-15 minutes", "-1 hour"] .map((val) => ({ label: `time:${val}` })) - .filter((item) => item.label.toLowerCase().startsWith(context.query)); + .filter((item) => item.label.toLowerCase().contains(context.query)); + } + if (context.query.match(/wee/i)) { + return ["this", "next", "last"] + .map((val) => ({ label: `week:${val} week` })) + .filter((item) => item.label.toLowerCase().contains(context.query)); } if (context.query.match(/(next|last|this)/i)) { const reference = context.query.match(/(next|last|this)/i)[1]; @@ -70,22 +75,24 @@ export default class DateSuggest extends EditorSuggest { } const relativeDate = - context.query.match(/^in ([+-]?\d+)/i) || context.query.match(/^([+-]?\d+)/i); + context.query.match(/in ([+-]?\d+)/i) || context.query.match(/([+-]?\d+)/i); if (relativeDate) { const timeDelta = relativeDate[1]; return [ - { label: `in ${timeDelta} minutes` }, - { label: `in ${timeDelta} hours` }, + { label: `time:in ${timeDelta} minutes` }, + { label: `time:in ${timeDelta} hours` }, { label: `in ${timeDelta} days` }, { label: `in ${timeDelta} weeks` }, { label: `in ${timeDelta} months` }, { label: `${timeDelta} days ago` }, { label: `${timeDelta} weeks ago` }, { label: `${timeDelta} months ago` }, - ].filter((items) => items.label.toLowerCase().startsWith(context.query)); + ].filter((items) => items.label.toLowerCase().contains(context.query)); } - return [{ label: "Today" }, { label: "Yesterday" }, { label: "Tomorrow" }].filter( + const defaultSuggestions = [{ label: "Today" }, { label: "Yesterday" }, { label: "Tomorrow" }]; + const additionalSuggestions = this.plugin.settings.additionalSuggestions.map(x => {return {label: x}}); + return additionalSuggestions.concat(defaultSuggestions).filter( (items) => items.label.toLowerCase().startsWith(context.query) ); } @@ -105,6 +112,9 @@ export default class DateSuggest extends EditorSuggest { const timePart = suggestion.label.substring(5); dateStr = this.plugin.parseTime(timePart).formattedString; makeIntoLink = false; + } else if (suggestion.label.startsWith("week:")) { + const weekPart = suggestion.label.substring(5); + dateStr = this.plugin.parseWeek(weekPart).formattedString; } else { dateStr = this.plugin.parseDate(suggestion.label).formattedString; } @@ -123,7 +133,7 @@ export default class DateSuggest extends EditorSuggest { onTrigger( cursor: EditorPosition, editor: Editor, - file: TFile + _: TFile ): EditorSuggestTriggerInfo { if (!this.plugin.settings.isAutosuggestEnabled) { return null;