Module: Jekyll::VitePressTheme::SEO
- Defined in:
- lib/jekyll/vitepress_theme/seo.rb
Overview
Generates the metadata shared by the HTML head, sitemap, and robots.txt. Keeping those outputs on one canonical URL resolver prevents contradictory indexing signals across theme consumers. rubocop:disable Metrics/AbcSize, Metrics/ModuleLength
Defined Under Namespace
Classes: GeneratedPage
Constant Summary collapse
- DEFAULT_ROBOTS =
'index,follow,max-image-preview:large,max-snippet:-1,max-video-preview:-1'- ARTICLE_TYPES =
%w[Article BlogPosting NewsArticle TechArticle].freeze
- VERIFICATION_NAMES =
{ 'google' => 'google-site-verification', 'bing' => 'msvalidate.01', 'yandex' => 'yandex-verification', 'baidu' => 'baidu-site-verification', 'facebook' => 'facebook-domain-verification' }.freeze
Class Method Summary collapse
- .absolute?(value) ⇒ Boolean
- .absolute_url(site, value) ⇒ Object
- .alternates_for(item, page_config) ⇒ Object
- .apply(site) ⇒ Object
- .author_for(item, page_config) ⇒ Object
- .breadcrumb_ancestors(item) ⇒ Object
- .breadcrumb_graph(item, canonical, root_url) ⇒ Object
- .canonical_url(site, value) ⇒ Object
- .clean_text(value) ⇒ Object
- .config_for(site) ⇒ Object
- .custom_output?(site, name) ⇒ Boolean
- .dates_for(item, page_config) ⇒ Object
- .enabled?(site) ⇒ Boolean
- .full_title_for(page_title, title, config) ⇒ Object
- .generate_discovery_files(site, items) ⇒ Object
- .hash_value(value) ⇒ Object
- .html_items(site) ⇒ Object
- .image_for(item, page_config, config) ⇒ Object
- .json_ld_for(item, metadata, schema_type, publisher, config) ⇒ Object
- .locale_for(item) ⇒ Object
- .metadata_for(item, page_config = page_config(item)) ⇒ Object
- .page_config(item) ⇒ Object
- .person_reference(author) ⇒ Object
- .positive_integer(value) ⇒ Object
- .posts_document?(item) ⇒ Boolean
- .prepare(item) ⇒ Object
- .publisher_for(site, config) ⇒ Object
- .robots_for(item, page_config, config) ⇒ Object
- .robots_txt(site, config, site_url) ⇒ Object
- .safe_json(value) ⇒ Object
- .schema_type_for(item, page_config, config) ⇒ Object
- .site_title(site) ⇒ Object
- .sitemap_candidate?(item, metadata) ⇒ Boolean
- .sitemap_enabled?(config) ⇒ Boolean
- .sitemap_xml(items) ⇒ Object
- .twitter_for(item, author = nil) ⇒ Object
- .twitter_handle(value) ⇒ Object
- .verifications_for(site) ⇒ Object
- .warn_about_metadata(site, items) ⇒ Object
- .warn_for_items(label, items) ⇒ Object
- .xml_escape(value) ⇒ Object
- .xml_time(value) ⇒ Object
Class Method Details
.absolute?(value) ⇒ Boolean
530 531 532 533 534 535 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 530 def absolute?(value) uri = URI.parse(value.to_s) uri.is_a?(URI::HTTP) && uri.host rescue URI::InvalidURIError false end |
.absolute_url(site, value) ⇒ Object
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 496 def absolute_url(site, value) return nil if value.nil? || value == false string = value.to_s.strip return nil if string.empty? return string if absolute?(string) base = site.config['url'].to_s.strip.sub(%r{/+\z}, '') return nil if base.empty? baseurl = site.config['baseurl'].to_s.strip baseurl = '' if baseurl == '/' baseurl = "/#{baseurl}" unless baseurl.empty? || baseurl.start_with?('/') baseurl = baseurl.sub(%r{/+\z}, '') path = "/#{string}" unless string.start_with?('/') path ||= string "#{base}#{baseurl}#{path}".sub(%r{/index\.html\z}, '/').sub(%r{/+\z}, '/') end |
.alternates_for(item, page_config) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 227 def alternates_for(item, page_config) raw = page_config['alternates'] || item.data['alternates'] Array(raw).filter_map do |alternate| next unless alternate.is_a?(Hash) language = clean_text(alternate['lang'] || alternate['hreflang']) url = absolute_url(item.site, alternate['url'] || alternate['href']) next if language.to_s.empty? || url.to_s.empty? { 'hreflang' => language, 'url' => url, 'locale' => language.tr('-', '_') } end end |
.apply(site) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 37 def apply(site) return unless enabled?(site) items = html_items(site) items.each { |item| prepare(item) } (site, items) generate_discovery_files(site, items) rescue StandardError => e Jekyll.logger.warn('jekyll-vitepress-theme', "SEO generation failed: #{e.}") end |
.author_for(item, page_config) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 168 def (item, page_config) value = page_config['author'] || item.data['author'] || item.site.config['author'] return nil if value.nil? || value == false = value.is_a?(Hash) ? value : { 'name' => value } name = clean_text(['name'] || [:name]) return nil if name.to_s.empty? { 'name' => name, 'url' => absolute_url(item.site, ['url'] || [:url]), 'twitter' => clean_text(['twitter'] || [:twitter]) }.compact end |
.breadcrumb_ancestors(item) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 357 def (item) return [] unless item.respond_to?(:collection) && item.collection docs = item.collection.docs ancestors = [] current = item seen = [] while (parent = Sidebar.parent_doc_for(current, docs)) && !seen.include?(parent) seen << parent ancestors.unshift(parent) current = parent end ancestors end |
.breadcrumb_graph(item, canonical, root_url) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 337 def (item, canonical, root_url) return nil if item.url == '/' ancestors = (item) entries = [{ 'name' => site_title(item.site), 'item' => root_url }] ancestors.each do |ancestor| entries << { 'name' => clean_text(ancestor.data['title']), 'item' => absolute_url(item.site, ancestor.url) } end entries << { 'name' => clean_text(item.data['title'] || site_title(item.site)), 'item' => canonical } entries.reject! { |entry| entry['name'].to_s.empty? || entry['item'].to_s.empty? } return nil if entries.length < 2 { '@type' => 'BreadcrumbList', '@id' => "#{canonical}#breadcrumb", 'itemListElement' => entries.each_with_index.map do |entry, index| { '@type' => 'ListItem', 'position' => index + 1, 'name' => entry['name'], 'item' => entry['item'] } end } end |
.canonical_url(site, value) ⇒ Object
515 516 517 518 519 520 521 522 523 524 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 515 def canonical_url(site, value) url = absolute_url(site, value) return nil unless url uri = URI.parse(url) uri.fragment = nil uri.to_s rescue URI::InvalidURIError nil end |
.clean_text(value) ⇒ Object
537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 537 def clean_text(value) return nil if value.nil? || value == false text = value.to_s.gsub(/\[([^\]]+)\]\([^)]+\)/, '\\1') .gsub(/<[^>]*>/, ' ') .gsub(/[`*~]/, '') .gsub(/\s+/, ' ') .strip text = CGI.unescapeHTML(text) text.empty? ? nil : text end |
.config_for(site) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 69 def config_for(site) theme_config = site.config['jekyll_vitepress'] return {} unless theme_config.is_a?(Hash) value = theme_config.fetch('seo', {}) value == false ? false : hash_value(value) end |
.custom_output?(site, name) ⇒ Boolean
443 444 445 446 447 448 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 443 def custom_output?(site, name) expected = "/#{name}" (site.pages + site.static_files).any? do |item| item.url == expected || (item.respond_to?(:relative_path) && item.relative_path == expected) end end |
.dates_for(item, page_config) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 240 def dates_for(item, page_config) page_theme = hash_value(item.data['jekyll_vitepress']) published = page_config['date_published'] || item.data['date_published'] published ||= item.data['date'] if posts_document?(item) modified = page_config['date_modified'] || item.data['date_modified'] || item.data['last_modified_at'] modified ||= page_theme['last_updated_at'] { 'published' => xml_time(published), 'modified' => xml_time(modified) }.compact end |
.enabled?(site) ⇒ Boolean
64 65 66 67 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 64 def enabled?(site) config = config_for(site) config != false && !(config.is_a?(Hash) && config['enabled'] == false) end |
.full_title_for(page_title, title, config) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 123 def full_title_for(page_title, title, config) return title if page_title.to_s.empty? return page_title if title.to_s.empty? || page_title == title template = config.is_a?(Hash) ? config['title_template'] : nil if template.to_s.include?(':page') || template.to_s.include?(':site') clean_text(template.to_s.gsub(':page', page_title).gsub(':site', title)) else separator = config.is_a?(Hash) ? config.fetch('title_separator', ' | ') : ' | ' "#{page_title}#{separator}#{title}" end end |
.generate_discovery_files(site, items) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 372 def generate_discovery_files(site, items) config = config_for(site) return unless config.is_a?(Hash) site_url = absolute_url(site, '/') if sitemap_enabled?(config) && !custom_output?(site, 'sitemap.xml') && absolute?(site_url) site.pages << GeneratedPage.new(site, 'sitemap.xml', sitemap_xml(items)) end return if config.fetch('robots_txt', true) == false || custom_output?(site, 'robots.txt') site.pages << GeneratedPage.new(site, 'robots.txt', robots_txt(site, config, site_url)) end |
.hash_value(value) ⇒ Object
559 560 561 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 559 def hash_value(value) value.is_a?(Hash) ? value : {} end |
.html_items(site) ⇒ Object
437 438 439 440 441 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 437 def html_items(site) pages = site.pages.select { |page| page.output_ext == '.html' } docs = site.collections.values.reject { |collection| collection.['output'] == false }.flat_map(&:docs) (pages + docs).uniq end |
.image_for(item, page_config, config) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 150 def image_for(item, page_config, config) raw = page_config['image'] || item.data['image'] raw ||= config['image'] if config.is_a?(Hash) raw ||= item.site.config['logo'] return nil if raw.nil? || raw == false image = raw.is_a?(Hash) ? raw : { 'path' => raw } path = image['path'] || image['src'] || image['url'] return nil if path.to_s.strip.empty? { 'url' => absolute_url(item.site, path), 'alt' => clean_text(image['alt'] || item.data['image_alt'] || item.data['title'] || site_title(item.site)), 'width' => positive_integer(image['width']), 'height' => positive_integer(image['height']) }.compact end |
.json_ld_for(item, metadata, schema_type, publisher, config) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 260 def json_ld_for(item, , schema_type, publisher, config) return nil if config.is_a?(Hash) && config.dig('schema', 'enabled') == false return nil if ['canonical_url'].to_s.empty? site = item.site root_url = absolute_url(site, '/') website_id = "#{root_url}#website" webpage_id = "#{['canonical_url']}#webpage" graph = [] if publisher publisher_id = "#{root_url}#identity" identity = { '@type' => publisher['type'], '@id' => publisher_id, 'name' => publisher['name'], 'url' => publisher['url'] } identity['logo'] = { '@type' => 'ImageObject', 'url' => publisher['logo'] } if publisher['logo'] identity['sameAs'] = publisher['same_as'] unless publisher['same_as'].empty? graph << identity end website = { '@type' => 'WebSite', '@id' => website_id, 'url' => root_url, 'name' => site_title(site), 'description' => clean_text(site.config['description']), 'inLanguage' => ['language'] }.compact website['publisher'] = { '@id' => "#{root_url}#identity" } if publisher graph << website if ['image'] graph << { '@type' => 'ImageObject', '@id' => "#{['image']['url']}#primaryimage", 'url' => ['image']['url'], 'contentUrl' => ['image']['url'], 'caption' => ['image']['alt'], 'width' => ['image']['width'], 'height' => ['image']['height'] }.compact end article_type = ARTICLE_TYPES.include?(schema_type) webpage = { '@type' => article_type ? 'WebPage' : schema_type, '@id' => webpage_id, 'url' => ['canonical_url'], 'name' => ['page_title'], 'description' => ['description'], 'isPartOf' => { '@id' => website_id }, 'inLanguage' => ['language'], 'datePublished' => ['date_published'], 'dateModified' => ['date_modified'] }.compact if ['image'] webpage['primaryImageOfPage'] = { '@id' => "#{['image']['url']}#primaryimage" } end = (item, ['canonical_url'], root_url) if webpage['breadcrumb'] = { '@id' => "#{['canonical_url']}#breadcrumb" } graph << end graph << webpage if article_type article_id = "#{['canonical_url']}#article" webpage['mainEntity'] = { '@id' => article_id } article = { '@type' => schema_type, '@id' => article_id, 'mainEntityOfPage' => { '@id' => webpage_id }, 'isPartOf' => { '@id' => website_id }, 'headline' => ['page_title'], 'description' => ['description'], 'inLanguage' => ['language'], 'datePublished' => ['date_published'], 'dateModified' => ['date_modified'] }.compact article['image'] = { '@id' => "#{['image']['url']}#primaryimage" } if ['image'] article['author'] = person_reference(['author']) if ['author'] article['publisher'] = { '@id' => "#{root_url}#identity" } if publisher graph << article end safe_json('@context' => 'https://schema.org', '@graph' => graph) end |
.locale_for(item) ⇒ Object
486 487 488 489 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 486 def locale_for(item) value = item.data['locale'] || item.data['lang'] || item.site.config['locale'] || item.site.config['lang'] || 'en-US' clean_text(value) || 'en-US' end |
.metadata_for(item, page_config = page_config(item)) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 84 def (item, page_config = page_config(item)) site = item.site config = config_for(site) page_title = clean_text(page_config['title'] || item.data['title'] || site_title(site)) full_title = full_title_for(page_title, site_title(site), config) description = clean_text(page_config['description'] || item.data['description'] || site.config['description']) canonical = canonical_url(site, page_config['canonical_url'] || item.data['canonical_url'] || item.url) locale = locale_for(item) image = image_for(item, page_config, config) = (item, page_config) schema_type = schema_type_for(item, page_config, config) dates = dates_for(item, page_config) robots = robots_for(item, page_config, config) alternates = alternates_for(item, page_config) publisher = publisher_for(site, config) = { 'title' => full_title, 'page_title' => page_title, 'description' => description, 'canonical_url' => canonical, 'locale' => locale.tr('-', '_'), 'language' => locale.tr('_', '-'), 'robots' => robots, 'og_type' => ARTICLE_TYPES.include?(schema_type) ? 'article' : 'website', 'image' => image, 'author' => , 'twitter' => twitter_for(item, ), 'facebook' => hash_value(site.config['facebook']), 'verifications' => verifications_for(site), 'alternates' => alternates, 'date_published' => dates['published'], 'date_modified' => dates['modified'], 'sitemap_url' => sitemap_enabled?(config) ? absolute_url(site, '/sitemap.xml') : nil } ['json_ld'] = json_ld_for(item, , schema_type, publisher, config) end |
.page_config(item) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 77 def page_config(item) value = item.data['seo'] return false if value == false value.is_a?(Hash) ? value : {} end |
.person_reference(author) ⇒ Object
567 568 569 570 571 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 567 def person_reference() value = { '@type' => 'Person', 'name' => ['name'] } value['url'] = ['url'] if ['url'] value end |
.positive_integer(value) ⇒ Object
549 550 551 552 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 549 def positive_integer(value) integer = Integer(value, exception: false) integer if integer&.positive? end |
.posts_document?(item) ⇒ Boolean
563 564 565 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 563 def posts_document?(item) item.respond_to?(:collection) && item.collection&.label == 'posts' end |
.prepare(item) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 48 def prepare(item) return unless enabled?(item.site) page_config = page_config(item) if page_config == false item.data['_seo_disabled'] = true item.data.delete('_seo') return end = (item, page_config) item.data['_seo'] = item.data['sitemap'] = false unless sitemap_candidate?(item, ) end |
.publisher_for(site, config) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 183 def publisher_for(site, config) raw = config['publisher'] if config.is_a?(Hash) return nil unless raw.is_a?(Hash) name = clean_text(raw['name'] || site_title(site)) return nil if name.to_s.empty? type = %w[Organization Person].include?(raw['type']) ? raw['type'] : 'Organization' image = raw['logo'] || site.config['logo'] same_as = Array(raw['same_as'] || raw['sameAs']).filter_map do |url| absolute_url(site, url) end { 'type' => type, 'name' => name, 'url' => absolute_url(site, raw['url'] || '/'), 'logo' => absolute_url(site, image.is_a?(Hash) ? image['path'] || image['src'] : image), 'same_as' => same_as }.compact end |
.robots_for(item, page_config, config) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 136 def robots_for(item, page_config, config) explicit = page_config['robots'] || item.data['robots'] return clean_text(explicit) if explicit && explicit != true site_index = !config.is_a?(Hash) || config.fetch('index', true) != false noindex = page_config['noindex'] == true || item.data['noindex'] == true || !site_index nofollow = page_config['nofollow'] == true || item.data['nofollow'] == true return "noindex,#{nofollow ? 'nofollow' : 'follow'}" if noindex return DEFAULT_ROBOTS unless config.is_a?(Hash) clean_text(config['robots']) || DEFAULT_ROBOTS end |
.robots_txt(site, config, site_url) ⇒ Object
411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 411 def robots_txt(site, config, site_url) index = config.fetch('index', true) != false lines = ['User-agent: *', index ? 'Allow: /' : 'Disallow: /'] lines << 'Disallow: /*.md$' if index && CopyPage.site_enabled?(site) additional = config['robots_txt_rules'] lines.push('', additional.to_s.strip) unless additional.to_s.strip.empty? sitemap_url = absolute_url(site, '/sitemap.xml') if sitemap_enabled?(config) && absolute?(site_url) && sitemap_url lines.push('', "Sitemap: #{sitemap_url}") end "#{lines.join("\n")}\n" end |
.safe_json(value) ⇒ Object
583 584 585 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 583 def safe_json(value) JSON.generate(value).gsub('<', '\\u003c').gsub('>', '\\u003e').gsub('&', '\\u0026') end |
.schema_type_for(item, page_config, config) ⇒ Object
253 254 255 256 257 258 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 253 def schema_type_for(item, page_config, config) value = page_config['type'] value ||= config['page_type'] if config.is_a?(Hash) value = 'WebPage' if value.to_s.empty? || item.url == '/' clean_text(value) end |
.site_title(site) ⇒ Object
491 492 493 494 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 491 def site_title(site) branding = site.config.dig('jekyll_vitepress', 'branding') clean_text(site.config['title'] || (branding['site_title'] if branding.is_a?(Hash))) end |
.sitemap_candidate?(item, metadata) ⇒ Boolean
424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 424 def sitemap_candidate?(item, ) return false unless .is_a?(Hash) return false if item.data['sitemap'] == false || item.data['redirect_to'] || item.data['_seo_disabled'] return false if ['/404.html', '/404/'].include?(item.url) robot_tokens = ['robots'].to_s.downcase.split(',').map(&:strip) return false if robot_tokens.intersect?(%w[noindex none]) canonical = ['canonical_url'] self_url = absolute_url(item.site, item.url) absolute?(canonical) && canonical == self_url end |
.sitemap_enabled?(config) ⇒ Boolean
526 527 528 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 526 def sitemap_enabled?(config) config.fetch('index', true) != false && config.fetch('sitemap', true) != false end |
.sitemap_xml(items) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 386 def sitemap_xml(items) rows = items.filter_map do |item| = item.data['_seo'] next unless sitemap_candidate?(item, ) last_modified = ['date_modified'] last_modified ||= ['date_published'] if posts_document?(item) [['canonical_url'], last_modified] end rows.uniq!(&:first) rows.sort_by!(&:first) body = rows.map do |url, last_modified| lastmod = last_modified ? "\n <lastmod>#{xml_escape(last_modified)}</lastmod>" : '' " <url>\n <loc>#{xml_escape(url)}</loc>#{lastmod}\n </url>" end.join("\n") <<~XML <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> #{body} </urlset> XML end |
.twitter_for(item, author = nil) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 205 def twitter_for(item, = nil) site_twitter = hash_value(item.site.config['twitter']) page_twitter = hash_value(item.data['twitter']) creator = page_twitter['creator'] || page_twitter['username'] || &.dig('twitter') { 'card' => page_twitter['card'] || site_twitter['card'] || 'summary_large_image', 'site' => twitter_handle(site_twitter['username']), 'creator' => twitter_handle(creator) }.compact end |
.twitter_handle(value) ⇒ Object
554 555 556 557 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 554 def twitter_handle(value) handle = clean_text(value) handle ? "@#{handle.delete_prefix('@')}" : nil end |
.verifications_for(site) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 217 def verifications_for(site) values = hash_value(site.config['webmaster_verifications']) values['google'] ||= site.config['google_site_verification'] VERIFICATION_NAMES.filter_map do |key, name| content = clean_text(values[key]) { 'name' => name, 'content' => content } if content end end |
.warn_about_metadata(site, items) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 450 def (site, items) root = absolute_url(site, '/') unless absolute?(root) Jekyll.logger.warn( 'jekyll-vitepress-theme', 'Set an absolute `url` in _config.yml to emit canonical URLs, JSON-LD, sitemap.xml, and robots.txt discovery.' ) end enabled_items = items.reject { |item| item.data['_seo_disabled'] } warn_for_items('Missing SEO title', enabled_items.select { |item| item.data.dig('_seo', 'page_title').to_s.empty? }) warn_for_items('Missing SEO description', enabled_items.select { |item| item.data.dig('_seo', 'description').to_s.empty? }) duplicate_titles = enabled_items.group_by { |item| item.data.dig('_seo', 'title') } .select { |title, matches| !title.to_s.empty? && matches.length > 1 } duplicate_titles.each do |title, matches| paths = matches.first(5).map { |item| item.path || item.url }.join(', ') Jekyll.logger.warn('jekyll-vitepress-theme', "Duplicate SEO title '#{title}': #{paths}") end duplicate_descriptions = enabled_items.group_by { |item| item.data.dig('_seo', 'description') } .select { |description, matches| !description.to_s.empty? && matches.length > 1 } duplicate_descriptions.each do |description, matches| paths = matches.first(5).map { |item| item.path || item.url }.join(', ') Jekyll.logger.warn('jekyll-vitepress-theme', "Duplicate SEO description '#{description}': #{paths}") end end |
.warn_for_items(label, items) ⇒ Object
478 479 480 481 482 483 484 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 478 def warn_for_items(label, items) return if items.empty? paths = items.first(5).map { |item| item.path || item.url }.join(', ') suffix = items.length > 5 ? " (and #{items.length - 5} more)" : '' Jekyll.logger.warn('jekyll-vitepress-theme', "#{label}: #{paths}#{suffix}") end |
.xml_escape(value) ⇒ Object
587 588 589 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 587 def xml_escape(value) CGI.escapeHTML(value.to_s) end |
.xml_time(value) ⇒ Object
573 574 575 576 577 578 579 580 581 |
# File 'lib/jekyll/vitepress_theme/seo.rb', line 573 def xml_time(value) return nil if value.nil? || value == false return value if value.is_a?(String) && value.match?(/\A\d{4}-\d{2}-\d{2}\z/) time = value.respond_to?(:to_time) ? value.to_time : Time.parse(value.to_s) time.xmlschema rescue ArgumentError, TypeError nil end |