summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancisco Javier López <fjlopez@gitlab.com>2019-09-09 18:54:46 +0200
committerFrancisco Javier López <fjlopez@gitlab.com>2019-09-09 18:54:46 +0200
commitf82cfbc62938e2237b3158b1c2c7dc19d9acb6fb (patch)
treec00da455393c0035af784a4571a2ce5b9fe08021
parent20fc3f668876e065a44b77104083eef5c7bac882 (diff)
downloadgitlab-ce-fj-datetime-filter.tar.gz
Added date time object to gfmfj-datetime-filter
-rw-r--r--lib/banzai/filter/date_time_filter.rb38
-rw-r--r--lib/banzai/pipeline/post_process_pipeline.rb1
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/banzai/filter/date_time_filter.rb b/lib/banzai/filter/date_time_filter.rb
new file mode 100644
index 00000000000..9116284e47f
--- /dev/null
+++ b/lib/banzai/filter/date_time_filter.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+module Banzai
+ module Filter
+ class DateTimeFilter < HTML::Pipeline::Filter
+ DATETIME_CLASS = 'gfm-date_time'.freeze
+
+ def call
+ doc.css('code').each do |node|
+ begin
+ date = Time.parse(node.content)
+ rescue
+ next
+ end
+
+
+ element = doc.document.create_element('span', class: DATETIME_CLASS, style: 'background-color: #DCDCDC; color: black')
+ element.content = if current_user
+ date.in_time_zone(current_user.timezone)
+ else
+ date
+ end
+
+ node.content = ''
+ node << element
+ end
+
+ doc
+ end
+
+ private
+
+ def current_user
+ context[:current_user]
+ end
+ end
+ end
+end
diff --git a/lib/banzai/pipeline/post_process_pipeline.rb b/lib/banzai/pipeline/post_process_pipeline.rb
index 54af26b41be..be3d1e546e4 100644
--- a/lib/banzai/pipeline/post_process_pipeline.rb
+++ b/lib/banzai/pipeline/post_process_pipeline.rb
@@ -5,6 +5,7 @@ module Banzai
class PostProcessPipeline < BasePipeline
def self.filters
@filters ||= FilterArray[
+ Filter::DateTimeFilter,
*internal_link_filters,
Filter::AbsoluteLinkFilter
]