Sha256: 6a55304e909bd308d92630fba8f9d8e0f5c99e2b0067af42ac9703fb6104e35f

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

Jekyll::Hooks.register :site, :post_render do |site|
  unless site.config['plugins'].include? 'jekyll-linked-posts'
    Jekyll.logger.warn 'Enable jekyll-linked-posts to generate metadata about alternate pages'
    next
  end

  # URLs can be relative but Google asks for fully qualified URLs
  # @see {https://support.google.com/webmasters/answer/189077?hl=en}
  url = site.config['url']&.sub(%r{/\z}, '')

  unless url
    Jekyll.logger.warn 'Provide a `url` value in configuration to create fully qualified URLs'
    next
  end

  site.each_site_file do |doc|
    next unless doc.is_a? Jekyll::Document
    next if doc.output.nil?
    next unless doc&.data&.dig 'locales'

    alternate = doc.data['locales'].map do |translation|
      locale = translation.collection.label
      # TODO: Every doc should know how to build their own URL
      doc_url = [url, locale, translation.url].join('/')

      %(<link rel="alternate" hreflang="#{locale}" href="#{doc_url}" />)
    end

    next if alternate.empty?

    doc.output.sub! '<head>', '<head>' + alternate.join
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-locales-0.1.12 lib/jekyll/locales/hooks/post_render.rb
jekyll-locales-0.1.11 lib/jekyll/locales/hooks/post_render.rb