Redcarpet replace urls on dedicated line with custom html

I'm using Redcarpet to parse markdown to HTML. Now I like internal urls (not links) to be replaced by custom cards, but only if the URL is on a dedicated line and not surrounded by any other content on the same line:

## This should be replaced by a card:

https://myapp.io/article/1

## But these should not:

- https://myapp.io/article/1
- Link to [article](https://myapp.io/article/1)

https://stackoverflow.com

Nor should this: https://myapp.io/article/1

I have the autolink extension enabled, and my first thought was to extend this method to render the cards. This works as far as replacing all internal URLs, but afaik there is no way to detects if the URL is on a dedicated line.

class MarkdownRenderer < Redcarpet::Render::HTML
  def autolink(link, link_type)
    if internal_url?(link)
      "<card>...</card>"
    else
      "<a target=\"_blank\" href=\"#{link}\">#{link}</a>"
    end
  end
end

Does anyone know a way to solve this and also keep the autolink feature?

Thanks in advance!

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum