Ruby auto-linking of only URLs, with no HTML sanitization.
Forked from https://github.com/tenderlove/rails_autolink.
Add to your Gemfile:
gem "anchored"
Or:
$ gem install anchored
Anchored::Linker.auto_link("text")
# => "text"
Anchored::Linker.auto_link("hello www.example.com.")
# => "hello <a href='http://www.example.com'>www.example.com</a>."
text = "Welcome to http://www.example.com/."
Anchored::Linker.auto_link(text, target: "_blank") do |text|
text[0...12] + "..."
end
# => "Welcome to <a href=\"http://www.example.com/\" target=\"_blank\">http://exampl...</a>."
text = "Hello http://www.example.com/."
Anchored::Linker.auto_link(text, target: "_blank", domain: "example.com")
# => "Hello <a href=\"http://www.example.com/\">http://example.com</a>."
Anchored does not sanitize html. Be sure to use something else for that.
- No HTML sanitization
- No email auto-linking
- No dependencies on rails
- Dropped support for uncommon protocols (gopher, etc)
- Option to remove target attribute when URL matches a domain
Run bin/setup
to install dependencies. Then, run rake test
to run the tests.
You can also run bin/console
for an irb prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/neighborland/anchored. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.