mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 13:03:39 +08:00
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
|
||||
|
||||
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?
|
||||
regexps << /(^|\s)?(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})($|\s)?/
|
||||
regexps << /(^|\s)?(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})($|\s)?/
|
||||
regexps << /(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
|
||||
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
|
||||
end
|
||||
|
||||
regexps.each do |regexp|
|
||||
markdown.scan(regexp) do |match|
|
||||
if matched_uploads(match[1]).present?
|
||||
raw_matches << [match[1], match[1], +"![](#{PLACEHOLDER})", $~.offset(0)[0]]
|
||||
indexes = Set.new
|
||||
|
||||
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
|
||||
|
@ -125,7 +165,7 @@ class InlineUploads
|
|||
|
||||
def self.match_md_reference(markdown)
|
||||
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(
|
||||
match[0],
|
||||
match[3],
|
||||
|
|
|
@ -124,25 +124,37 @@ RSpec.describe InlineUploads do
|
|||
|
||||
it "should correct markdown references" do
|
||||
md = <<~MD
|
||||
This is a [some reference] somethign
|
||||
This is a [some reference] something
|
||||
|
||||
[some reference]: #{Discourse.base_url}#{upload.url}
|
||||
|
||||
<img src="#{upload.url}">
|
||||
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}
|
||||
|
||||
![](#{upload.short_url})
|
||||
MD
|
||||
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
|
||||
#{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
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -174,7 +186,7 @@ RSpec.describe InlineUploads do
|
|||
![some image](#{upload.short_url} "some title")
|
||||
![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})
|
||||
MD
|
||||
|
@ -469,8 +481,8 @@ RSpec.describe InlineUploads do
|
|||
MD
|
||||
|
||||
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||
![](#{upload2.short_url}) ![](#{upload2.short_url})
|
||||
![](#{upload2.short_url})
|
||||
#{Discourse.base_url}#{upload2.short_path} #{Discourse.base_url}#{upload2.short_path}
|
||||
#{Discourse.base_url}#{upload2.short_path}
|
||||
|
||||
![some image](#{upload.short_url})
|
||||
![some image](#{upload2.short_url})
|
||||
|
|
Loading…
Reference in New Issue
Block a user