Allow Giphy links to be used and not from Cloudinary (#927)
* Test for giphy images * Allow giphy images to be used as-is
This commit is contained in:
parent
63acc9b72a
commit
0dab93bdad
2 changed files with 56 additions and 8 deletions
|
|
@ -59,12 +59,18 @@ class MarkdownParser
|
|||
end
|
||||
|
||||
def prefix_all_images(html, width = 880)
|
||||
# wrap with Cloudinary or allow if from giphy or githubusercontent.com
|
||||
doc = Nokogiri::HTML.fragment(html)
|
||||
doc.css("img").each do |img|
|
||||
if img.attr("src") && check_image_rehost_whitelist(img.attr("src"))
|
||||
src = img.attr("src")
|
||||
img["src"] = img_of_size(src, width)
|
||||
end
|
||||
src = img.attr("src")
|
||||
next unless src
|
||||
# allow image to render as-is
|
||||
next if whitelisted_image_host?(src)
|
||||
img["src"] = if giphy_img?(src)
|
||||
src.gsub("https://media.", "https://i.")
|
||||
else
|
||||
img_of_size(src, width)
|
||||
end
|
||||
end
|
||||
doc.to_html
|
||||
end
|
||||
|
|
@ -99,9 +105,19 @@ class MarkdownParser
|
|||
end
|
||||
end
|
||||
|
||||
def check_image_rehost_whitelist(src)
|
||||
def whitelisted_image_host?(src)
|
||||
# GitHub camo image won't parse but should be safe to host direct
|
||||
!src.start_with?("https://camo.githubusercontent.com/")
|
||||
src.start_with?("https://camo.githubusercontent.com/")
|
||||
end
|
||||
|
||||
def giphy_img?(source)
|
||||
uri = URI.parse(source)
|
||||
return false if uri.scheme != "https"
|
||||
return false if uri.userinfo || uri.fragment || uri.query
|
||||
return false if uri.host != "media.giphy.com" && uri.host != "i.giphy.com"
|
||||
return false if uri.port != 443 # I think it has to be this if its https?
|
||||
|
||||
uri.path.ends_with?(".gif")
|
||||
end
|
||||
|
||||
def remove_nested_linebreak_in_list(html)
|
||||
|
|
|
|||
|
|
@ -77,10 +77,42 @@ RSpec.describe MarkdownParser do
|
|||
end
|
||||
end
|
||||
|
||||
context "when using gifs from Giphy as images" do
|
||||
let(:giphy_markdown_texts) do
|
||||
%w(
|
||||

|
||||

|
||||

|
||||
)
|
||||
end
|
||||
|
||||
it "does not wrap giphy images with Cloudinary" do
|
||||
giphy_markdown_texts.each do |body_markdown|
|
||||
html = Nokogiri::HTML(generate_and_parse_markdown(body_markdown))
|
||||
img_src = html.search("img")[0]["src"]
|
||||
expect(img_src).not_to include("https://res.cloudinary.com")
|
||||
end
|
||||
end
|
||||
|
||||
it "uses the raw gif from i.giphy.com" do
|
||||
giphy_markdown_texts.each do |body_markdown|
|
||||
html = Nokogiri::HTML(generate_and_parse_markdown(body_markdown))
|
||||
img_src = html.search("img")[0]["src"]
|
||||
expect(img_src).to start_with("https://i.giphy.com")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when an image is used" do
|
||||
let(:markdown_with_img) { "" }
|
||||
|
||||
it "wraps image in link" do
|
||||
inline_code = ""
|
||||
expect(generate_and_parse_markdown(inline_code)).to include("<a")
|
||||
expect(generate_and_parse_markdown(markdown_with_img)).to include("<a")
|
||||
end
|
||||
|
||||
it "wraps the image with Cloudinary" do
|
||||
expect(generate_and_parse_markdown(markdown_with_img)).
|
||||
to include("https://res.cloudinary.com")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue