Skip to content
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 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ group :development do
gem "rake"
gem "bundler"
gem "pry"
gem "pry-nav"
gem "github-markdown"
end

Expand Down
13 changes: 7 additions & 6 deletions lib/html/pipeline/linkify_github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class LinkifyGitHubFilter < Filter

def call
doc.search("a").each do |element|
next if element.blank? || element.comment?
next if element["href"].to_s.empty?

text = element.content
text = element.inner_html

element.content = if is_a_pull_request_link? text
element.inner_html = if is_a_pull_request_link? text
replace_pull_request_link(text)
elsif is_a_issue_link? text
replace_issue_link(text)
Expand Down Expand Up @@ -57,19 +58,19 @@ def is_a_commit_link?(text)
end

def replace_pull_request_link(text)
text.match(PULL_REQUEST_REGEXP) do
text.gsub(PULL_REQUEST_REGEXP) do
pull_request_shorthand($1, $2, $3)
end
end

def replace_issue_link(text)
text.match(ISSUES_REGEXP) do
text.gsub(ISSUES_REGEXP) do
issue_shorthand($1, $2, $3)
end
end

def replace_commit_link(text)
text.match(COMMIT_REGEXP) do
text.gsub(COMMIT_REGEXP) do
commit_shorthand($1, $2, $3)
end
end
Expand All @@ -83,7 +84,7 @@ def issue_shorthand(repo, owner, number)
end

def commit_shorthand(repo, owner, number)
"#{repo}/#{owner}@`#{number[0..5]}`"
"#{repo}/#{owner}@<code>#{number[0..5]}</code>"
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion test/linkify_github_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ def pipeline
HTML::Pipeline::LinkifyGitHubFilter
]
end

def test_works_with_markdown_filter
result = pipeline.call <<-MARKDOWN.strip_heredoc
https://github.com/rails/rails/pull/21862
https://github.com/rails/rails/issues/21843
https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864
MARKDOWN

assert_equal "<p><a href=\"https://github.com/rails/rails/pull/21862\">rails/rails#21862</a><br>\n<a href=\"https://github.com/rails/rails/issues/21843\">rails/rails#21843</a><br>\n<a href=\"https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864\">rails/rails@`67597e`</a></p>",
assert_equal "<p><a href=\"https://github.com/rails/rails/pull/21862\">rails/rails#21862</a><br>\n<a href=\"https://github.com/rails/rails/issues/21843\">rails/rails#21843</a><br>\n<a href=\"https://github.com/rails/rails/commit/67597e1719ec6af7e22964603cc77aa5b085a864\">rails/rails@<code>67597e</code></a></p>",
result[:output].to_html
end

Expand All @@ -29,4 +30,13 @@ def test_works_when_markdown_already_linkified
assert_equal "<ul>\n<li><a href=\"https://github.com/rails/rails/pull/21862\">rails/rails#21862</a></li>\n</ul>",
result[:output].to_html
end

def test_preserve_tags_inside_link
result = pipeline.call <<-MARKDOWN.strip_heredoc
- [**rails/rails#21862**](https://github.com/rails/rails/pull/21862)
MARKDOWN

assert_equal "<ul>\n<li><a href=\"https://github.com/rails/rails/pull/21862\"><strong>rails/rails#21862</strong></a></li>\n</ul>",
result[:output].to_html
end
end
2 changes: 1 addition & 1 deletion test/linkify_github_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_linkify_github_commit_html_link
doc = Nokogiri::HTML::DocumentFragment.parse(body)

res = filter(doc)
assert_equal_html %Q(<a href="#{commit_url}">rails/rails@`67597e`</a>),
assert_equal_html %Q(<a href="#{commit_url}">rails/rails@<code>67597e</code></a>),
res.to_html
end
end