2019-06-04 04:14:21 -04:00
|
|
|
# frozen_string_literal: true
|
2019-05-30 02:38:46 -04:00
|
|
|
RSpec.describe InlineUploads do
|
|
|
|
before do
|
2019-06-14 01:11:53 -04:00
|
|
|
set_cdn_url "https://awesome.com"
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.process' do
|
|
|
|
describe 'local uploads' do
|
|
|
|
fab!(:upload) { Fabricate(:upload) }
|
|
|
|
fab!(:upload2) { Fabricate(:upload) }
|
|
|
|
fab!(:upload3) { Fabricate(:upload) }
|
|
|
|
|
|
|
|
it "should not correct existing inline uploads" do
|
|
|
|
md = <<~MD
|
|
|
|
![test](#{upload.short_url})haha
|
|
|
|
[test]#{upload.short_url}
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
|
|
|
|
md = <<~MD
|
|
|
|
![test](#{upload.short_url})
|
|
|
|
[test|attachment](#{upload.short_url})
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not escape existing content" do
|
|
|
|
md = "1 > 2"
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not escape invalid HTML tags" do
|
|
|
|
md = "<x>.<y>"
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:31:00 -04:00
|
|
|
it "should work with invalid img tags" do
|
|
|
|
md = <<~MD
|
|
|
|
<img src="#{upload.url}">
|
|
|
|
|
|
|
|
This is an invalid `<img ...>` tag
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}">
|
2019-06-11 22:31:00 -04:00
|
|
|
|
|
|
|
This is an invalid `<img ...>` tag
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2021-05-14 09:52:40 -04:00
|
|
|
it "should work with invalid img tags" do
|
|
|
|
md = '<img data-id="<>">'
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
end
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
it "should not correct code blocks" do
|
2019-06-12 04:41:44 -04:00
|
|
|
md = "`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`"
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
|
|
|
|
md = " <a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>"
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(md)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not correct invalid links in quotes" do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
user = Fabricate(:user)
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-12 04:33:49 -04:00
|
|
|
md = <<~MD
|
2019-06-12 04:41:44 -04:00
|
|
|
[quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
|
|
|
|
<img src="#{upload.url}"
|
|
|
|
someothertext#{upload2.url}someothertext
|
|
|
|
|
|
|
|
<img src="#{upload.url}"
|
2019-06-12 04:33:49 -04:00
|
|
|
|
2019-06-12 04:41:44 -04:00
|
|
|
sometext#{upload2.url}sometext
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-12 04:41:44 -04:00
|
|
|
#{upload3.url}
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload3.url}
|
|
|
|
[/quote]
|
2019-06-12 04:33:49 -04:00
|
|
|
|
2019-06-12 04:41:44 -04:00
|
|
|
<img src="#{upload2.url}">
|
2019-06-12 04:33:49 -04:00
|
|
|
MD
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-12 04:41:44 -04:00
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
[quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
|
|
|
|
<img src="#{upload.url}"
|
|
|
|
someothertext#{upload2.url}someothertext
|
|
|
|
|
|
|
|
<img src="#{upload.url}"
|
|
|
|
|
|
|
|
sometext#{upload2.url}sometext
|
|
|
|
|
|
|
|
#{upload3.url}
|
|
|
|
|
|
|
|
![](#{upload3.short_url})
|
|
|
|
[/quote]
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload2.short_url}">
|
2019-06-12 04:41:44 -04:00
|
|
|
MD
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
|
2019-06-12 04:41:44 -04:00
|
|
|
it "should correct links in quotes" do
|
2019-06-03 03:41:26 -04:00
|
|
|
post = Fabricate(:post)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
md = <<~MD
|
|
|
|
[quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
|
|
|
|
some quote
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload3.url}
|
2019-06-06 03:50:16 -04:00
|
|
|
|
|
|
|
![](#{upload.url})
|
2019-06-03 03:41:26 -04:00
|
|
|
[/quote]
|
|
|
|
MD
|
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
[quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
|
|
|
|
some quote
|
|
|
|
|
|
|
|
![](#{upload3.short_url})
|
|
|
|
|
|
|
|
![](#{upload.short_url})
|
|
|
|
[/quote]
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should correct markdown linked images" do
|
|
|
|
md = <<~MD
|
|
|
|
[![](#{upload.url})](https://somelink.com)
|
|
|
|
|
|
|
|
[![some test](#{upload2.url})](https://somelink.com)
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
[![](#{upload.short_url})](https://somelink.com)
|
|
|
|
|
|
|
|
[![some test](#{upload2.short_url})](https://somelink.com)
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should correct markdown images with title" do
|
|
|
|
md = <<~MD
|
|
|
|
![](#{upload.url} "some alt")
|
|
|
|
![testing](#{upload2.url} 'some alt' )
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
![](#{upload.short_url} "some alt")
|
|
|
|
![testing](#{upload2.short_url} 'some alt' )
|
|
|
|
MD
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
it "should correct bbcode img URLs to the short version" do
|
|
|
|
md = <<~MD
|
2019-06-13 01:47:18 -04:00
|
|
|
[img]http://some.external.img[/img]
|
2019-05-30 02:38:46 -04:00
|
|
|
[img]#{upload.url}[/img]
|
2019-06-13 01:47:18 -04:00
|
|
|
<img src="#{upload3.url}">
|
2019-05-30 02:38:46 -04:00
|
|
|
|
|
|
|
[img]
|
|
|
|
#{upload2.url}
|
|
|
|
[/img]
|
2019-06-19 23:47:32 -04:00
|
|
|
|
|
|
|
[img]#{upload.url}[/img][img]#{upload2.url}[/img]
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2019-06-13 01:47:18 -04:00
|
|
|
[img]http://some.external.img[/img]
|
2019-05-30 02:38:46 -04:00
|
|
|
![](#{upload.short_url})
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload3.short_url}">
|
2019-05-30 02:38:46 -04:00
|
|
|
|
|
|
|
![](#{upload2.short_url})
|
2019-06-19 23:47:32 -04:00
|
|
|
|
|
|
|
![](#{upload.short_url})![](#{upload2.short_url})
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
it "should correct markdown references" do
|
|
|
|
md = <<~MD
|
2019-06-13 00:08:01 -04:00
|
|
|
[link3][3]
|
2019-06-06 03:50:16 -04:00
|
|
|
|
2019-06-13 00:08:01 -04:00
|
|
|
[3]: #{Discourse.base_url}#{upload2.url}
|
|
|
|
|
|
|
|
This is a [link1][1] test [link2][2] something
|
2019-06-06 23:46:52 -04:00
|
|
|
|
|
|
|
<img src="#{upload.url}">
|
2019-06-13 00:08:01 -04:00
|
|
|
|
|
|
|
[1]: #{Discourse.base_url}#{upload.url}
|
2019-06-14 00:48:31 -04:00
|
|
|
[2]: #{Discourse.base_url.sub("http://", "https://")}#{upload2.url}
|
2019-06-06 03:50:16 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2019-06-13 00:08:01 -04:00
|
|
|
[link3][3]
|
2019-06-06 03:50:16 -04:00
|
|
|
|
2019-06-13 00:08:01 -04:00
|
|
|
[3]: #{Discourse.base_url}#{upload2.short_path}
|
|
|
|
|
|
|
|
This is a [link1][1] test [link2][2] something
|
2019-06-06 23:46:52 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}">
|
2019-06-13 00:08:01 -04:00
|
|
|
|
|
|
|
[1]: #{Discourse.base_url}#{upload.short_path}
|
|
|
|
[2]: #{Discourse.base_url}#{upload2.short_path}
|
2019-06-06 03:50:16 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-09-12 12:55:15 -04:00
|
|
|
it "should correct html and markdown uppercase references" do
|
|
|
|
md = <<~MD
|
|
|
|
[IMG]#{upload.url}[/IMG]
|
|
|
|
<IMG src="#{upload2.url}" />
|
|
|
|
<A class="attachment" href="#{upload3.url}">Text</A>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
![](#{upload.short_url})
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload2.short_url}">
|
2019-09-12 12:55:15 -04:00
|
|
|
[Text|attachment](#{upload3.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-09-13 12:21:19 -04:00
|
|
|
it "should correct image URLs with v parameters" do
|
|
|
|
md = <<~MD
|
|
|
|
<img src="#{upload.url}?v=1">
|
|
|
|
|
|
|
|
<img src="#{Discourse.base_url}#{upload.url}?v=2">
|
|
|
|
|
|
|
|
<img src="#{GlobalSetting.cdn_url}#{upload.url}?v=3">
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload.url}?v=45
|
|
|
|
|
|
|
|
#{GlobalSetting.cdn_url}#{upload.url}?v=999
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}">
|
2019-09-13 12:21:19 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}">
|
2019-09-13 12:21:19 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}">
|
2019-09-13 12:21:19 -04:00
|
|
|
|
|
|
|
![](#{upload.short_url})
|
|
|
|
|
|
|
|
![](#{upload.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-09-05 01:31:49 -04:00
|
|
|
context "subfolder" do
|
|
|
|
before do
|
2019-11-15 00:48:24 -05:00
|
|
|
set_subfolder "/community"
|
2019-09-05 01:31:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should correct subfolder images" do
|
|
|
|
md = <<~MD
|
|
|
|
<img src="/community#{upload.url}">
|
2019-09-10 22:02:42 -04:00
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload.url}
|
2019-09-05 01:31:49 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}">
|
2019-09-10 22:02:42 -04:00
|
|
|
|
|
|
|
![](#{upload.short_url})
|
2019-09-05 01:31:49 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-06 23:46:52 -04:00
|
|
|
it "should correct raw image URLs to the short url and paths" do
|
2019-06-03 03:41:26 -04:00
|
|
|
md = <<~MD
|
2019-06-06 23:46:52 -04:00
|
|
|
#{Discourse.base_url}#{upload.url}
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url}
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload3.url}
|
2019-06-14 01:11:53 -04:00
|
|
|
|
|
|
|
#{GlobalSetting.cdn_url}#{upload3.url}
|
2019-06-03 03:41:26 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2019-06-06 23:46:52 -04:00
|
|
|
![](#{upload.short_url})
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path}
|
|
|
|
|
2019-06-14 01:11:53 -04:00
|
|
|
![](#{upload3.short_url})
|
|
|
|
|
2019-06-06 23:46:52 -04:00
|
|
|
![](#{upload3.short_url})
|
2019-06-03 03:41:26 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-07-16 23:45:09 -04:00
|
|
|
it "should correct non image URLs to the short url" do
|
2019-07-16 08:35:17 -04:00
|
|
|
SiteSetting.authorized_extensions = "mp4"
|
2019-07-16 09:51:16 -04:00
|
|
|
upload = Fabricate(:video_upload)
|
2019-07-17 01:43:50 -04:00
|
|
|
upload2 = Fabricate(:video_upload)
|
2019-07-16 08:35:17 -04:00
|
|
|
|
|
|
|
md = <<~MD
|
2019-07-17 01:43:50 -04:00
|
|
|
#{Discourse.base_url}#{upload.url}
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload.url} #{Discourse.base_url}#{upload2.url}
|
2019-07-17 01:46:35 -04:00
|
|
|
|
|
|
|
#{GlobalSetting.cdn_url}#{upload2.url}
|
2019-07-16 08:35:17 -04:00
|
|
|
MD
|
|
|
|
|
2019-07-16 23:45:09 -04:00
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
#{Discourse.base_url}#{upload.short_path}
|
2019-07-17 01:43:50 -04:00
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload.short_path} #{Discourse.base_url}#{upload2.short_path}
|
2019-07-17 01:46:35 -04:00
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload2.short_path}
|
2019-07-16 23:45:09 -04:00
|
|
|
MD
|
2019-07-16 08:35:17 -04:00
|
|
|
end
|
|
|
|
|
2019-06-12 23:18:49 -04:00
|
|
|
it "should correct img tags with uppercase upload extension" do
|
|
|
|
md = <<~MD
|
|
|
|
test<img src="#{upload.url.sub(".png", ".PNG")}">
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2022-03-29 05:55:10 -04:00
|
|
|
test<img src="#{upload.short_url}">
|
2019-06-12 23:18:49 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-06-14 01:47:44 -04:00
|
|
|
it "should correct image URLs that follows an image md" do
|
|
|
|
md = <<~MD
|
|
|
|
![image|690x290](#{upload.short_url})#{Discourse.base_url}#{upload2.url}
|
|
|
|
|
|
|
|
<#{Discourse.base_url}#{upload2.url}>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
![image|690x290](#{upload.short_url})#{Discourse.base_url}#{upload2.short_path}
|
|
|
|
|
|
|
|
<#{Discourse.base_url}#{upload2.short_path}>
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
it "should correct image URLs to the short version" do
|
|
|
|
md = <<~MD
|
|
|
|
![image|690x290](#{upload.short_url})
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
![IMAge|690x190,60%](#{upload.short_url})
|
|
|
|
|
|
|
|
![image](#{upload2.url})
|
|
|
|
![image|100x100](#{upload3.url})
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
<img src="#{Discourse.base_url}#{upload.url}" alt="some image" title="some title" />
|
2019-06-03 03:41:26 -04:00
|
|
|
<img src="#{Discourse.base_url}#{upload2.url}" alt="some image"><img src="#{Discourse.base_url}#{upload3.url}" alt="some image">
|
|
|
|
|
|
|
|
#{Discourse.base_url}#{upload3.url} #{Discourse.base_url}#{upload3.url}
|
2019-05-30 02:38:46 -04:00
|
|
|
|
|
|
|
<img src="#{upload.url}" width="5" height="4">
|
2019-07-05 14:34:11 -04:00
|
|
|
<img src="#{upload.url}" width="5px" height="auto">
|
2019-09-12 15:25:14 -04:00
|
|
|
|
|
|
|
`<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">
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
![image|690x290](#{upload.short_url})
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
![IMAge|690x190,60%](#{upload.short_url})
|
|
|
|
|
|
|
|
![image](#{upload2.short_url})
|
|
|
|
![image|100x100](#{upload3.short_url})
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}" alt="some image" title="some title">
|
|
|
|
<img src="#{upload2.short_url}" alt="some image"><img src="#{upload3.short_url}" alt="some image">
|
2019-06-03 03:41:26 -04:00
|
|
|
|
2019-06-06 23:46:52 -04:00
|
|
|
#{Discourse.base_url}#{upload3.short_path} #{Discourse.base_url}#{upload3.short_path}
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}" width="5" height="4">
|
|
|
|
<img src="#{upload.short_url}" width="5px" height="auto">
|
2019-09-12 15:25:14 -04:00
|
|
|
|
|
|
|
`<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">
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
it "should not be affected by an emoji" do
|
|
|
|
CustomEmoji.create!(name: 'test', upload: upload3)
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
md = <<~MD
|
|
|
|
:test:
|
|
|
|
|
|
|
|
![image|690x290](#{upload.url})
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
:test:
|
|
|
|
|
|
|
|
![image|690x290](#{upload.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
it "should correctly update images sources within anchor tags with indentation" do
|
2019-05-30 02:38:46 -04:00
|
|
|
md = <<~MD
|
2019-06-21 00:32:02 -04:00
|
|
|
<h1></h1>
|
|
|
|
<a href="http://somelink.com">
|
|
|
|
<img src="#{upload2.url}" alt="test" width="500" height="500">
|
|
|
|
</a>
|
2019-06-03 03:41:26 -04:00
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
<a href="http://somelink.com">
|
|
|
|
<img src="#{upload2.url}" alt="test" width="500" height="500">
|
|
|
|
</a>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2019-06-20 21:55:39 -04:00
|
|
|
<h1></h1>
|
2019-06-21 00:32:02 -04:00
|
|
|
<a href="http://somelink.com">
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload2.short_url}" alt="test" width="500" height="500">
|
2019-06-21 00:32:02 -04:00
|
|
|
</a>
|
|
|
|
|
2019-06-20 21:55:39 -04:00
|
|
|
<a href="http://somelink.com">
|
|
|
|
<img src="#{upload2.url}" alt="test" width="500" height="500">
|
|
|
|
</a>
|
2019-06-21 00:32:02 -04:00
|
|
|
MD
|
2019-06-21 02:07:06 -04:00
|
|
|
|
|
|
|
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>"
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(InlineUploads.process(md)).to eq("<h1></h1>\r\n<a href=\"http://somelink.com\">\r\n <img src=\"#{upload.short_url}\" alt=\"test\" width=\"500\" height=\"500\">\r\n</a>")
|
2019-06-21 00:32:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should correctly update image sources within anchor or paragraph tags" do
|
|
|
|
md = <<~MD
|
|
|
|
<a href="http://somelink.com">
|
|
|
|
<img src="#{upload.url}" alt="test" width="500" height="500">
|
|
|
|
</a>
|
2019-06-20 21:55:39 -04:00
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
<p>
|
|
|
|
<img src="#{upload2.url}" alt="test">
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<a href="http://somelink.com"><img src="#{upload3.url}" alt="test" width="500" height="500"></a>
|
|
|
|
|
|
|
|
<a href="http://somelink.com"> <img src="#{upload.url}" alt="test" width="500" height="500"> </a>
|
|
|
|
|
|
|
|
<a href="http://somelink.com">
|
|
|
|
|
|
|
|
|
|
|
|
<img src="#{upload.url}" alt="test" width="500" height="500">
|
|
|
|
|
|
|
|
</a>
|
|
|
|
|
|
|
|
<p>Test <img src="#{upload2.url}" alt="test" width="500" height="500"></p>
|
|
|
|
|
|
|
|
<hr/>
|
|
|
|
<img src="#{upload2.url}" alt="test" width="500" height="500">
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
<a href="http://somelink.com">
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}" alt="test" width="500" height="500">
|
2019-06-03 03:41:26 -04:00
|
|
|
</a>
|
|
|
|
|
|
|
|
<p>
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload2.short_url}" alt="test">
|
2019-06-03 03:41:26 -04:00
|
|
|
</p>
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<a href="http://somelink.com"><img src="#{upload3.short_url}" alt="test" width="500" height="500"></a>
|
2019-06-03 03:41:26 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<a href="http://somelink.com"> <img src="#{upload.short_url}" alt="test" width="500" height="500"> </a>
|
2019-06-03 03:41:26 -04:00
|
|
|
|
|
|
|
<a href="http://somelink.com">
|
|
|
|
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}" alt="test" width="500" height="500">
|
2019-06-03 03:41:26 -04:00
|
|
|
|
|
|
|
</a>
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<p>Test <img src="#{upload2.short_url}" alt="test" width="500" height="500"></p>
|
2019-06-03 03:41:26 -04:00
|
|
|
|
|
|
|
<hr/>
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload2.short_url}" alt="test" width="500" height="500">
|
2019-06-03 03:41:26 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be affected by fake HTML tags" do
|
|
|
|
md = <<~MD
|
|
|
|
```
|
|
|
|
This is some <img src=" and <a href="
|
|
|
|
```
|
|
|
|
|
|
|
|
<img src="#{upload.url}" alt="test">
|
|
|
|
|
2019-06-11 22:44:25 -04:00
|
|
|
<img src="#{upload2.url}" alt="test" height="150<img">
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
> some quote
|
2019-05-30 02:38:46 -04:00
|
|
|
|
|
|
|
<a class="attachment" href="#{upload2.url}">test2</a>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2019-06-03 03:41:26 -04:00
|
|
|
```
|
|
|
|
This is some <img src=" and <a href="
|
|
|
|
```
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}" alt="test">
|
2019-06-03 03:41:26 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload2.short_url}" alt="test" height="150<img">
|
2019-06-11 22:44:25 -04:00
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
> some quote
|
|
|
|
|
|
|
|
[test2|attachment](#{upload2.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be affected by an external or invalid links" do
|
|
|
|
md = <<~MD
|
|
|
|
<a id="test">invalid</a>
|
|
|
|
|
|
|
|
[test]("https://this.is.some.external.link")
|
|
|
|
|
|
|
|
<a href="https://some.external.com/link">test</a>
|
|
|
|
|
|
|
|
<a class="attachment" href="#{upload2.url}">test2</a>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
<a id="test">invalid</a>
|
|
|
|
|
|
|
|
[test]("https://this.is.some.external.link")
|
|
|
|
|
|
|
|
<a href="https://some.external.com/link">test</a>
|
|
|
|
|
|
|
|
[test2|attachment](#{upload2.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should correct attachment URLS to the short version when raw contains inline image" do
|
|
|
|
md = <<~MD
|
|
|
|
![image](#{upload.short_url}) ![image](#{upload.short_url})
|
|
|
|
|
|
|
|
[some complicated.doc %50](#{upload3.url})
|
|
|
|
|
|
|
|
<a class="attachment" href="#{upload2.url}">test2</a>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
![image](#{upload.short_url}) ![image](#{upload.short_url})
|
|
|
|
|
|
|
|
[some complicated.doc %50](#{upload3.short_url})
|
2019-05-30 02:38:46 -04:00
|
|
|
|
|
|
|
[test2|attachment](#{upload2.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should correct attachment URLs to the short version" do
|
|
|
|
md = <<~MD
|
|
|
|
<a class="attachment" href="#{upload.url}">
|
|
|
|
this
|
|
|
|
is
|
|
|
|
some
|
|
|
|
attachment
|
|
|
|
|
|
|
|
</a>
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
- <a class="attachment" href="#{upload.url}">test2</a>
|
2019-05-30 02:38:46 -04:00
|
|
|
- <a class="attachment" href="#{upload2.url}">test2</a>
|
2019-06-03 03:41:26 -04:00
|
|
|
- <a class="attachment" href="#{upload3.url}">test2</a>
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
<a class="test attachment" href="#{upload.url}">test3</a>
|
|
|
|
<a class="test attachment" href="#{upload2.url}">test3</a><a class="test attachment" href="#{upload3.url}">test3</a>
|
2019-06-18 23:14:46 -04:00
|
|
|
|
|
|
|
<a class="test attachment" href="#{upload3.url}">This is some _test_ here</a>
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
[this is some attachment|attachment](#{upload.short_url})
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
- [test2|attachment](#{upload.short_url})
|
2019-05-30 02:38:46 -04:00
|
|
|
- [test2|attachment](#{upload2.short_url})
|
2019-06-03 03:41:26 -04:00
|
|
|
- [test2|attachment](#{upload3.short_url})
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
[test3|attachment](#{upload.short_url})
|
|
|
|
[test3|attachment](#{upload2.short_url})[test3|attachment](#{upload3.short_url})
|
2019-06-18 23:14:46 -04:00
|
|
|
|
|
|
|
[This is some _test_ here|attachment](#{upload3.short_url})
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correct full upload url to the shorter version' do
|
|
|
|
md = <<~MD
|
|
|
|
Some random text
|
|
|
|
|
|
|
|
![test](#{upload.short_url})
|
|
|
|
[test|attachment](#{upload.short_url})
|
|
|
|
|
|
|
|
<a class="test attachment" href="#{upload.url}">
|
|
|
|
test
|
|
|
|
</a>
|
|
|
|
|
|
|
|
`<a class="attachment" href="#{upload2.url}">In Code Block</a>`
|
|
|
|
|
|
|
|
<a class="attachment" href="#{upload3.url}">In Code Block</a>
|
|
|
|
|
|
|
|
<a href="#{upload.url}">newtest</a>
|
|
|
|
<a href="#{Discourse.base_url_no_prefix}#{upload.url}">newtest</a>
|
|
|
|
|
|
|
|
<a href="https://somerandomesite.com#{upload.url}">test</a>
|
|
|
|
<a class="attachment" href="https://somerandom.com/url">test</a>
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
|
|
|
Some random text
|
|
|
|
|
|
|
|
![test](#{upload.short_url})
|
|
|
|
[test|attachment](#{upload.short_url})
|
|
|
|
|
|
|
|
[test|attachment](#{upload.short_url})
|
|
|
|
|
|
|
|
`<a class="attachment" href="#{upload2.url}">In Code Block</a>`
|
|
|
|
|
|
|
|
<a class="attachment" href="#{upload3.url}">In Code Block</a>
|
|
|
|
|
|
|
|
[newtest](#{upload.short_url})
|
|
|
|
[newtest](#{upload.short_url})
|
|
|
|
|
|
|
|
<a href="https://somerandomesite.com#{upload.url}">test</a>
|
|
|
|
<a class="attachment" href="https://somerandom.com/url">test</a>
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a block that yields when link does not match an upload in the db' do
|
|
|
|
url = "#{Discourse.base_url}#{upload.url}"
|
|
|
|
|
|
|
|
md = <<~MD
|
|
|
|
<img src="#{url}" alt="some image">
|
|
|
|
<img src="#{upload2.url}" alt="some image">
|
|
|
|
MD
|
|
|
|
|
|
|
|
upload.destroy!
|
|
|
|
|
|
|
|
InlineUploads.process(md, on_missing: lambda { |link|
|
|
|
|
expect(link).to eq(url)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "s3 uploads" do
|
|
|
|
let(:upload) { Fabricate(:upload_s3) }
|
2019-06-03 03:41:26 -04:00
|
|
|
let(:upload2) { Fabricate(:upload_s3) }
|
2019-07-29 06:57:56 -04:00
|
|
|
let(:upload3) { Fabricate(:upload) }
|
2019-05-30 02:38:46 -04:00
|
|
|
|
|
|
|
before do
|
2019-07-29 06:57:56 -04:00
|
|
|
upload3
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2019-05-30 02:38:46 -04:00
|
|
|
SiteSetting.s3_cdn_url = "https://s3.cdn.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should correct image URLs to the short version" do
|
|
|
|
md = <<~MD
|
2019-06-12 04:41:44 -04:00
|
|
|
#{upload.url}
|
2019-05-30 02:38:46 -04:00
|
|
|
<img src="#{upload.url}" alt="some image">
|
2019-06-12 23:18:49 -04:00
|
|
|
test<img src="#{upload2.url}" alt="some image">test
|
2019-06-03 03:41:26 -04:00
|
|
|
<img src="#{URI.join(SiteSetting.s3_cdn_url, URI.parse(upload2.url).path).to_s}" alt="some image">
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2019-06-12 04:41:44 -04:00
|
|
|
![](#{upload.short_url})
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="#{upload.short_url}" alt="some image">
|
|
|
|
test<img src="#{upload2.short_url}" alt="some image">test
|
|
|
|
<img src="#{upload2.short_url}" alt="some image">
|
2019-05-30 02:38:46 -04:00
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
it "should correct markdown references" do
|
|
|
|
md = <<~MD
|
2021-05-20 21:43:47 -04:00
|
|
|
This is a [some reference] something
|
2019-06-06 03:50:16 -04:00
|
|
|
|
|
|
|
[some reference]: https:#{upload.url}
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
2021-05-20 21:43:47 -04:00
|
|
|
This is a [some reference] something
|
2019-06-06 03:50:16 -04:00
|
|
|
|
|
|
|
[some reference]: #{Discourse.base_url}#{upload.short_path}
|
|
|
|
MD
|
|
|
|
end
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-20 21:47:48 -04:00
|
|
|
|
|
|
|
describe ".match_md_inline_img" do
|
|
|
|
it "matches URLs with various characters" do
|
|
|
|
md = <<~MD
|
|
|
|
![test](https://some-site.com/a_test?q=1&b=hello%20there)
|
|
|
|
MD
|
|
|
|
|
|
|
|
url = nil
|
|
|
|
InlineUploads.match_md_inline_img(md, external_src: true) { |_match, src| url = src }
|
|
|
|
|
|
|
|
expect(url).to eq("https://some-site.com/a_test?q=1&b=hello%20there")
|
|
|
|
end
|
|
|
|
end
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|