discourse/plugins/discourse-local-dates/assets/javascripts/lib/discourse-markdown/discourse-local-dates.js.es6

98 lines
2.7 KiB
Plaintext
Raw Normal View History

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,
forceTimezone: null,
format: "YYYY-MM-DD HH:mm:ss",
2018-05-04 14:51:41 +08:00
timezones: ""
};
2018-06-16 00:42:20 +08:00
let parsed = parseBBCodeTag(
"[date date" + matches[1] + "]",
0,
matches[1].length + 11
);
2018-05-04 14:51:41 +08:00
config.date = parsed.attrs.date;
config.time = parsed.attrs.time;
config.forceTimezone = parsed.attrs.forceTimezone || parsed.attrs.timezone;
config.recurring = parsed.attrs.recurring;
2018-05-04 14:51:41 +08:00
config.format = parsed.attrs.format || config.format;
config.timezones = parsed.attrs.timezones || config.timezones;
2018-06-16 00:42:20 +08:00
token = new state.Token("span_open", "span", 1);
2018-05-04 14:51:41 +08:00
token.attrs = [
2018-06-16 00:42:20 +08:00
["class", "discourse-local-date"],
2018-08-27 17:19:30 +08:00
["data-date", state.md.utils.escapeHtml(config.date)],
["data-time", state.md.utils.escapeHtml(config.time)],
["data-format", state.md.utils.escapeHtml(config.format)],
2018-09-17 10:26:48 +08:00
["data-timezones", state.md.utils.escapeHtml(config.timezones)]
2018-05-04 14:51:41 +08:00
];
2018-08-27 17:19:30 +08:00
let dateTime;
if (config.forceTimezone) {
2018-09-17 10:26:48 +08:00
token.attrs.push([
"data-force-timezone",
state.md.utils.escapeHtml(config.forceTimezone)
]);
dateTime = moment.tz(`${config.date} ${config.time}`, config.forceTimezone);
} else {
dateTime = moment.utc(`${config.date} ${config.time}`);
}
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-05-04 14:51:41 +08:00
buffer.push(token);
let emailPreview;
const emailTimezone = config.timezones.split("|")[0];
const formattedDateTime = dateTime.tz(emailTimezone).format(config.format);
const formattedTimezone = emailTimezone.replace("/", ": ").replace("_", " ");
2018-05-04 14:51:41 +08:00
if (formattedDateTime.match(/TZ/)) {
emailPreview = formattedDateTime.replace("TZ", formattedTimezone);
} else {
emailPreview = `${formattedDateTime} (${formattedTimezone})`;
}
2018-05-04 14:51:41 +08:00
token.attrs.push(["data-email-preview", emailPreview]);
2018-06-16 00:42:20 +08:00
token = new state.Token("text", "", 0);
2018-10-10 06:59:34 +08:00
token.content = dateTime.utc().format(config.format);
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-05-04 14:51:41 +08:00
buffer.push(token);
}
export function setup(helper) {
helper.whiteList([
2018-06-16 00:42:20 +08:00
"span.discourse-local-date",
"span[data-*]",
"span[title]"
2018-05-04 14:51:41 +08:00
]);
helper.registerOptions((opts, siteSettings) => {
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 = {
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
});
}