Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
19 changes: 19 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getParseCommand,
getCurrentDateCommand,
getCurrentTimeCommand,
getCurrentWeekCommand,
getNowCommand,
} from "./commands";
import { getFormattedDate, getOrCreateDailyNote, parseTruthy } from "./utils";
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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<void> {
const { workspace } = this.app;

Expand Down
8 changes: 4 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand Down
32 changes: 32 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = [
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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();
})
);
}
}
26 changes: 18 additions & 8 deletions src/suggest/date-suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ export default class DateSuggest extends EditorSuggest<IDateCompletion> {
}

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];
Expand All @@ -70,22 +75,24 @@ export default class DateSuggest extends EditorSuggest<IDateCompletion> {
}

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)
);
}
Expand All @@ -105,6 +112,9 @@ export default class DateSuggest extends EditorSuggest<IDateCompletion> {
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;
}
Expand All @@ -123,7 +133,7 @@ export default class DateSuggest extends EditorSuggest<IDateCompletion> {
onTrigger(
cursor: EditorPosition,
editor: Editor,
file: TFile
_: TFile
): EditorSuggestTriggerInfo {
if (!this.plugin.settings.isAutosuggestEnabled) {
return null;
Expand Down