From 53dc38e65019a7c4b94031d0e97f7e410e3ea862 Mon Sep 17 00:00:00 2001 From: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> Date: Thu, 21 Sep 2023 12:46:03 +0530 Subject: [PATCH] fix: Renders youtube and vimeo links within blank lines as embeds (#7422) Within the article we are now rendering every link for youtube and vimeo as embeds. This isn't a good solution, as users might need to have plain links as well. This fix will render only links within two blank lines as embeds. Co-authored-by: Sojan Jose --- lib/custom_markdown_renderer.rb | 21 ++++++++++++++++++++- spec/lib/custom_markdown_renderer_spec.rb | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/custom_markdown_renderer.rb b/lib/custom_markdown_renderer.rb index b683c9281..3b262b69f 100644 --- a/lib/custom_markdown_renderer.rb +++ b/lib/custom_markdown_renderer.rb @@ -15,11 +15,30 @@ class CustomMarkdownRenderer < CommonMarker::HtmlRenderer end def link(node) - render_embedded_content(node) || super + return if surrounded_by_empty_lines?(node) && render_embedded_content(node) + + # If it's not YouTube or Vimeo link, render normally + super end private + def surrounded_by_empty_lines?(node) + prev_node_empty?(node.previous) && next_node_empty?(node.next) + end + + def prev_node_empty?(prev_node) + prev_node.nil? || node_empty?(prev_node) + end + + def next_node_empty?(next_node) + next_node.nil? || node_empty?(next_node) + end + + def node_empty?(node) + (node.type == :text && node.string_content.strip.empty?) || (node.type != :text) + end + def render_embedded_content(node) link_url = node.url diff --git a/spec/lib/custom_markdown_renderer_spec.rb b/spec/lib/custom_markdown_renderer_spec.rb index 623f12de8..a4b0cae48 100644 --- a/spec/lib/custom_markdown_renderer_spec.rb +++ b/spec/lib/custom_markdown_renderer_spec.rb @@ -104,8 +104,8 @@ describe CustomMarkdownRenderer do end context 'when multiple links are present' do - it 'renders all links' do - markdown = '[youtube](https://www.youtube.com/watch?v=VIDEO_ID) [vimeo](https://vimeo.com/1234567) ^ hello ^ [normal](https://example.com)' + it 'renders all links when present between empty lines' do + markdown = "\n[youtube](https://www.youtube.com/watch?v=VIDEO_ID)\n\n[vimeo](https://vimeo.com/1234567)\n^ hello ^ [normal](https://example.com)" output = render_markdown(markdown) expect(output).to include('src="https://www.youtube.com/embed/VIDEO_ID"') expect(output).to include('src="https://player.vimeo.com/video/1234567"') @@ -113,5 +113,15 @@ describe CustomMarkdownRenderer do expect(output).to include(' hello ') end end + + context 'when links within text are present' do + it 'renders only text within blank lines as embeds' do + markdown = "\n[youtube](https://www.youtube.com/watch?v=VIDEO_ID)\nthis is such an amazing [vimeo](https://vimeo.com/1234567)\n[vimeo](https://vimeo.com/1234567)" + output = render_markdown(markdown) + expect(output).to include('src="https://www.youtube.com/embed/VIDEO_ID"') + expect(output).to include('href="https://vimeo.com/1234567"') + expect(output).to include('src="https://player.vimeo.com/video/1234567"') + end + end end end