Skip to content

Commit

Permalink
write a regex for html images. might not be perfect
Browse files Browse the repository at this point in the history
  • Loading branch information
DrumsnChocolate committed Nov 21, 2024
1 parent fbf1070 commit 4a72f7e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ def camofy(markdown)
markdown.gsub(markdown_img_regex) do
"![#{Regexp.last_match(1)}](#{camo(Regexp.last_match(2))}#{Regexp.last_match(3)})"
end

markdown.gsub(html_img_regex) do
quote_mark = Regexp.last_match(2)
"<img#{Regexp.last_match(1)} src=#{quote_mark}#{camo(Regexp.last_match(3))}#{quote_mark}#{Regexp.last_match(4)}"

Check failure on line 13 in app/helpers/markdown_helper.rb

View workflow job for this annotation

GitHub Actions / Lint

Layout/LineLength: Line is too long. [118/100] (https://rubystyle.guide#max-line-length)

Check warning on line 13 in app/helpers/markdown_helper.rb

View check run for this annotation

Codecov / codecov/patch

app/helpers/markdown_helper.rb#L12-L13

Added lines #L12 - L13 were not covered by tests
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!

Check failure on line 23 in app/helpers/markdown_helper.rb

View workflow job for this annotation

GitHub Actions / Lint

Layout/LineLength: Line is too long. [108/100] (https://rubystyle.guide#max-line-length)
# <img...something... src="url"
# or, the alternative quotes: <img...something... src='url'
# or, even without
# 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.
/<img([^>]*) src=(["']?)(.+?)\2([ >])/
end
end

0 comments on commit 4a72f7e

Please sign in to comment.