From fd6f45675a259ccadd9dfa402f12a68d314b2c5c Mon Sep 17 00:00:00 2001 From: smriti Date: Tue, 27 Jul 2021 15:33:50 +0530 Subject: [PATCH] Changes for red carpet html rendering github compliance Signed-off-by: smriti --- src/supermarket/Gemfile | 1 + src/supermarket/Gemfile.lock | 1 + .../app/helpers/markdown_helper.rb | 23 +++++++++++++++--- .../spec/helpers/markdown_helper_spec.rb | 24 ++++++++++++++++--- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/supermarket/Gemfile b/src/supermarket/Gemfile index 011bb630a..c4bc03ece 100644 --- a/src/supermarket/Gemfile +++ b/src/supermarket/Gemfile @@ -8,6 +8,7 @@ gem "omniauth-chef-oauth2" gem "omniauth-github" gem "omniauth-oauth2", "~> 1.7.1" gem "omniauth-rails_csrf_protection" +gem "coderay" #markdown doc - syntax highlighting gem "sidekiq", "~> 4.2" gem "sidekiq-cron" diff --git a/src/supermarket/Gemfile.lock b/src/supermarket/Gemfile.lock index e226f22ae..2aea422c0 100644 --- a/src/supermarket/Gemfile.lock +++ b/src/supermarket/Gemfile.lock @@ -790,6 +790,7 @@ DEPENDENCIES capybara-screenshot chef (~> 16.13) chefstyle + coderay compass-rails database_cleaner ddtrace diff --git a/src/supermarket/app/helpers/markdown_helper.rb b/src/supermarket/app/helpers/markdown_helper.rb index 138e7e2ea..ba77c3af9 100644 --- a/src/supermarket/app/helpers/markdown_helper.rb +++ b/src/supermarket/app/helpers/markdown_helper.rb @@ -2,16 +2,33 @@ module MarkdownHelper # # Make auto-links target=_blank # + class SupermarketRenderer < Redcarpet::Render::Safe include ActionView::Helpers::TagHelper def initialize(extensions = {}) super extensions.merge( link_attributes: { target: "_blank", rel: "noopener" }, - with_toc_data: true + with_toc_data: true, + hard_wrap: true, + xhtml: true ) end + #Syntax highlighting using CodeRay library + def block_code(code, language) + if language.present? + CodeRay.scan(code, language).div + else + "
#{code}
" + end + end + + #process doc to remove markdown comments as the same is not supported by RedCarpet + def remove_comments(raw_html) + raw_html.gsub(/<!--(.*?)-->/, "") + end + # # Last stop opportunity to transform the HTML Redcarpet has generated # from markdown input. @@ -25,7 +42,7 @@ def postprocess(html_document) # should be considered doc = Nokogiri::HTML::DocumentFragment.parse(html_document) doc = make_img_src_urls_protocol_relative(doc) - doc.to_s + remove_comments(doc.to_s) end private @@ -71,4 +88,4 @@ def render_markdown(text) text ).html_safe # rubocop:todo Rails/OutputSafety end -end +end \ No newline at end of file diff --git a/src/supermarket/spec/helpers/markdown_helper_spec.rb b/src/supermarket/spec/helpers/markdown_helper_spec.rb index 79351e4a5..98d97aa03 100644 --- a/src/supermarket/spec/helpers/markdown_helper_spec.rb +++ b/src/supermarket/spec/helpers/markdown_helper_spec.rb @@ -13,7 +13,20 @@ ``` CODEBLOCK - expect(helper.render_markdown(codeblock)).to match(/
/)
+      expect(helper.render_markdown(codeblock)).to include("
\n "\ + "
"\
+                                                            "$ bundle exec rake spec:all\n
") + end + + it "renders code block with syntax highlighting" do + codeblock = <<-CODEBLOCK.strip_heredoc + ```ruby + require 'redcarpet' + ``` + CODEBLOCK + + expect(helper.render_markdown(codeblock)).to include("
\n "\ + "
require")
     end
 
     it "auto renders links with target blank" do
@@ -33,13 +46,13 @@
     expect(helper.render_markdown(table)).to match(//)
   end
 
-  it "doesn't adds br tags on hard wraps" do
+  it "adds br tags on hard wraps" do
     markdown = <<-HARDWRAP.strip_heredoc
       There is no hard
       wrap.
     HARDWRAP
 
-    expect(helper.render_markdown(markdown)).to_not match(/
/) + expect(helper.render_markdown(markdown)).to match(/
/) end it "doesn't emphasize underscored words" do @@ -58,6 +71,10 @@ expect(helper.render_markdown("Supermarket^2")).to match(//) end + it "removes escaped comments" do + expect(helper.render_markdown("

Hello

")).to_not include("<!-- Comment -->") + end + context "protocol in URLs for images get converted" do it "HTTP -> protocol-relative" do html = helper.render_markdown("![](http://img.example.com)") @@ -68,6 +85,7 @@ html = helper.render_markdown("![](https://img.example.com)") expect(html).to include('') end + end describe "to prevent XSS attacks" do