Class: Jekyll::TargetBlank

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-target-blank.rb

Constant Summary collapse

BODY_START_TAG =
"<body"
OPENING_BODY_TAG_REGEX =
%r!<body([^<>]*)>\s*!.freeze

Class Method Summary collapse

Class Method Details

.document_processable?(doc) ⇒ Boolean

Public: Determines if the document should be processed.

doc - the document being processed.

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/jekyll-target-blank.rb', line 44

def document_processable?(doc)
  (doc.is_a?(Jekyll::Page) || doc.write?) &&
    doc.output_ext == ".html" || (doc.permalink&.end_with?("/"))
end

.process(content) ⇒ Object

Public: Processes the content and updated the external links by adding target=“_blank” and rel=“noopener noreferrer” attributes.

content - the document or page to be processes.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-target-blank.rb', line 17

def process(content)
  @site_url                              = content.site.config["url"]
  @config                                = content.site.config
  @target_blank_config                   = class_config
  @requires_specified_css_class          = false
  @required_css_class_name               = nil
  @should_add_css_classes                = false
  @css_classes_to_add                    = nil
  @should_add_noopener                   = true
  @should_add_noreferrrer                = true
  @should_add_extra_rel_attribute_values = false
  @extra_rel_attribute_values            = nil

  return unless content.output.include?("<a")

  initialise

  content.output = if content.output.include? BODY_START_TAG
                     process_html(content)
                   else
                     process_anchor_tags(content.output)
                   end
end