Class: Bridgetown::SeoTag::Drop

Inherits:
Drops::Drop
  • Object
show all
Includes:
UrlHelper
Defined in:
lib/bridgetown-seo-tag/drop.rb

Constant Summary collapse

TITLE_SEPARATOR =
" | "
FORMAT_STRING_METHODS =
[
  :markdownify, :strip_html, :normalize_whitespace, :escape_once,
].freeze
HOMEPAGE_OR_ABOUT_REGEX =
%r!^/(about/)?(index.html?)?$!.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text, context) ⇒ Drop

Returns a new instance of Drop.



14
15
16
17
18
19
# File 'lib/bridgetown-seo-tag/drop.rb', line 14

def initialize(text, context)
  @obj = {}
  @mutations = {}
  @text = text
  @context = context
end

Instance Method Details

#authorObject

A drop representing the page author



99
100
101
# File 'lib/bridgetown-seo-tag/drop.rb', line 99

def author
  @author ||= AuthorDrop.new(page: page, site: site)
end

#canonical_urlObject



167
168
169
170
171
172
173
174
175
# File 'lib/bridgetown-seo-tag/drop.rb', line 167

def canonical_url
  @canonical_url ||= begin
    if page["canonical_url"].to_s.empty?
      filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/")
    else
      page["canonical_url"]
    end
  end
end

#date_modifiedObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bridgetown-seo-tag/drop.rb', line 110

def date_modified
  @date_modified ||= begin
    date = if page_seo["date_modified"]
             page_seo["date_modified"]
           elsif page["last_modified_at"]
             page["last_modified_at"].to_liquid
           else
             page["date"]
           end
    filters.date_to_xmlschema(date) if date
  end
end

#date_publishedObject



123
124
125
# File 'lib/bridgetown-seo-tag/drop.rb', line 123

def date_published
  @date_published ||= filters.date_to_xmlschema(page["date"]) if page["date"]
end

#descriptionObject



90
91
92
93
94
95
96
# File 'lib/bridgetown-seo-tag/drop.rb', line 90

def description
  @description ||= begin
    format_string(
      page["description"] || page["subtitle"] || page["excerpt"]
    ) || site_description
  end
end

#imageObject

Returns a Drop representing the page’s image Returns nil if the image has no path, to preserve backwards compatability



105
106
107
108
# File 'lib/bridgetown-seo-tag/drop.rb', line 105

def image
  @image ||= ImageDrop.new(page: page, context: @context)
  @image if @image.path
end


141
142
143
144
145
146
147
148
149
# File 'lib/bridgetown-seo-tag/drop.rb', line 141

def links
  @links ||= begin
    if page_seo["links"]
      page_seo["links"]
    elsif homepage_or_about? && site_social["links"]
      site_social["links"]
    end
  end
end

#logoObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bridgetown-seo-tag/drop.rb', line 151

def 
   ||= begin
    return unless site.data.dig("site_metadata", "logo")

    if absolute_url? site.data.dig("site_metadata", "logo")
      filters.uri_escape site.data.dig("site_metadata", "logo")
    else
      filters.uri_escape filters.absolute_url site.data.dig("site_metadata", "logo")
    end
  end
end

#nameObject

rubocop:enable Metrics/CyclomaticComplexity



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bridgetown-seo-tag/drop.rb', line 76

def name
  return @name if defined?(@name)

  @name = if seo_name
            seo_name
          elsif !homepage_or_about?
            nil
          elsif site_social["name"]
            format_string site_social["name"]
          elsif site_title
            site_title
          end
end

#page_langObject



163
164
165
# File 'lib/bridgetown-seo-tag/drop.rb', line 163

def page_lang
  @page_lang ||= page["lang"] || site["lang"] || "en_US"
end

#page_titleObject

Page title without site title or description appended



49
50
51
# File 'lib/bridgetown-seo-tag/drop.rb', line 49

def page_title
  @page_title ||= format_string(page["title"]) || site_title
end

#site_descriptionObject



44
45
46
# File 'lib/bridgetown-seo-tag/drop.rb', line 44

def site_description
  @site_description ||= format_string site.data.dig("site_metadata", "description")
end

#site_taglineObject



40
41
42
# File 'lib/bridgetown-seo-tag/drop.rb', line 40

def site_tagline
  @site_tagline ||= format_string site.data.dig("site_metadata", "tagline")
end

#site_tagline_or_descriptionObject



53
54
55
# File 'lib/bridgetown-seo-tag/drop.rb', line 53

def site_tagline_or_description
  site_tagline || site_description
end

#site_titleObject



33
34
35
36
37
38
# File 'lib/bridgetown-seo-tag/drop.rb', line 33

def site_title
  @site_title ||= format_string(
    site.data.dig("site_metadata", "title") ||
    site.data.dig("site_metadata", "name")
  )
end

#titleObject

Page title with site title or description appended rubocop:disable Metrics/CyclomaticComplexity



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bridgetown-seo-tag/drop.rb', line 59

def title
  @title ||= begin
    if site_title && page_title != site_title
      page_title + TITLE_SEPARATOR + site_title
    elsif site_description && site_title
      site_title + TITLE_SEPARATOR + site_tagline_or_description
    else
      page_title || site_title
    end
  end

  return page_number + @title if page_number

  @title
end

#title?Boolean

Should the ‘<title>` tag be generated for this page?

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/bridgetown-seo-tag/drop.rb', line 26

def title?
  return false unless title
  return @display_title if defined?(@display_title)

  @display_title = (@text !~ %r!title=false!i)
end

#typeObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bridgetown-seo-tag/drop.rb', line 127

def type
  @type ||= begin
    if page_seo["type"]
      page_seo["type"]
    elsif homepage_or_about?
      "WebSite"
    elsif page["date"]
      "BlogPosting"
    else
      "WebPage"
    end
  end
end

#versionObject



21
22
23
# File 'lib/bridgetown-seo-tag/drop.rb', line 21

def version
  Bridgetown::SeoTag::VERSION
end