2024-03-20 08:28:45 +08:00
|
|
|
moment.tz.link(["Asia/Kolkata|IST", "Asia/Seoul|KST", "Asia/Tokyo|JST"]);
|
2021-08-04 14:27:22 +08:00
|
|
|
const timezoneNames = moment.tz.names();
|
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
function addLocalDate(attributes, state, buffer, applyDataAttributes) {
|
|
|
|
if (attributes.timezone) {
|
|
|
|
if (!timezoneNames.includes(attributes.timezone)) {
|
|
|
|
delete attributes.timezone;
|
|
|
|
}
|
2018-11-26 21:20:32 +08:00
|
|
|
}
|
2018-08-27 17:19:30 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
if (attributes.displayedTimezone) {
|
|
|
|
if (!timezoneNames.includes(attributes.displayedTimezone)) {
|
|
|
|
delete attributes.displayedTimezone;
|
|
|
|
}
|
2018-10-12 05:41:57 +08:00
|
|
|
}
|
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
if (attributes.timezones) {
|
|
|
|
attributes.timezones = attributes.timezones
|
|
|
|
.split("|")
|
|
|
|
.filter((tz) => timezoneNames.includes(tz))
|
|
|
|
.join("|");
|
2018-11-26 21:20:32 +08:00
|
|
|
}
|
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
const dateTime = moment.tz(
|
|
|
|
[attributes._default || attributes.date, attributes.time]
|
|
|
|
.filter(Boolean)
|
|
|
|
.join("T"),
|
|
|
|
attributes.timezone || "Etc/UTC"
|
|
|
|
);
|
2018-11-23 00:19:24 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
const emailFormat =
|
|
|
|
state.md.options.discourse.datesEmailFormat || moment.defaultFormat;
|
2018-09-12 21:54:36 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
attributes.emailPreview = `${dateTime.utc().format(emailFormat)} UTC`;
|
2018-11-23 00:19:24 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
let token = new state.Token("span_open", "span", 1);
|
|
|
|
token.attrs = [["class", "discourse-local-date"]];
|
|
|
|
applyDataAttributes(token, attributes, "date");
|
2018-05-04 14:51:41 +08:00
|
|
|
buffer.push(token);
|
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = dateTime.utc().format(attributes.format);
|
|
|
|
buffer.push(token);
|
2018-11-26 21:20:32 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
token = new state.Token("span_close", "span", -1);
|
|
|
|
buffer.push(token);
|
2022-01-10 15:02:36 +08:00
|
|
|
}
|
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
function date(buffer, matches, state, { parseBBCodeTag, applyDataAttributes }) {
|
|
|
|
const parsed = parseBBCodeTag(matches[0], 0, matches[0].length);
|
2022-01-10 15:02:36 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
if (parsed?.tag === "date") {
|
|
|
|
addLocalDate(parsed.attrs, state, buffer, applyDataAttributes);
|
|
|
|
} else {
|
|
|
|
let token = new state.Token("text", "", 0);
|
|
|
|
token.content = matches[0];
|
2024-04-07 04:35:24 +08:00
|
|
|
buffer.push(token);
|
2022-01-10 15:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
function range(
|
|
|
|
buffer,
|
|
|
|
matches,
|
|
|
|
state,
|
|
|
|
{ parseBBCodeTag, applyDataAttributes }
|
|
|
|
) {
|
2018-11-26 21:20:32 +08:00
|
|
|
let token;
|
2024-05-24 16:47:45 +08:00
|
|
|
const parsed = parseBBCodeTag(matches[0], 0, matches[0].length);
|
|
|
|
|
|
|
|
if (parsed?.tag === "date-range") {
|
|
|
|
if (parsed.attrs.from) {
|
|
|
|
const { from, ...attributes } = { ...parsed.attrs, range: "from" };
|
|
|
|
delete attributes.to;
|
|
|
|
[attributes.date, attributes.time] = from.split("T");
|
|
|
|
addLocalDate(attributes, state, buffer, applyDataAttributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed.attrs.from && parsed.attrs.to) {
|
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = "→";
|
|
|
|
buffer.push(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed.attrs.to) {
|
|
|
|
const { to, ...attributes } = { ...parsed.attrs, range: "to" };
|
|
|
|
delete attributes.from;
|
|
|
|
[attributes.date, attributes.time] = to.split("T");
|
|
|
|
addLocalDate(attributes, state, buffer, applyDataAttributes);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = matches[0];
|
|
|
|
buffer.push(token);
|
|
|
|
}
|
2018-05-04 14:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
2021-09-22 22:49:45 +08:00
|
|
|
helper.allowList([
|
|
|
|
"span.discourse-local-date",
|
|
|
|
"span[aria-label]",
|
|
|
|
"span[data-calendar]",
|
2024-05-24 16:47:45 +08:00
|
|
|
"span[data-countdown]",
|
|
|
|
"span[data-date]",
|
2021-09-22 22:49:45 +08:00
|
|
|
"span[data-displayed-timezone]",
|
2024-05-24 16:47:45 +08:00
|
|
|
"span[data-email-preview]",
|
|
|
|
"span[data-format]",
|
|
|
|
"span[data-recurring]",
|
|
|
|
"span[data-time]",
|
2021-09-22 22:49:45 +08:00
|
|
|
"span[data-timezone]",
|
|
|
|
"span[data-timezones]",
|
|
|
|
]);
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
|
|
helper.registerOptions((opts, siteSettings) => {
|
2019-01-16 19:53:41 +08:00
|
|
|
opts.datesEmailFormat = siteSettings.discourse_local_dates_email_format;
|
|
|
|
|
2022-07-06 16:37:54 +08:00
|
|
|
opts.features["discourse-local-dates"] =
|
|
|
|
!!siteSettings.discourse_local_dates_enabled;
|
2018-05-04 14:51:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
2024-05-24 16:47:45 +08:00
|
|
|
md.core.textPostProcess.ruler.push("date", {
|
|
|
|
matcher: /\[date=.+?\]/,
|
|
|
|
onMatch: date,
|
|
|
|
});
|
2022-01-10 15:02:36 +08:00
|
|
|
|
2024-05-24 16:47:45 +08:00
|
|
|
md.core.textPostProcess.ruler.push("date-range", {
|
|
|
|
matcher: /\[date-range .+?\]/,
|
|
|
|
onMatch: range,
|
|
|
|
});
|
2022-01-10 15:02:36 +08:00
|
|
|
});
|
2018-05-04 14:51:41 +08:00
|
|
|
}
|