Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Use html5 <time> tag for timestamps.
Rohitt Vashishtha6ea3816fa60f
diff --git a/frontend_tests/node_tests/rendered_markdown.js b/frontend_tests/node_tests/rendered_markdown.jsindex 21caa616fbe..4dd31110627 100644--- a/frontend_tests/node_tests/rendered_markdown.js+++ b/frontend_tests/node_tests/rendered_markdown.js@@ -68,7 +68,7 @@ const get_content_element = () => {$content.set_find_results('.user-group-mention', $array([]));$content.set_find_results('a.stream', $array([]));$content.set_find_results('a.stream-topic', $array([]));- $content.set_find_results('span.timestamp', $array([]));+ $content.set_find_results('time', $array([]));$content.set_find_results('.emoji', $array([]));$content.set_find_results('div.spoiler-header', $array([]));return $content;@@ -160,11 +160,10 @@ run_test('timestamp', () => {// Setupconst $content = get_content_element();const $timestamp = $.create('timestamp(valid)');- $timestamp.attr('data-timestamp', 1);+ $timestamp.attr('datetime', '1970-01-01T00:00:01Z');const $timestamp_invalid = $.create('timestamp(invalid)');- $timestamp.addClass('timestamp');- $timestamp_invalid.addClass('timestamp');- $content.set_find_results('span.timestamp', $array([$timestamp, $timestamp_invalid]));+ $timestamp_invalid.attr('datetime', 'invalid');+ $content.set_find_results('time', $array([$timestamp, $timestamp_invalid]));// Initial assertsassert.equal($timestamp.text(), 'never-been-set');@@ -173,12 +172,9 @@ run_test('timestamp', () => {rm.update_elements($content);// Final asserts- assert($timestamp.hasClass('timestamp'));- assert(!$timestamp_invalid.hasClass('timestamp'));assert.equal($timestamp.text(), 'Thu, Jan 1 1970, 12:00 AM');assert.equal($timestamp.attr('title'), "This time is in your timezone. Original text was 'never-been-set'.");- assert.equal($timestamp_invalid.text(), 'never-been-set');- assert.equal($timestamp_invalid.attr('title'), 'Could not parse timestamp.');+ assert.equal($timestamp_invalid.text(), 'translated: Could not parse timestamp.');});run_test('emoji', () => {diff --git a/static/js/markdown.js b/static/js/markdown.jsindex a5eb10577a8..158a1854b92 100644--- a/static/js/markdown.js+++ b/static/js/markdown.js@@ -300,15 +300,17 @@ function handleTimestamp(time) {// JavaScript dates are in milliseconds, Unix timestamps are in secondstimeobject = moment(time * 1000);}- const istimevalid = !(timeobject === null || !timeobject.isValid());- // Generate HTML- let timestring = '<span class="timestamp"';- if (istimevalid) {- timestring += ' data-timestamp="' + timeobject.unix() + '"';+ const escaped_time = _.escape(time);+ if (timeobject === null || !timeobject.isValid()) {+ // Unsupported time format: rerender accordingly.+ return `<span class="timestamp-error">Invalid time format: ${escaped_time}</span>`;}- timestring += '>' + _.escape(time) + '</span>';- return timestring;++ // Use html5 <time> tag for valid timestamps.+ // render time without milliseconds.+ const escaped_isotime = _.escape(timeobject.toISOString().split('.')[0] + 'Z');+ return `<time datetime="${escaped_isotime}">${escaped_time}</time>`;}function handleStream(stream_name) {diff --git a/static/js/rendered_markdown.js b/static/js/rendered_markdown.jsindex cfcbcb00435..543e43fcab8 100644--- a/static/js/rendered_markdown.js+++ b/static/js/rendered_markdown.js@@ -137,19 +137,27 @@ exports.update_elements = (content) => {}});- content.find('span.timestamp').each(function () {+ content.find('time').each(function () {// Populate each timestamp span with mentioned time// in user's local timezone.- const timestamp = moment.unix($(this).attr('data-timestamp'));- if (timestamp.isValid() && $(this).attr('data-timestamp') !== null) {+ const time_str = $(this).attr('datetime');+ if (time_str === undefined) {+ return;+ }++ // Moment throws a large deprecation warning when it has to fallback+ // to the Date() constructor. We needn't worry here and can let bugdown+ // handle any dates that moment misses.+ moment.suppressDeprecationWarnings = true;+ const timestamp = moment(time_str);+ if (timestamp.isValid()) {const text = $(this).text();const rendered_time = timerender.render_markdown_timestamp(timestamp,null, text);$(this).text(rendered_time.text);$(this).attr('title', rendered_time.title);} else {- $(this).removeClass('timestamp');- $(this).attr('title', 'Could not parse timestamp.');+ $(this).text(i18n.t('Could not parse timestamp.'));}});diff --git a/static/styles/rendered_markdown.scss b/static/styles/rendered_markdown.scssindex a1ef8eb2dca..1d14258e9c7 100644--- a/static/styles/rendered_markdown.scss+++ b/static/styles/rendered_markdown.scss@@ -142,7 +142,7 @@}/* Timestamps */- .timestamp {+ time {background: hsl(0, 0%, 93%);border-radius: 3px;padding: 0 0.2em;diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.pyindex 1a36f16a2b4..209d4cf6922 100644--- a/zerver/lib/bugdown/__init__.py+++ b/zerver/lib/bugdown/__init__.py@@ -33,6 +33,7 @@ import ahocorasickimport dateutil.parserimport dateutil.tzimport markdown+import pytzimport requestsfrom django.conf import settingsfrom django.db.models import Q@@ -1229,23 +1230,34 @@ def possible_avatar_emails(content: str) -> Set[str]:class Timestamp(markdown.inlinepatterns.Pattern):def handleMatch(self, match: Match[str]) -> Optional[Element]:- span = Element('span')- span.set('class', 'timestamp')+ time_input_string = match.group('time')timestamp = Nonetry:- timestamp = dateutil.parser.parse(match.group('time'), tzinfos=get_common_timezones())+ timestamp = dateutil.parser.parse(time_input_string, tzinfos=get_common_timezones())except ValueError:try:- timestamp = datetime.fromtimestamp(float(match.group('time')))+ timestamp = datetime.fromtimestamp(float(time_input_string))except ValueError:pass- if timestamp:- if timestamp.tzinfo:- timestamp = timestamp - timestamp.utcoffset()- span.set('data-timestamp', timestamp.strftime("%s"))- # Set text to initial input, so even if parsing fails, the data remains intact.- span.text = markdown.util.AtomicString(match.group('time'))- return span++ if not timestamp:+ error_element = Element('span')+ error_element.set('class', 'timestamp-error')+ error_element.text = markdown.util.AtomicString(+ f"Invalid time format: {time_input_string}")+ return error_element++ # Use HTML5 <time> element for valid timestamps.+ time_element = Element('time')+ if timestamp.tzinfo:+ timestamp = timestamp.astimezone(pytz.utc)+ else:+ timestamp = pytz.utc.localize(timestamp)+ time_element.set('datetime', timestamp.isoformat().replace('+00:00', 'Z'))+ # Set text to initial input, so simple clients translating+ # HTML to text will at least display something.+ time_element.text = markdown.util.AtomicString(time_input_string)+ return time_element# All of our emojis(non ZWJ sequences) belong to one of these unicode blocks:# \U0001f100-\U0001f1ff - Enclosed Alphanumeric Supplementdiff --git a/zerver/tests/fixtures/markdown_test_cases.json b/zerver/tests/fixtures/markdown_test_cases.jsonindex 9d726c7f8bb..9f794df1b7c 100644--- a/zerver/tests/fixtures/markdown_test_cases.json+++ b/zerver/tests/fixtures/markdown_test_cases.json@@ -743,34 +743,34 @@{"name": "timestamp_bugdown_only","input": "!time(Jun 5th 2017, 10:30PM)",- "expected_output": "<p><span class=\"timestamp\" data-timestamp=\"1496701800\">Jun 5th 2017, 10:30PM</span></p>",- "marked_expected_output": "<p><span class=\"timestamp\">Jun 5th 2017, 10:30PM</span></p>"+ "expected_output": "<p><time datetime=\"2017-06-05T22:30:00Z\">Jun 5th 2017, 10:30PM</time></p>",+ "marked_expected_output": "<p><span class=\"timestamp-error\">Invalid time format: Jun 5th 2017, 10:30PM</span></p>"},{"name": "timestamp_bugdown_and_marked","input": "!time(31 Dec 2017)",- "expected_output": "<p><span class=\"timestamp\" data-timestamp=\"1514678400\">31 Dec 2017</span></p>"+ "expected_output": "<p><time datetime=\"2017-12-31T00:00:00Z\">31 Dec 2017</time></p>"},{"name": "timestamp_invalid_input","input": "!time(<alert(1)>)",- "expected_output": "<p><span class=\"timestamp\"><alert(1</span>>)</p>"+ "expected_output": "<p><span class=\"timestamp-error\">Invalid time format: <alert(1</span>>)</p>"},{"name": "timestamp_timezone","input": "!time(31 Dec 2017 5:30 am IST)",- "expected_output": "<p><span class=\"timestamp\" data-timestamp=\"1514678400\">31 Dec 2017 5:30 am IST</span></p>",- "marked_expected_output": "<p><span class=\"timestamp\">31 Dec 2017 5:30 am IST</span></p>"+ "expected_output": "<p><time datetime=\"2017-12-31T00:00:00Z\">31 Dec 2017 5:30 am IST</time></p>",+ "marked_expected_output": "<p><span class=\"timestamp-error\">Invalid time format: 31 Dec 2017 5:30 am IST</span></p>"},{"name": "timestamp_incorrect",- "input": "!time(hello world)",- "expected_output": "<p><span class=\"timestamp\">hello world</span></p>"+ "input": "!time(**hello world**)",+ "expected_output": "<p><span class=\"timestamp-error\">Invalid time format: **hello world**</span></p>"},{"name": "timestamp_unix","input": "!time(1496701800)",- "expected_output": "<p><span class=\"timestamp\" data-timestamp=\"1496701800\">1496701800</span></p>"+ "expected_output": "<p><time datetime=\"2017-06-05T22:30:00Z\">1496701800</time></p>"},{"name": "tex_inline",