discourse/plugins/discourse-local-dates/assets/javascripts/lib/local-date-markup-generator.js
Martin Brennan 54a518b21d
FIX: Quoting local dates bbcode regeneration (#17141)
This commit allows quoting of discourse-local-date elements
and converts the quoted tags back into bbcode so that the
rendered quote will also render the discourse-local-date HTML.
This works on single dates as well as date ranges, and supports
all of the options used by discourse-local-date.

This also necessitated adding addTextDecorateCallback to the
to-markdown core lib (similar to addBlockDecorateCallback and
addTagDecorateCallback) to transform the text nodes between
date ranges to remove the -> in the final quote.

c.f. https://meta.discourse.org/t/quotes-that-contain-date-time/101999
2022-06-21 10:07:21 +10:00

59 lines
1.3 KiB
JavaScript

import { isEmpty } from "@ember/utils";
export default function generateDateMarkup(
fromDateTime,
options,
isRange,
toDateTime
) {
let text = ``;
if (isRange) {
let from = [fromDateTime.date, fromDateTime.time]
.filter((element) => !isEmpty(element))
.join("T");
let to = [toDateTime.date, toDateTime.time]
.filter((element) => !isEmpty(element))
.join("T");
text += `[date-range from=${from} to=${to}`;
} else {
text += `[date=${fromDateTime.date}`;
}
if (fromDateTime.time && !isRange) {
text += ` time=${fromDateTime.time}`;
}
if (fromDateTime.format && fromDateTime.format.length) {
text += ` format="${fromDateTime.format}"`;
}
if (options.timezone) {
text += ` timezone="${options.timezone}"`;
}
if (options.countdown) {
text += ` countdown="${options.countdown}"`;
}
if (options.displayedTimezone) {
text += ` displayedTimezone="${options.displayedTimezone}"`;
}
if (options.timezones && options.timezones.length) {
if (Array.isArray(options.timezones)) {
text += ` timezones="${options.timezones.join("|")}"`;
} else {
text += ` timezones="${options.timezones}"`;
}
}
if (options.recurring && !isRange) {
text += ` recurring="${options.recurring}"`;
}
text += `]`;
return text;
}