Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve markdown rendering is changelogs / readmes #2081

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/supermarket/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/supermarket/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ DEPENDENCIES
capybara-screenshot
chef (~> 16.13)
chefstyle
coderay
compass-rails
database_cleaner
ddtrace
Expand Down
23 changes: 20 additions & 3 deletions src/supermarket/app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
"<pre><code>#{code}</code></pre>"
end
end

#process doc to remove markdown comments as the same is not supported by RedCarpet
def remove_comments(raw_html)
raw_html.gsub(/&lt;!--(.*?)--&gt;/, "")
end

#
# Last stop opportunity to transform the HTML Redcarpet has generated
# from markdown input.
Expand All @@ -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
Expand Down Expand Up @@ -71,4 +88,4 @@ def render_markdown(text)
text
).html_safe # rubocop:todo Rails/OutputSafety
end
end
end
24 changes: 21 additions & 3 deletions src/supermarket/spec/helpers/markdown_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@
```
CODEBLOCK

expect(helper.render_markdown(codeblock)).to match(/<pre><code>/)
expect(helper.render_markdown(codeblock)).to include("<div class=\"CodeRay\">\n "\
"<div class=\"code\"><pre>"\
"$ bundle exec rake spec:all\n</pre>")
end

it "renders code block with syntax highlighting" do
codeblock = <<-CODEBLOCK.strip_heredoc
```ruby
require 'redcarpet'
```
CODEBLOCK

expect(helper.render_markdown(codeblock)).to include("<div class=\"CodeRay\">\n "\
"<div class=\"code\"><pre>require")
end

it "auto renders links with target blank" do
Expand All @@ -33,13 +46,13 @@
expect(helper.render_markdown(table)).to match(/<table>/)
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(/<br>/)
expect(helper.render_markdown(markdown)).to match(/<br>/)
end

it "doesn't emphasize underscored words" do
Expand All @@ -58,6 +71,10 @@
expect(helper.render_markdown("Supermarket^2")).to match(/<sup>/)
end

it "removes escaped comments" do
expect(helper.render_markdown("<!-- Comment --><p>Hello</p>")).to_not include("&lt;!-- Comment --&gt;")
end

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

end

describe "to prevent XSS attacks" do
Expand Down