Class: Bunto::SeoTag::Drop

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

Constant Summary collapse

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

Constants included from JSONLD

JSONLD::METHODS_KEYS

Instance Method Summary collapse

Methods included from JSONLD

#json_ld

Constructor Details

#initialize(text, context) ⇒ Drop

Returns a new instance of Drop.



12
13
14
15
16
17
# File 'lib/bunto-seo-tag/drop.rb', line 12

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

Instance Method Details

#authorObject

Returns a nil or a hash representing the author Author name will be pulled from:

  1. The ‘author` key, if the key is a string

  2. The first author in the ‘authors` key

  3. The ‘author` key in the site config

If the result from the name search is a string, we’ll also check to see if the author exists in ‘site.data.authors`



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bunto-seo-tag/drop.rb', line 84

def author
  @author ||= begin
    return if author_string_or_hash.to_s.empty?

    author = if author_string_or_hash.is_a?(String)
               author_hash(author_string_or_hash)
             else
               author_string_or_hash
             end

    author["twitter"] ||= author["name"]
    author["twitter"].delete! "@" if author["twitter"]
    author.to_liquid
  end
end

#canonical_urlObject



184
185
186
187
188
189
190
191
192
# File 'lib/bunto-seo-tag/drop.rb', line 184

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



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bunto-seo-tag/drop.rb', line 100

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



113
114
115
# File 'lib/bunto-seo-tag/drop.rb', line 113

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

#descriptionObject



69
70
71
72
73
# File 'lib/bunto-seo-tag/drop.rb', line 69

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

#imageObject

Returns nil or a hash representing the page image The image hash will always contain a path, pulled from:

  1. The ‘image` key if it’s a string

  2. The ‘image.path` key if it’s a hash

  3. The ‘image.facebook` key

  4. The ‘image.twitter` key

The resulting path is always an absolute URL



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/bunto-seo-tag/drop.rb', line 161

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

  image = page["image"]
  return @image = nil unless image

  image = { "path" => image } if image.is_a?(String)
  image["path"] ||= image["facebook"] || image["twitter"]
  return @image = nil unless image["path"]

  unless absolute_url? image["path"]
    image["path"] = filters.absolute_url image["path"]
  end

  image["path"] = filters.uri_escape image["path"]

  @image = image.to_liquid
end


131
132
133
134
135
136
137
138
139
# File 'lib/bunto-seo-tag/drop.rb', line 131

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



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

def 
  @logo ||= begin
    return unless site["logo"]
    if absolute_url? site["logo"]
      filters.uri_escape site["logo"]
    else
      filters.uri_escape filters.absolute_url site["logo"]
    end
  end
end

#nameObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bunto-seo-tag/drop.rb', line 56

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



180
181
182
# File 'lib/bunto-seo-tag/drop.rb', line 180

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

#page_titleObject

Page title without site title or description appended



39
40
41
# File 'lib/bunto-seo-tag/drop.rb', line 39

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

#site_descriptionObject



34
35
36
# File 'lib/bunto-seo-tag/drop.rb', line 34

def site_description
  @site_description ||= format_string site["description"]
end

#site_titleObject



30
31
32
# File 'lib/bunto-seo-tag/drop.rb', line 30

def site_title
  @site_title ||= format_string(site["title"] || site["name"])
end

#titleObject

Page title with site title or description appended



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bunto-seo-tag/drop.rb', line 44

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_description
    else
      page_title || site_title
    end
  end
end

#title?Boolean

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

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/bunto-seo-tag/drop.rb', line 24

def title?
  return false unless title
  return @display_title if defined?(@display_title)
  @display_title = (@text !~ %r!title=false!i)
end

#typeObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bunto-seo-tag/drop.rb', line 117

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



19
20
21
# File 'lib/bunto-seo-tag/drop.rb', line 19

def version
  Bunto::SeoTag::VERSION
end