-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Autolink emails and URLs at rendering time #564
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| module HtmlHelper | ||
| def format_html(html) | ||
| fragment = Nokogiri::HTML.fragment(html) | ||
|
|
||
| auto_link(fragment) | ||
|
|
||
| fragment.to_html.html_safe | ||
| end | ||
|
|
||
| private | ||
| EXCLUDED_ELEMENTS = %w[ a figcaption pre code ] | ||
| EMAIL_REGEXP = /\b[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\b/ | ||
| URL_REGEXP = URI::DEFAULT_PARSER.make_regexp(%w[http https]) | ||
|
|
||
| def auto_link(fragment) | ||
| fragment.traverse do |node| | ||
| next unless auto_linkable_node?(node) | ||
|
|
||
| content = node.text | ||
| linked_content = content.dup | ||
|
|
||
| auto_link_urls(linked_content) | ||
| auto_link_emails(linked_content) | ||
|
|
||
| if linked_content != content | ||
| node.replace(Nokogiri::HTML.fragment(linked_content)) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def auto_linkable_node?(node) | ||
| node.text? && node.ancestors.none? { |ancestor| EXCLUDED_ELEMENTS.include?(ancestor.name) } | ||
| end | ||
|
|
||
| def auto_link_urls(linked_content) | ||
| linked_content.gsub!(URL_REGEXP) do |match| | ||
| %(<a href="#{match}" rel="noreferrer">#{match}</a>) | ||
| end | ||
| end | ||
|
|
||
| def auto_link_emails(text) | ||
| text.gsub!(EMAIL_REGEXP) do |match| | ||
| %(<a href="mailto:#{match}">#{match}</a>) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| export default class extends Controller { | ||
| connect() { | ||
| this.element.querySelectorAll("a").forEach(this.#retargetLink.bind(this)) | ||
| } | ||
|
|
||
| #retargetLink(link) { | ||
| link.target = this.#targetsSameDomain(link) ? "_top" : "_blank" | ||
| } | ||
|
|
||
| #targetsSameDomain(link) { | ||
| return link.href.startsWith(window.location.origin) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <div class="action-text-content"> | ||
| <%= yield -%> | ||
| <%= format_html yield -%> | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require "test_helper" | ||
|
|
||
| class HtmlHelperTest < ActionView::TestCase | ||
| test "converts URLs into anchor tags" do | ||
| assert_equal_html \ | ||
| %(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a></p>), | ||
| format_html("<p>Check this: https://example.com</p>") | ||
| end | ||
|
|
||
| test "respect existing links" do | ||
| assert_equal_html \ | ||
| %(<p>Check this: <a href="https://example.com">https://example.com</a></p>), | ||
| format_html("<p>Check this: <a href=\"https://example.com\">https://example.com</a></p>") | ||
| end | ||
|
|
||
| test "converts email addresses into mailto links" do | ||
| assert_equal_html \ | ||
| %(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>), | ||
| format_html("<p>Contact us at support@example.com</p>") | ||
| end | ||
|
|
||
| test "respect existing linked emails" do | ||
| assert_equal_html \ | ||
| %(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>), | ||
| format_html(%(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>)) | ||
| end | ||
|
|
||
| test "don't autolink content in excluded elements" do | ||
| %w[ figcaption pre code ].each do |element| | ||
| assert_equal_html \ | ||
| "<#{element}>Check this: https://example.com</#{element}>", | ||
| format_html("<#{element}>Check this: https://example.com</#{element}>") | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to fix a problem where clicking on internal links will fail with a turbo-frame missing error. This will also make external links open in new pages.
I didn't handle this on the server because determining what's an internal link is simpler in the client, as right now we don't have a good way of querying for the current host in the server (in a way that doesn't require a web request/controller context). I wasn't sure if we could just use that
%{tenant}variable I'm seeing we use forconfig.action_mailer.default_url_optionsin regular hosts. Like, we could defineconfig.hostsand assume that the firstconfig.hostis the app URL (as we did in KIA), but I wasn't sure how to handle the tenant thing. cc @flavorjonesRegardless of that, I think doing this client side is totally fine. We do something like this in campfire too.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW we're planning to move away from using subdomains as the tenant parameter, to using path-based tenanting (similar to BC4's account slug), in which case the hostname will be the same for all tenants.