Skip to content

Commit

Permalink
Merge pull request #254 from jonathanhefner/unlink-unintentional-auto…
Browse files Browse the repository at this point in the history
…links

Unlink unintentional autolinked code ref links
  • Loading branch information
jonathanhefner authored Jul 31, 2023
2 parents 5f6a101 + 4837fea commit ef39d46
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/sdoc/postprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def process(rendered)

rebase_urls!(document)
version_rails_guides_urls!(document)
unlink_unintentional_ref_links!(document)
highlight_code_blocks!(document)

document.to_s
Expand Down Expand Up @@ -69,6 +70,14 @@ def version_url(url, version)
uri.to_s
end

def unlink_unintentional_ref_links!(document)
document.css(".description a[href^='classes/'] > code:only-child > text()").each do |text_node|
if text_node.inner_text.match?(/\A[A-Z](?:[A-Z]+|[a-z]+)\z/)
text_node.parent.parent.replace(text_node)
end
end
end

def highlight_code_blocks!(document)
document.css(".description pre > code, .sourcecode pre > code").each do |element|
code = element.inner_text
Expand Down
30 changes: 30 additions & 0 deletions spec/postprocessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@
end
end

it "unlinks unintentional autolinked code ref links in descriptions" do
rendered = <<~HTML
<base href="../" data-current-path="classes/Foo.html">
<div class="description">
<a href="Rails.html"><code>Rails</code></a>
<a href="ERB.html"><code>ERB</code></a>
<a href="Rails.html"><code>::Rails</code></a>
<a href="FooBar.html"><code>FooBar</code></a>
</div>
<a href="Nav.html"><code>Nav</code></a>
HTML

expected = <<~HTML
<div class="description">
Rails
ERB
<a href="classes/Rails.html"><code>::Rails</code></a>
<a href="classes/FooBar.html"><code>FooBar</code></a>
</div>
<a href="classes/Nav.html"><code>Nav</code></a>
HTML

_(SDoc::Postprocessor.process(rendered)).must_include expected
end

it "highlights code blocks" do
rendered = <<~HTML
<div class="description">
Expand Down

0 comments on commit ef39d46

Please sign in to comment.