2018-06-16 00:42:20 +08:00
|
|
|
|
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
|
|
|
|
function addLocalDate(buffer, matches, state) {
|
|
|
|
|
let token;
|
|
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
|
date: null,
|
|
|
|
|
time: null,
|
2018-10-16 07:34:55 +08:00
|
|
|
|
timezone: null,
|
2018-11-23 00:19:24 +08:00
|
|
|
|
format: null,
|
|
|
|
|
timezones: null,
|
2019-08-25 00:39:20 +08:00
|
|
|
|
displayedTimezone: null,
|
|
|
|
|
countdown: null,
|
2018-05-04 14:51:41 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-23 16:35:43 +08:00
|
|
|
|
const matchString = matches[1].replace(/‘|’|„|“|«|»|”/g, '"');
|
2019-11-21 18:43:35 +08:00
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
|
let parsed = parseBBCodeTag(
|
2019-11-21 18:43:35 +08:00
|
|
|
|
"[date date" + matchString + "]",
|
2018-06-16 00:42:20 +08:00
|
|
|
|
0,
|
2019-11-21 18:43:35 +08:00
|
|
|
|
matchString.length + 11
|
2018-06-16 00:42:20 +08:00
|
|
|
|
);
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
|
|
|
|
config.date = parsed.attrs.date;
|
2018-11-23 00:19:24 +08:00
|
|
|
|
config.format = parsed.attrs.format;
|
|
|
|
|
config.calendar = parsed.attrs.calendar;
|
2018-05-04 14:51:41 +08:00
|
|
|
|
config.time = parsed.attrs.time;
|
2020-12-08 07:57:18 +08:00
|
|
|
|
config.timezone = (parsed.attrs.timezone || "").trim();
|
2018-05-05 02:22:37 +08:00
|
|
|
|
config.recurring = parsed.attrs.recurring;
|
2018-11-23 00:19:24 +08:00
|
|
|
|
config.timezones = parsed.attrs.timezones;
|
2018-11-26 21:20:32 +08:00
|
|
|
|
config.displayedTimezone = parsed.attrs.displayedTimezone;
|
2019-08-25 00:39:20 +08:00
|
|
|
|
config.countdown = parsed.attrs.countdown;
|
2018-05-04 14:51:41 +08:00
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
|
token = new state.Token("span_open", "span", 1);
|
2018-11-26 21:20:32 +08:00
|
|
|
|
token.attrs = [["data-date", state.md.utils.escapeHtml(config.date)]];
|
|
|
|
|
|
|
|
|
|
if (!config.date.match(/\d{4}-\d{2}-\d{2}/)) {
|
|
|
|
|
closeBuffer(buffer, state, moment.invalid().format());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.time && !config.time.match(/\d{2}:\d{2}(?::\d{2})?/)) {
|
|
|
|
|
closeBuffer(buffer, state, moment.invalid().format());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-08-27 17:19:30 +08:00
|
|
|
|
|
2018-10-16 18:49:43 +08:00
|
|
|
|
let dateTime = config.date;
|
2018-10-12 05:41:57 +08:00
|
|
|
|
if (config.time) {
|
2018-10-12 06:26:35 +08:00
|
|
|
|
token.attrs.push(["data-time", state.md.utils.escapeHtml(config.time)]);
|
2018-10-16 18:49:43 +08:00
|
|
|
|
dateTime = `${dateTime} ${config.time}`;
|
2018-10-12 05:41:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 21:20:32 +08:00
|
|
|
|
if (!moment(dateTime).isValid()) {
|
|
|
|
|
closeBuffer(buffer, state, moment.invalid().format());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token.attrs.push(["class", "discourse-local-date"]);
|
|
|
|
|
|
2018-11-23 00:19:24 +08:00
|
|
|
|
if (config.format) {
|
|
|
|
|
token.attrs.push(["data-format", state.md.utils.escapeHtml(config.format)]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 00:39:20 +08:00
|
|
|
|
if (config.countdown) {
|
|
|
|
|
token.attrs.push([
|
|
|
|
|
"data-countdown",
|
|
|
|
|
state.md.utils.escapeHtml(config.countdown),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 00:19:24 +08:00
|
|
|
|
if (config.calendar) {
|
|
|
|
|
token.attrs.push([
|
|
|
|
|
"data-calendar",
|
|
|
|
|
state.md.utils.escapeHtml(config.calendar),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 21:20:32 +08:00
|
|
|
|
if (
|
|
|
|
|
config.displayedTimezone &&
|
|
|
|
|
moment.tz.names().includes(config.displayedTimezone)
|
|
|
|
|
) {
|
2018-11-23 00:19:24 +08:00
|
|
|
|
token.attrs.push([
|
2018-11-26 21:20:32 +08:00
|
|
|
|
"data-displayed-timezone",
|
|
|
|
|
state.md.utils.escapeHtml(config.displayedTimezone),
|
2018-11-23 00:19:24 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.timezones) {
|
2018-11-26 21:20:32 +08:00
|
|
|
|
const timezones = config.timezones.split("|").filter((timezone) => {
|
|
|
|
|
return moment.tz.names().includes(timezone);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-23 00:19:24 +08:00
|
|
|
|
token.attrs.push([
|
|
|
|
|
"data-timezones",
|
2018-11-26 21:20:32 +08:00
|
|
|
|
state.md.utils.escapeHtml(timezones.join("|")),
|
2018-11-23 00:19:24 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 21:20:32 +08:00
|
|
|
|
if (config.timezone && moment.tz.names().includes(config.timezone)) {
|
2018-09-17 10:26:48 +08:00
|
|
|
|
token.attrs.push([
|
2018-10-16 07:34:55 +08:00
|
|
|
|
"data-timezone",
|
|
|
|
|
state.md.utils.escapeHtml(config.timezone),
|
2018-09-17 10:26:48 +08:00
|
|
|
|
]);
|
2018-10-16 18:49:43 +08:00
|
|
|
|
dateTime = moment.tz(dateTime, config.timezone);
|
2018-10-09 22:45:32 +08:00
|
|
|
|
} else {
|
2018-10-16 18:49:43 +08:00
|
|
|
|
dateTime = moment.utc(dateTime);
|
2018-09-12 21:54:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 02:45:32 +08:00
|
|
|
|
if (config.recurring) {
|
2018-08-27 17:19:30 +08:00
|
|
|
|
token.attrs.push([
|
|
|
|
|
"data-recurring",
|
|
|
|
|
state.md.utils.escapeHtml(config.recurring),
|
|
|
|
|
]);
|
2018-05-05 02:45:32 +08:00
|
|
|
|
}
|
2018-11-23 00:19:24 +08:00
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
|
buffer.push(token);
|
|
|
|
|
|
2019-01-16 19:53:41 +08:00
|
|
|
|
const formattedDateTime = dateTime
|
|
|
|
|
.tz("Etc/UTC")
|
|
|
|
|
.format(
|
|
|
|
|
state.md.options.discourse.datesEmailFormat || moment.defaultFormat
|
|
|
|
|
);
|
|
|
|
|
token.attrs.push(["data-email-preview", `${formattedDateTime} UTC`]);
|
2018-05-18 21:35:37 +08:00
|
|
|
|
|
2018-11-26 21:20:32 +08:00
|
|
|
|
closeBuffer(buffer, state, dateTime.utc().format(config.format));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeBuffer(buffer, state, text) {
|
|
|
|
|
let token;
|
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
|
token = new state.Token("text", "", 0);
|
2018-11-26 21:20:32 +08:00
|
|
|
|
token.content = text;
|
2018-05-04 14:51:41 +08:00
|
|
|
|
buffer.push(token);
|
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
|
token = new state.Token("span_close", "span", -1);
|
2018-11-23 00:19:24 +08:00
|
|
|
|
|
2018-05-04 14:51:41 +08:00
|
|
|
|
buffer.push(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setup(helper) {
|
2020-10-28 10:22:06 +08:00
|
|
|
|
helper.allowList([
|
2018-06-16 00:42:20 +08:00
|
|
|
|
"span.discourse-local-date",
|
|
|
|
|
"span[data-*]",
|
2019-03-26 23:31:48 +08:00
|
|
|
|
"span[aria-label]",
|
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;
|
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
|
opts.features[
|
|
|
|
|
"discourse-local-dates"
|
|
|
|
|
] = !!siteSettings.discourse_local_dates_enabled;
|
2018-05-04 14:51:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
|
const rule = {
|
2019-02-19 05:39:45 +08:00
|
|
|
|
matcher: /\[date(=.+?)\]/,
|
2018-05-04 14:51:41 +08:00
|
|
|
|
onMatch: addLocalDate,
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
|
md.core.textPostProcess.ruler.push("discourse-local-dates", rule);
|
2018-05-04 14:51:41 +08:00
|
|
|
|
});
|
|
|
|
|
}
|