Rename timezone attribute and add it to local date details field

This commit is contained in:
Vinoth Kannan 2018-10-16 05:04:55 +05:30
parent 8d06731484
commit 2c8c1bf188
5 changed files with 16 additions and 14 deletions

View File

@ -12,9 +12,9 @@
} }
var relativeTime; var relativeTime;
if (options.forceTimezone) { if (options.timezone) {
relativeTime = moment relativeTime = moment
.tz(options.date + " " + options.time, options.forceTimezone) .tz(options.date + " " + options.time, options.timezone)
.utc(); .utc();
} else { } else {
relativeTime = moment.utc(options.date + " " + options.time); relativeTime = moment.utc(options.date + " " + options.time);
@ -104,7 +104,7 @@
options.time = $this.attr("data-time") || "00:00:00"; options.time = $this.attr("data-time") || "00:00:00";
options.recurring = $this.attr("data-recurring"); options.recurring = $this.attr("data-recurring");
options.timezones = $this.attr("data-timezones"); options.timezones = $this.attr("data-timezones");
options.forceTimezone = $this.attr("data-force-timezone"); options.timezone = $this.attr("data-timezone");
processElement($this, options); processElement($this, options);
}); });

View File

@ -6,7 +6,7 @@ function addLocalDate(buffer, matches, state) {
let config = { let config = {
date: null, date: null,
time: null, time: null,
forceTimezone: null, timezone: null,
format: "YYYY-MM-DD HH:mm:ss", format: "YYYY-MM-DD HH:mm:ss",
timezones: "Etc/UTC" timezones: "Etc/UTC"
}; };
@ -19,7 +19,7 @@ function addLocalDate(buffer, matches, state) {
config.date = parsed.attrs.date; config.date = parsed.attrs.date;
config.time = parsed.attrs.time; config.time = parsed.attrs.time;
config.forceTimezone = parsed.attrs.forceTimezone || parsed.attrs.timezone; config.timezone = parsed.attrs.timezone;
config.recurring = parsed.attrs.recurring; config.recurring = parsed.attrs.recurring;
config.format = parsed.attrs.format || config.format; config.format = parsed.attrs.format || config.format;
config.timezones = parsed.attrs.timezones || config.timezones; config.timezones = parsed.attrs.timezones || config.timezones;
@ -37,12 +37,12 @@ function addLocalDate(buffer, matches, state) {
} }
let dateTime; let dateTime;
if (config.forceTimezone) { if (config.timezone) {
token.attrs.push([ token.attrs.push([
"data-force-timezone", "data-timezone",
state.md.utils.escapeHtml(config.forceTimezone) state.md.utils.escapeHtml(config.timezone)
]); ]);
dateTime = moment.tz(`${config.date} ${config.time}`, config.forceTimezone); dateTime = moment.tz(`${config.date} ${config.time}`, config.timezone);
} else { } else {
dateTime = moment.utc(`${config.date} ${config.time}`); dateTime = moment.utc(`${config.date} ${config.time}`);
} }

View File

@ -27,9 +27,10 @@ after_initialize do
dates = doc.css('span.discourse-local-date').map do |cooked_date| dates = doc.css('span.discourse-local-date').map do |cooked_date|
date = {} date = {}
cooked_date.attributes.values.each do |attribute| cooked_date.attributes.values.each do |attribute|
if attribute.name && ['data-date', 'data-time'].include?(attribute.name) data_name = attribute.name&.gsub('data-', '')
if data_name && ['date', 'time', 'timezone'].include?(data_name)
unless attribute.value == 'undefined' unless attribute.value == 'undefined'
date[attribute.name.gsub('data-', '')] = CGI.escapeHTML(attribute.value || "") date[data_name] = CGI.escapeHTML(attribute.value || "")
end end
end end
end end

View File

@ -15,7 +15,7 @@ RSpec.describe "Local Dates" do
expect(cooked).to include('class="discourse-local-date"') expect(cooked).to include('class="discourse-local-date"')
expect(cooked).to include('data-date="2018-05-08"') expect(cooked).to include('data-date="2018-05-08"')
expect(cooked).to include('data-format="L LTS"') expect(cooked).to include('data-format="L LTS"')
expect(cooked).not_to include('data-force-timezone=') expect(cooked).not_to include('data-timezone=')
expect(cooked).to include( expect(cooked).to include(
'data-timezones="Europe/Paris|America/Los_Angeles"' 'data-timezones="Europe/Paris|America/Los_Angeles"'
@ -32,7 +32,7 @@ RSpec.describe "Local Dates" do
cooked = post.cooked cooked = post.cooked
expect(cooked).to include('data-force-timezone="Asia/Calcutta"') expect(cooked).to include('data-timezone="Asia/Calcutta"')
expect(cooked).to include('05/08/2018 4:30:00 PM') expect(cooked).to include('05/08/2018 4:30:00 PM')
end end

View File

@ -9,13 +9,14 @@ describe Post do
describe '#local_dates' do describe '#local_dates' do
it "should have correct custom fields" do it "should have correct custom fields" do
post = Fabricate(:post, raw: <<~SQL) post = Fabricate(:post, raw: <<~SQL)
[date=2018-09-17 time=01:39:00 format="LLL" timezones="Europe/Paris|America/Los_Angeles"] [date=2018-09-17 time=01:39:00 format="LLL" timezone="Europe/Paris" timezones="Europe/Paris|America/Los_Angeles"]
SQL SQL
CookedPostProcessor.new(post).post_process CookedPostProcessor.new(post).post_process
expect(post.local_dates.count).to eq(1) expect(post.local_dates.count).to eq(1)
expect(post.local_dates[0]["date"]).to eq("2018-09-17") expect(post.local_dates[0]["date"]).to eq("2018-09-17")
expect(post.local_dates[0]["time"]).to eq("01:39:00") expect(post.local_dates[0]["time"]).to eq("01:39:00")
expect(post.local_dates[0]["timezone"]).to eq("Europe/Paris")
post.raw = "Text removed" post.raw = "Text removed"
post.save post.save