What method is used by Discourse to identify urls in post body

What method is used by Discourse to identify urls in post body and make it clickable?. I tried going through the codebase. But still no luck.

It uses markdown-it’s linkify extension.

https://github.com/markdown-it/linkify-it

2 Likes

Thank you @notriddle :grinning:

The above is used for client, are there any similar ways for ruby on rails ?

It actually also uses linkify-it.

I could be wrong, AFAIK the PrettyText module is used to cook raw content into markdown: it essentially utilizes mini_racer to embed the V8 JavaScript Engine into Ruby, and runs exactly the same scripts as the client.

6 Likes

I guess you want to extract all the links inside the post for the feature Preventing malicious linking - #27 by Sudaraka. You can get it simply by doc.css("a[href]") using Nokogiri module.

example
https://github.com/discourse/discourse/blob/b3b55e18d1c804982eef5a1b4264234e5a5a335f/lib/cooked_post_processor.rb#L154-L155

5 Likes

Thank you @xrav3nz and @vinothkannans . Yes @vinothkannans I was trying to find the optimal method for extracting all urls in the post :slight_smile:

1 Like