Class: Decidim::ContentRenderers::LinkRenderer

Inherits:
BaseRenderer
  • Object
show all
Defined in:
lib/decidim/content_renderers/link_renderer.rb

Overview

Original: github.com/neighborland/anchored/ A renderer that converts URLs to links and strips attributes in anchors.

Examples: ‘<a href=“urls.net” onmouseover=“alert(’hello’)”>URLs</a>‘ Gets rendered as: `<a href=“decidim.org” target=“_blank” rel=“noopener”>decidim.org</a>` And: `<a href=“javascript:document.cookies”>click me</a>` Gets rendered as: `click me`

Instance Attribute Summary

Attributes inherited from BaseRenderer

#content

Instance Method Summary collapse

Methods inherited from BaseRenderer

#initialize

Constructor Details

This class inherits a constructor from Decidim::ContentRenderers::BaseRenderer

Instance Method Details



27
28
29
30
31
# File 'lib/decidim/content_renderers/link_renderer.rb', line 27

def auto_link(text, options = {}, &block)
  return "" if text.to_s.empty?

  auto_link_urls(text, options, &block)
end

#remove_target_if_local(href, domain, options) ⇒ Object

remove_target_if_local(“same.com/x”, “same.com”, target: “_blank”)

> <a href=“same.com/x”>same.com/x</a>

remove_target_if_local(“same.com/x”, “different.com”, target: “_blank”)

> <a href=“same.com/x” target=“_blank”>same.com/x</a>

modifies options in place



40
41
42
43
44
# File 'lib/decidim/content_renderers/link_renderer.rb', line 40

def remove_target_if_local(href, domain, options)
  return unless options[:target]

  options.delete(:target) if href.include?("//#{domain}")
end

#render(options = {}) ⇒ String

Returns the content ready to display (contains HTML).

Returns:

  • (String)

    the content ready to display (contains HTML)



20
21
22
23
24
25
# File 'lib/decidim/content_renderers/link_renderer.rb', line 20

def render(options = {})
  return content unless content.is_a?(String)

  options = { target: "_blank", rel: "nofollow noopener noreferrer ugc" }.merge(options)
  auto_link(content, options)
end