Skip to content

Commit

Permalink
hreflang
Browse files Browse the repository at this point in the history
  • Loading branch information
usdaud committed Jul 12, 2024
1 parent d469ce5 commit 82bae77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions _layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
{% if page.canonical_url %}
<link rel="canonical" href="{{ page.canonical_url }}" />
{% endif %}

{% if page.hreflang_urls %}
{% for hreflang in page.hreflang_urls %}
<link rel="alternate" hreflang="{{ hreflang.lang }}" href="{{ hreflang.url }}" />
{% endfor %}
{% endif %}
</head>
<body>
<header id="sticky-header">
Expand Down
24 changes: 17 additions & 7 deletions _plugins/letter_page_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class LetterPageGenerator < Generator
safe true

SECTIONS = ['pedia', 'soft', 'brokers', 'market-data', 'community']
LANGUAGES = ['en', 'ru', 'zh']

def generate(site)
Jekyll.logger.info "LetterPageGenerator:", "Total pages: #{site.pages.size}"
Expand All @@ -11,9 +12,7 @@ def generate(site)
@processed_pages = Set.new
base_url = site.config['url'] || ''

languages = ['en', 'ru', 'zh']

languages.each do |lang|
LANGUAGES.each do |lang|
locale = load_locale(site, lang)

process_root_page(site, lang, locale, base_url)
Expand All @@ -40,6 +39,15 @@ def load_locale(site, lang)
end
end

def generate_hreflang_urls(base_url, path)
LANGUAGES.map do |lang|
{
'lang' => lang,
'url' => "#{base_url}/#{lang}#{path}"
}
end
end

def process_root_page(site, lang, locale, base_url)
root_file = File.join(site.source, lang, 'index.md')
if File.exist?(root_file)
Expand All @@ -49,6 +57,7 @@ def process_root_page(site, lang, locale, base_url)
page.data['lang'] = lang
page.data['locale'] = locale
page.data['canonical_url'] = "#{base_url}/#{lang}/"
page.data['hreflang_urls'] = generate_hreflang_urls(base_url, "/")
site.pages << page
else
Jekyll.logger.warn "LetterPageGenerator:", "Root page not found for language: #{lang}"
Expand All @@ -67,6 +76,7 @@ def process_subscribe_page(site, lang, locale, base_url)
page.data['locale'] = locale
page.data['permalink'] = "/#{lang}/subscribe/"
page.data['canonical_url'] = "#{base_url}#{page.data['permalink']}"
page.data['hreflang_urls'] = generate_hreflang_urls(base_url, "/subscribe/")
site.pages << page
end

Expand Down Expand Up @@ -146,13 +156,10 @@ def process_markdown_file(site, file, lang, locale, base_url)
page = Jekyll::Page.new(site, site.source, File.dirname(file), File.basename(file))
page.data['layout'] = 'base'
page.data['title'] = title if title

page.data['lang'] = lang
page.data['locale'] = locale

page.data['permalink'] = "/#{file.sub(site.source + '/', '').sub('.md', '.html')}"
page.data['canonical_url'] = "#{base_url}#{page.data['permalink']}"


site.pages << page
@processed_pages.add(file)
rescue => e
Expand Down Expand Up @@ -201,6 +208,7 @@ def initialize(site, base, lang, section, letter, locale, base_url)
self.data['section'] = section
self.data['permalink'] = "/#{lang}/#{section}/#{letter}/"
self.data['canonical_url'] = "#{base_url}#{self.data['permalink']}"
self.data['hreflang_urls'] = LetterPageGenerator.new.generate_hreflang_urls(base_url, self.data['permalink'])
end
end

Expand Down Expand Up @@ -231,6 +239,7 @@ def initialize(site, base, lang, section, letters, locale, base_url)
self.data['section'] = section
self.data['permalink'] = "/#{lang}/#{section}/"
self.data['canonical_url'] = "#{base_url}#{self.data['permalink']}"
self.data['hreflang_urls'] = LetterPageGenerator.new.generate_hreflang_urls(base_url, self.data['permalink'])
end
end

Expand Down Expand Up @@ -282,6 +291,7 @@ def initialize(site, base, lang, section, catalog_data, locale, base_url)
self.data['section'] = section
self.data['permalink'] = "/#{lang}/#{section}/"
self.data['canonical_url'] = "#{base_url}#{self.data['permalink']}"
self.data['hreflang_urls'] = LetterPageGenerator.new.generate_hreflang_urls(base_url, self.data['permalink'])

special_filters_file = File.join(base, lang, section, 'special_filters.yml')
if File.exist?(special_filters_file)
Expand Down

0 comments on commit 82bae77

Please sign in to comment.