2018-06-15 12:42:20 -04:00
|
|
|
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
|
2018-05-04 02:51:41 -04:00
|
|
|
|
|
|
|
function addLocalDate(buffer, matches, state) {
|
|
|
|
let token;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
date: null,
|
|
|
|
time: null,
|
2018-10-09 10:45:32 -04:00
|
|
|
forceTimezone: null,
|
2018-08-30 06:38:05 -04:00
|
|
|
format: "YYYY-MM-DD HH:mm:ss",
|
2018-05-04 02:51:41 -04:00
|
|
|
timezones: ""
|
|
|
|
};
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
let parsed = parseBBCodeTag(
|
|
|
|
"[date date" + matches[1] + "]",
|
|
|
|
0,
|
|
|
|
matches[1].length + 11
|
|
|
|
);
|
2018-05-04 02:51:41 -04:00
|
|
|
|
|
|
|
config.date = parsed.attrs.date;
|
|
|
|
config.time = parsed.attrs.time;
|
2018-10-09 10:45:32 -04:00
|
|
|
config.forceTimezone = parsed.attrs.forceTimezone || parsed.attrs.timezone;
|
2018-05-04 14:22:37 -04:00
|
|
|
config.recurring = parsed.attrs.recurring;
|
2018-05-04 02:51:41 -04:00
|
|
|
config.format = parsed.attrs.format || config.format;
|
|
|
|
config.timezones = parsed.attrs.timezones || config.timezones;
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
token = new state.Token("span_open", "span", 1);
|
2018-05-04 02:51:41 -04:00
|
|
|
token.attrs = [
|
2018-06-15 12:42:20 -04:00
|
|
|
["class", "discourse-local-date"],
|
2018-08-27 05:19:30 -04: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-16 22:26:48 -04:00
|
|
|
["data-timezones", state.md.utils.escapeHtml(config.timezones)]
|
2018-05-04 02:51:41 -04:00
|
|
|
];
|
2018-08-27 05:19:30 -04:00
|
|
|
|
2018-10-09 10:45:32 -04:00
|
|
|
let dateTime;
|
2018-09-12 09:54:36 -04:00
|
|
|
if (config.forceTimezone) {
|
2018-09-16 22:26:48 -04:00
|
|
|
token.attrs.push([
|
|
|
|
"data-force-timezone",
|
|
|
|
state.md.utils.escapeHtml(config.forceTimezone)
|
|
|
|
]);
|
2018-10-09 10:45:32 -04:00
|
|
|
dateTime = moment.tz(`${config.date} ${config.time}`, config.forceTimezone);
|
|
|
|
} else {
|
|
|
|
dateTime = moment.utc(`${config.date} ${config.time}`);
|
2018-09-12 09:54:36 -04:00
|
|
|
}
|
|
|
|
|
2018-05-04 14:45:32 -04:00
|
|
|
if (config.recurring) {
|
2018-08-27 05:19:30 -04:00
|
|
|
token.attrs.push([
|
|
|
|
"data-recurring",
|
|
|
|
state.md.utils.escapeHtml(config.recurring)
|
|
|
|
]);
|
2018-05-04 14:45:32 -04:00
|
|
|
}
|
2018-05-04 02:51:41 -04:00
|
|
|
buffer.push(token);
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const previews = config.timezones
|
|
|
|
.split("|")
|
|
|
|
.filter(t => t)
|
|
|
|
.map(timezone => {
|
2018-10-09 10:45:32 -04:00
|
|
|
const formattedDateTime = dateTime.tz(timezone).format(config.format);
|
2018-05-04 02:51:41 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
const formattedTimezone = timezone.replace("/", ": ").replace("_", " ");
|
2018-05-04 02:51:41 -04:00
|
|
|
|
2018-10-09 10:45:32 -04:00
|
|
|
if (formattedDateTime.match(/TZ/)) {
|
|
|
|
return formattedDateTime.replace("TZ", formattedTimezone);
|
2018-06-15 12:42:20 -04:00
|
|
|
} else {
|
2018-10-09 10:45:32 -04:00
|
|
|
return `${formattedDateTime} (${formattedTimezone})`;
|
2018-06-15 12:42:20 -04:00
|
|
|
}
|
|
|
|
});
|
2018-05-04 02:51:41 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
token.attrs.push(["data-email-preview", previews[0]]);
|
2018-05-18 09:35:37 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
token = new state.Token("text", "", 0);
|
2018-05-04 02:51:41 -04:00
|
|
|
token.content = previews.join(", ");
|
|
|
|
buffer.push(token);
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
token = new state.Token("span_close", "span", -1);
|
2018-05-04 02:51:41 -04:00
|
|
|
buffer.push(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
helper.whiteList([
|
2018-06-15 12:42:20 -04:00
|
|
|
"span.discourse-local-date",
|
|
|
|
"span[data-*]",
|
|
|
|
"span[title]"
|
2018-05-04 02:51:41 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
helper.registerOptions((opts, siteSettings) => {
|
2018-06-15 12:42:20 -04:00
|
|
|
opts.features[
|
|
|
|
"discourse-local-dates"
|
|
|
|
] = !!siteSettings.discourse_local_dates_enabled;
|
2018-05-04 02:51:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
helper.registerPlugin(md => {
|
|
|
|
const rule = {
|
2018-05-24 04:56:04 -04:00
|
|
|
matcher: /\[date(.+?)\]/,
|
2018-05-04 02:51:41 -04:00
|
|
|
onMatch: addLocalDate
|
|
|
|
};
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
md.core.textPostProcess.ruler.push("discourse-local-dates", rule);
|
2018-05-04 02:51:41 -04:00
|
|
|
});
|
|
|
|
}
|