mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
core: add nowz and timestampz formats (#7270)
Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
This commit is contained in:
@@ -29,6 +29,7 @@ Notesnook supports the following (Markdown) shortcuts in the editor:
|
|||||||
| Current Date | `/date` |
|
| Current Date | `/date` |
|
||||||
| Date Time | `/time` |
|
| Date Time | `/time` |
|
||||||
| Current Date & Time | `/now` |
|
| Current Date & Time | `/now` |
|
||||||
|
| Current Date & Time with timezone | `/nowz` |
|
||||||
|
|
||||||
## FAQs
|
## FAQs
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ Go to `Settings` > `Editor` > `Title format` to customize the title formatting.
|
|||||||
|
|
||||||
**$timestamp$**: Full date & time without any spaces or symbols (e.g. 202305261253)
|
**$timestamp$**: Full date & time without any spaces or symbols (e.g. 202305261253)
|
||||||
|
|
||||||
|
**$timestampz$**: UTC offset added to _timestamp_
|
||||||
|
|
||||||
You can use a combination of these templates in the note title. For example `$headline$ - $date$` will become `Your note headline - 06-22-2023`.
|
You can use a combination of these templates in the note title. For example `$headline$ - $date$` will become `Your note headline - 06-22-2023`.
|
||||||
|
|
||||||
## Paragraph spacing
|
## Paragraph spacing
|
||||||
|
|||||||
@@ -18,8 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
import advancedFormat from "dayjs/plugin/advancedFormat";
|
||||||
|
import timezone from "dayjs/plugin/timezone";
|
||||||
import { TimeFormat } from "../types.js";
|
import { TimeFormat } from "../types.js";
|
||||||
|
|
||||||
|
dayjs.extend(advancedFormat);
|
||||||
|
dayjs.extend(timezone);
|
||||||
|
|
||||||
export function getWeekGroupFromTimestamp(timestamp: number) {
|
export function getWeekGroupFromTimestamp(timestamp: number) {
|
||||||
const date = new Date(timestamp);
|
const date = new Date(timestamp);
|
||||||
const { start, end } = getWeek(date);
|
const { start, end } = getWeek(date);
|
||||||
@@ -63,6 +68,9 @@ export function getTimeFormat(format: TimeFormat) {
|
|||||||
return format === "12-hour" ? "hh:mm A" : "HH:mm";
|
return format === "12-hour" ? "hh:mm A" : "HH:mm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TimeZoneOptions = {
|
||||||
|
type: "timezone";
|
||||||
|
};
|
||||||
export type TimeOptions = {
|
export type TimeOptions = {
|
||||||
type: "time";
|
type: "time";
|
||||||
timeFormat: TimeFormat;
|
timeFormat: TimeFormat;
|
||||||
@@ -76,7 +84,17 @@ export type DateTimeOptions = {
|
|||||||
dateFormat: string;
|
dateFormat: string;
|
||||||
timeFormat: TimeFormat;
|
timeFormat: TimeFormat;
|
||||||
};
|
};
|
||||||
export type FormatDateOptions = TimeOptions | DateOptions | DateTimeOptions;
|
export type DateTimeWithTimeZoneOptions = {
|
||||||
|
type: "date-time-timezone";
|
||||||
|
dateFormat: string;
|
||||||
|
timeFormat: TimeFormat;
|
||||||
|
};
|
||||||
|
export type FormatDateOptions =
|
||||||
|
| TimeZoneOptions
|
||||||
|
| TimeOptions
|
||||||
|
| DateOptions
|
||||||
|
| DateTimeOptions
|
||||||
|
| DateTimeWithTimeZoneOptions;
|
||||||
|
|
||||||
export function formatDate(
|
export function formatDate(
|
||||||
date: string | number | Date | null | undefined,
|
date: string | number | Date | null | undefined,
|
||||||
@@ -87,6 +105,10 @@ export function formatDate(
|
|||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
switch (options.type) {
|
switch (options.type) {
|
||||||
|
case "date-time-timezone":
|
||||||
|
return dayjs(date).format(
|
||||||
|
`${options.dateFormat} ${getTimeFormat(options.timeFormat)} z`
|
||||||
|
);
|
||||||
case "date-time":
|
case "date-time":
|
||||||
return dayjs(date).format(
|
return dayjs(date).format(
|
||||||
`${options.dateFormat} ${getTimeFormat(options.timeFormat)}`
|
`${options.dateFormat} ${getTimeFormat(options.timeFormat)}`
|
||||||
@@ -95,6 +117,8 @@ export function formatDate(
|
|||||||
return dayjs(date).format(getTimeFormat(options.timeFormat));
|
return dayjs(date).format(getTimeFormat(options.timeFormat));
|
||||||
case "date":
|
case "date":
|
||||||
return dayjs(date).format(options.dateFormat);
|
return dayjs(date).format(options.dateFormat);
|
||||||
|
case "timezone":
|
||||||
|
return dayjs(date).format("ZZ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ const COUNT_REGEX = /\$count\$/g;
|
|||||||
const TIME_REGEX = /\$time\$/g;
|
const TIME_REGEX = /\$time\$/g;
|
||||||
const HEADLINE_REGEX = /\$headline\$/g;
|
const HEADLINE_REGEX = /\$headline\$/g;
|
||||||
const TIMESTAMP_REGEX = /\$timestamp\$/g;
|
const TIMESTAMP_REGEX = /\$timestamp\$/g;
|
||||||
|
const TIMESTAMP_Z_REGEX = /\$timestampz\$/g;
|
||||||
const DATE_TIME_STRIP_REGEX = /[\\\-:./, ]/g;
|
const DATE_TIME_STRIP_REGEX = /[\\\-:./, ]/g;
|
||||||
|
|
||||||
export function formatTitle(
|
export function formatTitle(
|
||||||
@@ -45,7 +46,11 @@ export function formatTitle(
|
|||||||
timeFormat,
|
timeFormat,
|
||||||
type: "time"
|
type: "time"
|
||||||
});
|
});
|
||||||
|
const timezone = formatDate(Date.now(), {
|
||||||
|
type: "timezone"
|
||||||
|
});
|
||||||
const timestamp = `${date}${time}`.replace(DATE_TIME_STRIP_REGEX, "");
|
const timestamp = `${date}${time}`.replace(DATE_TIME_STRIP_REGEX, "");
|
||||||
|
const timestampWithTimeZone = `${timestamp}${timezone}`;
|
||||||
|
|
||||||
return titleFormat
|
return titleFormat
|
||||||
.replace(NEWLINE_STRIP_REGEX, " ")
|
.replace(NEWLINE_STRIP_REGEX, " ")
|
||||||
@@ -53,5 +58,6 @@ export function formatTitle(
|
|||||||
.replace(TIME_REGEX, time)
|
.replace(TIME_REGEX, time)
|
||||||
.replace(HEADLINE_REGEX, headline || "")
|
.replace(HEADLINE_REGEX, headline || "")
|
||||||
.replace(TIMESTAMP_REGEX, timestamp)
|
.replace(TIMESTAMP_REGEX, timestamp)
|
||||||
|
.replace(TIMESTAMP_Z_REGEX, timestampWithTimeZone)
|
||||||
.replace(COUNT_REGEX, `${totalNotes + 1}`);
|
.replace(COUNT_REGEX, `${totalNotes + 1}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,11 @@ declare module "@tiptap/core" {
|
|||||||
* Insert date & time at current position
|
* Insert date & time at current position
|
||||||
*/
|
*/
|
||||||
insertDateTime: () => ReturnType;
|
insertDateTime: () => ReturnType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert date & time with time zone at current position
|
||||||
|
*/
|
||||||
|
insertDateTimeWithTimeZone: () => ReturnType;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,7 +69,8 @@ export const DateTime = Extension.create<DateTimeOptions>({
|
|||||||
return {
|
return {
|
||||||
"Alt-t": ({ editor }) => editor.commands.insertTime(),
|
"Alt-t": ({ editor }) => editor.commands.insertTime(),
|
||||||
"Alt-d": ({ editor }) => editor.commands.insertDate(),
|
"Alt-d": ({ editor }) => editor.commands.insertDate(),
|
||||||
"Mod-Alt-d": ({ editor }) => editor.commands.insertDateTime()
|
"Mod-Alt-d": ({ editor }) => editor.commands.insertDateTime(),
|
||||||
|
"Mod-Alt-z": ({ editor }) => editor.commands.insertDateTimeWithTimeZone()
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -97,6 +103,16 @@ export const DateTime = Extension.create<DateTimeOptions>({
|
|||||||
timeFormat: this.options.timeFormat,
|
timeFormat: this.options.timeFormat,
|
||||||
type: "date-time"
|
type: "date-time"
|
||||||
})
|
})
|
||||||
|
),
|
||||||
|
insertDateTimeWithTimeZone:
|
||||||
|
() =>
|
||||||
|
({ commands }) =>
|
||||||
|
commands.insertContent(
|
||||||
|
formatDate(Date.now(), {
|
||||||
|
dateFormat: this.options.dateFormat,
|
||||||
|
timeFormat: this.options.timeFormat,
|
||||||
|
type: "date-time-timezone"
|
||||||
|
})
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -130,6 +146,16 @@ export const DateTime = Extension.create<DateTimeOptions>({
|
|||||||
type: "date-time"
|
type: "date-time"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
shortcutInputRule({
|
||||||
|
shortcut: "/nowz",
|
||||||
|
replace: () => {
|
||||||
|
return formatDate(Date.now(), {
|
||||||
|
dateFormat: this.options.dateFormat,
|
||||||
|
timeFormat: this.options.timeFormat,
|
||||||
|
type: "date-time-timezone"
|
||||||
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -203,5 +229,14 @@ export function replaceDateTime(
|
|||||||
}) + " "
|
}) + " "
|
||||||
);
|
);
|
||||||
|
|
||||||
|
value = value.replaceAll(
|
||||||
|
"/nowz ",
|
||||||
|
formatDate(Date.now(), {
|
||||||
|
dateFormat,
|
||||||
|
timeFormat,
|
||||||
|
type: "date-time-timezone"
|
||||||
|
}) + " "
|
||||||
|
);
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user