diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 7ac41d2442d..75174a8a826 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -306,7 +306,12 @@ JS def self.add_s3_cdn(doc) doc.css("img").each do |img| next unless img["src"] - img["src"] = img["src"].sub(Discourse.store.absolute_base_url, SiteSetting.s3_cdn_url) + if img["src"].include? Discourse.store.absolute_base_url + src = img["src"].sub(Discourse.store.absolute_base_url, SiteSetting.s3_cdn_url) + # absolute is // style so we may have added an extra https:// here + src = src.sub(/https?:h/, "h") + img["src"] = src + end end end diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 6172faf84c3..0ffd8e85fd9 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -360,8 +360,17 @@ HTML SiteSetting.s3_cdn_url = "https://awesome.cdn" # add extra img tag to ensure it does not blow up - raw = " + + + + +HTML + + cooked = <


+HTML expect(PrettyText.cook(raw)).to match_html(cooked) end