Skip to content

Commit

Permalink
Camofy html image sources (#467)
Browse files Browse the repository at this point in the history
* write a regex for html images. might not be perfect

* linter was annoying

* I'm not used to how ruby deals with returns

* running the linter is a pain

* rename variable

* improve regex to be more acommodating to general whitespace
  • Loading branch information
DrumsnChocolate authored Jan 9, 2025
1 parent 4066ff5 commit 6ee4b7e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
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

0 comments on commit 6ee4b7e

Please sign in to comment.