Fix links without http(s) (#4155) [ci skip]

This commit is contained in:
Yash Dave 2019-10-07 19:22:07 +05:30 committed by Mac Siri
parent 9338e30303
commit 328d27ff41
2 changed files with 26 additions and 7 deletions

View file

@ -14,7 +14,11 @@ module Redcarpet
@options[:link_attributes]&.each do |attribute, value|
link_attributes += %( #{attribute}="#{value}")
end
%(<a href="#{link}"#{link_attributes}>#{content}</a>)
if (/\A(https?:\/\/)/.match? link) || link.nil?
%(<a href="#{link}"#{link_attributes}>#{content}</a>)
else
%(<a href="//#{link}"#{link_attributes}>#{content}</a>)
end
end
def header(title, header_number)

View file

@ -64,6 +64,21 @@ RSpec.describe MarkdownParser do
expect(generate_and_parse_markdown(code_span)).to include random_word
end
context "when rendering links markdown" do
# the following specs are testing HTMLRouge
it "renders properly if protocol is included" do
code_span = "[github](http://github.com)"
test = generate_and_parse_markdown(code_span)
expect(test).to eq("<p><a href=\"http://github.com\">github</a></p>\n\n")
end
it "renders properly if protocol is not included" do
code_span = "[github](github.com)"
test = generate_and_parse_markdown(code_span)
expect(test).to eq("<p><a href=\"//github.com\">github</a></p>\n\n")
end
end
describe "mentions" do
let(:user) { build_stubbed(:user) }
@ -248,17 +263,17 @@ RSpec.describe MarkdownParser do
end
end
context 'when word as snake case' do
context "when word as snake case" do
it "doesn't change word" do
code_block = "word_italic_"
expect(generate_and_parse_markdown(code_block)).to include("word_italic_")
end
end
context 'when double underline' do
it 'renders italic' do
code_block = "word__italic__"
expect(generate_and_parse_markdown(code_block)).to include("word_<em>italic</em>_")
end
context "when double underline" do
it "renders italic" do
code_block = "word__italic__"
expect(generate_and_parse_markdown(code_block)).to include("word_<em>italic</em>_")
end
end
end