FIX: support <img> in code blocks when inlining uploads
Simpler code is better :) Also added moar specs to ensure <img> tag inside code blocks are properly ignored.
This commit is contained in:
parent
8f601d5025
commit
aa511c5b59
|
@ -34,7 +34,7 @@ class InlineUploads
|
||||||
if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value))
|
if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value))
|
||||||
link_occurences << { link: actual_link, is_valid: true }
|
link_occurences << { link: actual_link, is_valid: true }
|
||||||
elsif node.name != "p"
|
elsif node.name != "p"
|
||||||
link_occurences << { link: actual_link, is_valid: false }
|
link_occurences << { link: seen_link, is_valid: false }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -219,8 +219,8 @@ class InlineUploads
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.match_img(markdown, external_src: false)
|
def self.match_img(markdown, external_src: false)
|
||||||
markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?([\r\n]*)(([ ]*)<img ([^>\n]+)>([ ]*))([\r\n]*)/i) do |match|
|
markdown.scan(/(<(?!img)[^<>]+\/?>)?(\s*)(<img [^>\n]+>)/i) do |match|
|
||||||
node = Nokogiri::HTML::fragment(match[3].strip).children[0]
|
node = Nokogiri::HTML::fragment(match[2].strip).children[0]
|
||||||
src = node.attributes["src"]&.value
|
src = node.attributes["src"]&.value
|
||||||
|
|
||||||
if src && (matched_uploads(src).present? || external_src)
|
if src && (matched_uploads(src).present? || external_src)
|
||||||
|
@ -229,36 +229,10 @@ class InlineUploads
|
||||||
height = node.attributes["height"]&.value.to_i
|
height = node.attributes["height"]&.value.to_i
|
||||||
title = node.attributes["title"]&.value
|
title = node.attributes["title"]&.value
|
||||||
text = "#{text}|#{width}x#{height}" if width > 0 && height > 0
|
text = "#{text}|#{width}x#{height}" if width > 0 && height > 0
|
||||||
after_html_tag = match[0].present?
|
spaces_before = match[1].present? ? match[1][/ +$/].size : 0
|
||||||
|
replacement = +"#{" " * spaces_before}![#{text}](#{PLACEHOLDER}#{title.present? ? " \"#{title}\"" : ""})"
|
||||||
|
|
||||||
spaces_before =
|
yield(match[2], src, replacement, $~.offset(0)[0]) if block_given?
|
||||||
if after_html_tag && !match[0].end_with?("/>")
|
|
||||||
(match[4].length > 0 ? match[4] : " ")
|
|
||||||
else
|
|
||||||
""
|
|
||||||
end
|
|
||||||
|
|
||||||
replacement = +"#{spaces_before}![#{text}](#{PLACEHOLDER}#{title.present? ? " \"#{title}\"" : ""})"
|
|
||||||
|
|
||||||
if after_html_tag && (num_newlines = match[2].length) <= 1
|
|
||||||
replacement.prepend("\n" * (num_newlines == 0 ? 2 : 1))
|
|
||||||
end
|
|
||||||
|
|
||||||
if after_html_tag && !match[0].end_with?("/>") && (num_newlines = match[7].length) <= 1
|
|
||||||
replacement += ("\n" * (num_newlines == 0 ? 2 : 1))
|
|
||||||
end
|
|
||||||
|
|
||||||
match[3].strip! if !after_html_tag
|
|
||||||
|
|
||||||
if (match[1].nil? || match[1].length < 4)
|
|
||||||
if (match[4].nil? || match[4].length < 4)
|
|
||||||
yield(match[3], src, replacement, $~.offset(0)[0]) if block_given?
|
|
||||||
else
|
|
||||||
yield(match[3], src, match[3].sub(src, PATH_PLACEHOLDER), $~.offset(0)[0]) if block_given?
|
|
||||||
end
|
|
||||||
else
|
|
||||||
yield(match[3], src, match[3].sub(src, PATH_PLACEHOLDER), $~.offset(0)[0]) if block_given?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,7 +77,7 @@ describe Jobs::PullHotlinkedImages do
|
||||||
expect(post.reload.raw).to eq(<<~RAW.chomp)
|
expect(post.reload.raw).to eq(<<~RAW.chomp)
|
||||||
<h1></h1>
|
<h1></h1>
|
||||||
<a href="https://somelink.com">
|
<a href="https://somelink.com">
|
||||||
<img alt="somelink" src="#{upload.short_path}" />
|
![somelink](#{upload.short_url})
|
||||||
</a>
|
</a>
|
||||||
RAW
|
RAW
|
||||||
end
|
end
|
||||||
|
|
|
@ -329,6 +329,14 @@ RSpec.describe InlineUploads do
|
||||||
|
|
||||||
<img src="#{upload.url}" width="5" height="4">
|
<img src="#{upload.url}" width="5" height="4">
|
||||||
<img src="#{upload.url}" width="5px" height="auto">
|
<img src="#{upload.url}" width="5px" height="auto">
|
||||||
|
|
||||||
|
`<img src="#{upload.url}" alt="image inside code quotes">`
|
||||||
|
|
||||||
|
```
|
||||||
|
<img src="#{upload.url}" alt="image inside code fences">
|
||||||
|
```
|
||||||
|
|
||||||
|
<img src="#{upload.url}" alt="image inside code block">
|
||||||
MD
|
MD
|
||||||
|
|
||||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
|
@ -346,6 +354,14 @@ RSpec.describe InlineUploads do
|
||||||
|
|
||||||
![|5x4](#{upload.short_url})
|
![|5x4](#{upload.short_url})
|
||||||
![](#{upload.short_url})
|
![](#{upload.short_url})
|
||||||
|
|
||||||
|
`<img src="#{upload.url}" alt="image inside code quotes">`
|
||||||
|
|
||||||
|
```
|
||||||
|
<img src="#{upload.url}" alt="image inside code fences">
|
||||||
|
```
|
||||||
|
|
||||||
|
<img src="#{upload.url}" alt="image inside code block">
|
||||||
MD
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -381,7 +397,7 @@ RSpec.describe InlineUploads do
|
||||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
<h1></h1>
|
<h1></h1>
|
||||||
<a href="http://somelink.com">
|
<a href="http://somelink.com">
|
||||||
<img src="#{upload2.short_path}" alt="test" width="500" height="500">
|
![test|500x500](#{upload2.short_url})
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<a href="http://somelink.com">
|
<a href="http://somelink.com">
|
||||||
|
@ -391,7 +407,7 @@ RSpec.describe InlineUploads do
|
||||||
|
|
||||||
md = "<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n <img src=\"#{upload.url}\" alt=\"test\" width=\"500\" height=\"500\">\r\n</a>"
|
md = "<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n <img src=\"#{upload.url}\" alt=\"test\" width=\"500\" height=\"500\">\r\n</a>"
|
||||||
|
|
||||||
expect(InlineUploads.process(md)).to eq("<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n <img src=\"#{upload.short_path}\" alt=\"test\" width=\"500\" height=\"500\">\r\n</a>")
|
expect(InlineUploads.process(md)).to eq("<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n ![test|500x500](#{upload.short_url})\r\n</a>")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should correctly update image sources within anchor or paragraph tags" do
|
it "should correctly update image sources within anchor or paragraph tags" do
|
||||||
|
@ -423,40 +439,27 @@ RSpec.describe InlineUploads do
|
||||||
|
|
||||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
<a href="http://somelink.com">
|
<a href="http://somelink.com">
|
||||||
|
|
||||||
![test|500x500](#{upload.short_url})
|
![test|500x500](#{upload.short_url})
|
||||||
|
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
![test](#{upload2.short_url})
|
![test](#{upload2.short_url})
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<a href="http://somelink.com">
|
<a href="http://somelink.com">![test|500x500](#{upload3.short_url})</a>
|
||||||
|
|
||||||
![test|500x500](#{upload3.short_url})
|
<a href="http://somelink.com"> ![test|500x500](#{upload.short_url}) </a>
|
||||||
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<a href="http://somelink.com">
|
|
||||||
|
|
||||||
![test|500x500](#{upload.short_url})
|
|
||||||
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<a href="http://somelink.com">
|
<a href="http://somelink.com">
|
||||||
|
|
||||||
|
|
||||||
![test|500x500](#{upload.short_url})
|
![test|500x500](#{upload.short_url})
|
||||||
|
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<p>Test ![test|500x500](#{upload2.short_url})</p>
|
<p>Test ![test|500x500](#{upload2.short_url})</p>
|
||||||
|
|
||||||
<hr/>
|
<hr/>
|
||||||
|
|
||||||
![test|500x500](#{upload2.short_url})
|
![test|500x500](#{upload2.short_url})
|
||||||
MD
|
MD
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue