Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camofy html image sources #467

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
module MarkdownHelper
include CamoHelper

def camofy(markdown)
return unless markdown
def camofy(text)
return unless text

markdown.gsub(markdown_img_regex) do
text = sub_markdown(text)
sub_html(text)
end

def sub_markdown(text)
text.gsub(markdown_img_regex) do
"![#{Regexp.last_match(1)}](#{camo(Regexp.last_match(2))}#{Regexp.last_match(3)})"
end
end

def sub_html(text)
text.gsub(html_img_regex) do
preceding_src = Regexp.last_match(1)
quote_mark = Regexp.last_match(2)
url = Regexp.last_match(3)
ending = Regexp.last_match(4)
"<img#{preceding_src}src=#{quote_mark}#{camo(url)}#{quote_mark}#{ending}"
end
end

def markdown_img_regex
# ![alt text](url =widthxheight "title")
/!\[([^\[]*)\]\(([^\ )]+)(( =[^)]?[^ ]+)?( [^)]?"[^)]+")?)?\)/
end

def html_img_regex
# warning: this regex may not be perfect. They rarely are.
# If you find an edge case, improve this regex!
# <img...something... src="url"...ending...
# or, the alternative quotes: <img...something... src='url'...ending...
# or, even without quotes: <img...something... src=url...ending...
# and the ...ending... can be either a space, a > or />
# note that we don't allow mismatched quotes like 'url" or shenanigans like that
# This regex contains two particularly useful features:
# capturing groups, and lazy matching.
%r{<img([^>]*\s)src=(["']?)(.+?)\2(\s|>|/)}
end
end
37 changes: 37 additions & 0 deletions spec/helpers/markdown_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,43 @@
'![](https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067 =100x* "Image title")'
)
end

it do
expect(camofy('<img src="http://example.org/image.jpg">')).to eq(
'<img src="https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067">'
)
end

it do
expect(camofy("<img src='http://example.org/image.jpg'>")).to eq(
"<img src='https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067'>"
)
end

it do
expect(camofy('<img src="http://example.org/image.jpg"/>')).to eq(
'<img src="https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067"/>'
)
end

it do
expect(camofy('<img src="http://example.org/image.jpg" />')).to eq(
'<img src="https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067" />'
)
end

it do
expect(camofy('<img src="http://example.org/image.jpg" style="somekindofstyle" >')).to eq(
'<img src="https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067" style="somekindofstyle" >'
)
end

it do
expect(camofy('<img alt="" style="somekindofstyle" src="http://example.org/image.jpg">')).to eq(
'<img alt="" style="somekindofstyle" src="https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067">'
)
end

# rubocop:enable Layout/LineLength
end
end
Loading