diff options
author | Douwe Maan <douwe@gitlab.com> | 2018-02-02 22:26:14 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-02-02 22:26:14 +0300 |
commit | 974bb446bf834bcf3ead9b69b0809e62b28a1bc9 (patch) | |
tree | 898d1fa54e02cc870464f79c94e70a43a1714784 /lib/banzai/filter/color_filter.rb | |
parent | ee600a84d29d1ecbfdb3e9cb1a55c610ed65ab10 (diff) | |
parent | 57f8b24fe2d54df4dfcc3ddcb5796e41093f0fd7 (diff) |
Merge branch '24167__color_label' into 'master'
Add Colors to GitLab Flavored Markdown
Closes #24167
See merge request gitlab-org/gitlab-ce!16095
Diffstat (limited to 'lib/banzai/filter/color_filter.rb')
-rw-r--r-- | lib/banzai/filter/color_filter.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/banzai/filter/color_filter.rb b/lib/banzai/filter/color_filter.rb new file mode 100644 index 00000000000..6ab29ac281f --- /dev/null +++ b/lib/banzai/filter/color_filter.rb @@ -0,0 +1,31 @@ +module Banzai + module Filter + # HTML filter that renders `color` followed by a color "chip". + # + class ColorFilter < HTML::Pipeline::Filter + COLOR_CHIP_CLASS = 'gfm-color_chip'.freeze + + def call + doc.css('code').each do |node| + color = ColorParser.parse(node.content) + node << color_chip(color) if color + end + + doc + end + + private + + def color_chip(color) + checkerboard = doc.document.create_element('span', class: COLOR_CHIP_CLASS) + chip = doc.document.create_element('span', style: inline_styles(color: color)) + + checkerboard << chip + end + + def inline_styles(color:) + "background-color: #{color};" + end + end + end +end |