From 4a72f7e9d89124c32b39194a5409bb8dc733486c Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Thu, 21 Nov 2024 21:29:37 +0100 Subject: [PATCH 1/6] write a regex for html images. might not be perfect --- app/helpers/markdown_helper.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index bd20938b..44309a25 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -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) + "]*) src=(["']?)(.+?)\2([ >])/ + end end From 4f20a0839397a5002e12ca6453ad6405ffc19fcf Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Thu, 21 Nov 2024 21:45:19 +0100 Subject: [PATCH 2/6] linter was annoying --- app/helpers/markdown_helper.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index 44309a25..0c89b241 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -4,13 +4,24 @@ module MarkdownHelper def camofy(markdown) return unless markdown - markdown.gsub(markdown_img_regex) do + sub_markdown(markdown) + + sub_html(markdown) + 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 - markdown.gsub(html_img_regex) do + def sub_html(text) + text.gsub(html_img_regex) do + preceding_src = Regexp.last_match(1) quote_mark = Regexp.last_match(2) - " Date: Thu, 21 Nov 2024 22:11:09 +0100 Subject: [PATCH 3/6] I'm not used to how ruby deals with returns --- app/helpers/markdown_helper.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index 0c89b241..f6d1c2cc 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -4,8 +4,7 @@ module MarkdownHelper def camofy(markdown) return unless markdown - sub_markdown(markdown) - + markdown = sub_markdown(markdown) sub_html(markdown) end From 90aecb48c423fda140f3d3913d62e09319df9b08 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Thu, 21 Nov 2024 22:31:59 +0100 Subject: [PATCH 4/6] running the linter is a pain --- app/helpers/markdown_helper.rb | 9 ++++--- spec/helpers/markdown_helper_spec.rb | 37 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index f6d1c2cc..bdd733a0 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -32,12 +32,13 @@ def markdown_img_regex def html_img_regex # warning: this regex may not be perfect. They rarely are. # If you find an edge case, improve this regex! - # 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. - /]*) src=(["']?)(.+?)\2([ >])/ + %r{]*) src=(["']?)(.+?)\2( |>|/>)} end end diff --git a/spec/helpers/markdown_helper_spec.rb b/spec/helpers/markdown_helper_spec.rb index 2ecaa637..aeb958b7 100644 --- a/spec/helpers/markdown_helper_spec.rb +++ b/spec/helpers/markdown_helper_spec.rb @@ -29,6 +29,43 @@ '![](https://example.org/c7125941763fc18c9d8977ed19028ca5f9378070/687474703a2f2f6578616d706c652e6f72672f696d6167652e6a7067 =100x* "Image title")' ) end + + it do + expect(camofy('')).to eq( + '' + ) + end + + it do + expect(camofy("")).to eq( + "" + ) + end + + it do + expect(camofy('')).to eq( + '' + ) + end + + it do + expect(camofy('')).to eq( + '' + ) + end + + it do + expect(camofy('')).to eq( + '' + ) + end + + it do + expect(camofy('')).to eq( + '' + ) + end + # rubocop:enable Layout/LineLength end end From 02b0f950657f3c1fcc3ebe995d3faa143f980a1d Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Thu, 21 Nov 2024 22:52:07 +0100 Subject: [PATCH 5/6] rename variable --- app/helpers/markdown_helper.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index bdd733a0..79a629f3 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -1,11 +1,11 @@ module MarkdownHelper include CamoHelper - def camofy(markdown) - return unless markdown + def camofy(text) + return unless text - markdown = sub_markdown(markdown) - sub_html(markdown) + text = sub_markdown(text) + sub_html(text) end def sub_markdown(text) From ad7ba115104d5e2fb2436895f574fc3af5fa3645 Mon Sep 17 00:00:00 2001 From: DrumsnChocolate Date: Fri, 22 Nov 2024 00:28:08 +0100 Subject: [PATCH 6/6] improve regex to be more acommodating to general whitespace --- app/helpers/markdown_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index 79a629f3..8adbbac9 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -20,7 +20,7 @@ def sub_html(text) quote_mark = Regexp.last_match(2) url = Regexp.last_match(3) ending = Regexp.last_match(4) - "]*) src=(["']?)(.+?)\2( |>|/>)} + %r{]*\s)src=(["']?)(.+?)\2(\s|>|/)} end end