DEV: More improvements to `InlineUploads`.
* Convert inline links to short path ``` <link> <link> <link> ``` to ``` <short_path> <short_path> <short_path> ```
This commit is contained in:
parent
c05b617067
commit
ee142c2173
|
@ -56,18 +56,58 @@ class InlineUploads
|
||||||
db = RailsMultisite::ConnectionManagement.current_db
|
db = RailsMultisite::ConnectionManagement.current_db
|
||||||
|
|
||||||
regexps = [
|
regexps = [
|
||||||
/(^|\s)?(https?:\/\/[a-zA-Z0-9\.\/-]+\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})($|\s)/,
|
/(https?:\/\/[a-zA-Z0-9\.\/-]+\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,
|
||||||
]
|
]
|
||||||
|
|
||||||
if Discourse.store.external?
|
if Discourse.store.external?
|
||||||
regexps << /(^|\s)?(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})($|\s)?/
|
regexps << /(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
|
||||||
regexps << /(^|\s)?(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})($|\s)?/
|
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
|
||||||
end
|
end
|
||||||
|
|
||||||
regexps.each do |regexp|
|
regexps.each do |regexp|
|
||||||
markdown.scan(regexp) do |match|
|
indexes = Set.new
|
||||||
if matched_uploads(match[1]).present?
|
|
||||||
raw_matches << [match[1], match[1], +"![](#{PLACEHOLDER})", $~.offset(0)[0]]
|
markdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|
|
||||||
|
if match[1].present?
|
||||||
|
index = $~.offset(2)[0]
|
||||||
|
indexes << index
|
||||||
|
raw_matches << [match[1], match[1], +"![](#{PLACEHOLDER})", index]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
markdown.scan(/^#{regexp}(\s)/) do |match|
|
||||||
|
if match[0].present?
|
||||||
|
index = $~.offset(0)[0]
|
||||||
|
next if indexes.include?(index)
|
||||||
|
indexes << index
|
||||||
|
|
||||||
|
raw_matches << [
|
||||||
|
match[0],
|
||||||
|
match[0],
|
||||||
|
+"#{Discourse.base_url}#{PATH_PLACEHOLDER}",
|
||||||
|
$~.offset(0)[0]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
markdown.scan(/\[[^\[\]]*\]: #{regexp}/) do |match|
|
||||||
|
if match[0].present?
|
||||||
|
index = $~.offset(1)[0]
|
||||||
|
next if indexes.include?(index)
|
||||||
|
indexes << index
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
markdown.scan(/((\n|\s)+)#{regexp}/) do |match|
|
||||||
|
if matched_uploads(match[2]).present?
|
||||||
|
next if indexes.include?($~.offset(3)[0])
|
||||||
|
|
||||||
|
raw_matches << [
|
||||||
|
match[2],
|
||||||
|
match[2],
|
||||||
|
+"#{Discourse.base_url}#{PATH_PLACEHOLDER}",
|
||||||
|
$~.offset(0)[0]
|
||||||
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -125,7 +165,7 @@ class InlineUploads
|
||||||
|
|
||||||
def self.match_md_reference(markdown)
|
def self.match_md_reference(markdown)
|
||||||
markdown.scan(/(\[([^\]]+)\]:([ ]+)(\S+))/) do |match|
|
markdown.scan(/(\[([^\]]+)\]:([ ]+)(\S+))/) do |match|
|
||||||
if match[3] && matched_uploads(match[3]) && block_given?
|
if match[3] && matched_uploads(match[3]).present? && block_given?
|
||||||
yield(
|
yield(
|
||||||
match[0],
|
match[0],
|
||||||
match[3],
|
match[3],
|
||||||
|
|
|
@ -124,25 +124,37 @@ RSpec.describe InlineUploads do
|
||||||
|
|
||||||
it "should correct markdown references" do
|
it "should correct markdown references" do
|
||||||
md = <<~MD
|
md = <<~MD
|
||||||
This is a [some reference] somethign
|
This is a [some reference] something
|
||||||
|
|
||||||
[some reference]: #{Discourse.base_url}#{upload.url}
|
[some reference]: #{Discourse.base_url}#{upload.url}
|
||||||
|
|
||||||
|
<img src="#{upload.url}">
|
||||||
MD
|
MD
|
||||||
|
|
||||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
This is a [some reference] somethign
|
This is a [some reference] something
|
||||||
|
|
||||||
[some reference]: #{Discourse.base_url}#{upload.short_path}
|
[some reference]: #{Discourse.base_url}#{upload.short_path}
|
||||||
|
|
||||||
|
![](#{upload.short_url})
|
||||||
MD
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should correct raw image URLs to the short version" do
|
it "should correct raw image URLs to the short url and paths" do
|
||||||
md = <<~MD
|
md = <<~MD
|
||||||
#{Discourse.base_url}#{upload3.url} #{Discourse.base_url}#{upload3.url}
|
#{Discourse.base_url}#{upload.url}
|
||||||
|
|
||||||
|
#{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url}
|
||||||
|
|
||||||
|
#{Discourse.base_url}#{upload3.url}
|
||||||
MD
|
MD
|
||||||
|
|
||||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
![](#{upload3.short_url}) ![](#{upload3.short_url})
|
![](#{upload.short_url})
|
||||||
|
|
||||||
|
#{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path}
|
||||||
|
|
||||||
|
![](#{upload3.short_url})
|
||||||
MD
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -174,7 +186,7 @@ RSpec.describe InlineUploads do
|
||||||
![some image](#{upload.short_url} "some title")
|
![some image](#{upload.short_url} "some title")
|
||||||
![some image](#{upload2.short_url})![some image](#{upload3.short_url})
|
![some image](#{upload2.short_url})![some image](#{upload3.short_url})
|
||||||
|
|
||||||
![](#{upload3.short_url}) ![](#{upload3.short_url})
|
#{Discourse.base_url}#{upload3.short_path} #{Discourse.base_url}#{upload3.short_path}
|
||||||
|
|
||||||
![|5x4](#{upload.short_url})
|
![|5x4](#{upload.short_url})
|
||||||
MD
|
MD
|
||||||
|
@ -469,8 +481,8 @@ RSpec.describe InlineUploads do
|
||||||
MD
|
MD
|
||||||
|
|
||||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
![](#{upload2.short_url}) ![](#{upload2.short_url})
|
#{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path}
|
||||||
![](#{upload2.short_url})
|
#{Discourse.base_url}#{upload2.short_path}
|
||||||
|
|
||||||
![some image](#{upload.short_url})
|
![some image](#{upload.short_url})
|
||||||
![some image](#{upload2.short_url})
|
![some image](#{upload2.short_url})
|
||||||
|
|
Loading…
Reference in New Issue