Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendations on how to have autolink for phone numbers? #139

Closed
dorianmariecom opened this issue Jun 12, 2021 · 3 comments
Closed

Recommendations on how to have autolink for phone numbers? #139

dorianmariecom opened this issue Jun 12, 2021 · 3 comments

Comments

@dorianmariecom
Copy link

dorianmariecom commented Jun 12, 2021

Here is how I'm doing it but it's quite hacky, the main issue is with links that contain what could be a phone number

  PHONE_NUMBER_REGEXP = /(\+ ?|[0-9])[ 0-9()-]{8,}[0-9)]/

  def markdown(text)
    html =
      CommonMarker.render_html(text.to_s, :HARDBREAKS, [:autolink]).html_safe
    html.gsub(PHONE_NUMBER_REGEXP) do |match|
      next match if match =~ /^[0-9]{11,}$/
      "<a href=\"sms:#{h(match)}\">#{h(match)}</a>"
    end.html_safe
  end
@kivikakk
Copy link
Collaborator

If you can use a Renderer (like HtmlRenderer), you could do this by overriding/wrapping the text callback, ensuring you don't fire on random bits of text elsewhere. The alternative would be to do this same thing as a post-processing step with Nokogiri or similar.

@gjtorikian
Copy link
Owner

Yeah I would say that tapping into the a elements with Nokogiri (or https://github.com/gjtorikian/html-pipeline) would be the best option here.

@dorianmariecom
Copy link
Author

https://github.com/gjtorikian/html-pipeline is exactly what I need, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants