Skip to content

Commit 565df46

Browse files
committed
sitemap 多语言支持
1 parent c28e936 commit 565df46

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-4
lines changed

404.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
layout: splash
3+
sitemap: false
34
---
45

56
<div class="notice--danger">

404.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
layout: splash
3+
sitemap: false
34
---
45

56
<div class="notice--danger">

404.zh-TW.md renamed to 404.zh-Hant.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
layout: splash
3+
sitemap: false
34
---
45

56
<div class="notice--danger" id="not-found-content">

_config.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ defaults:
123123
path: assets
124124
values:
125125
i18n: false
126-
- scope:
127-
path: robots.txt
128-
values:
129-
i18n: false
130126

131127
# liquid:
132128
# error_mode: warn

_layouts/robots.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sitemap: {{ "sitemap.xml" | absolute_url }}

_layouts/sitemap.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
{% if page.xsl %}
3+
<?xml-stylesheet type="text/xsl" href="{{ "/sitemap.xsl" | absolute_url }}"?>
4+
{% endif %}
5+
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
6+
{% assign collections = site.collections | where_exp:'collection','collection.output != false' %}
7+
{% for collection in collections %}
8+
{%- assign locale = page.locale | default: site.locale | default: "en" %}
9+
{% assign docs = collection.docs | where: "locale", locale | where_exp:'doc','doc.sitemap != false' %}
10+
{% for doc in docs %}
11+
<url>
12+
<loc>{{ doc.url | replace:'/index.html','/' | absolute_url | xml_escape }}</loc>
13+
{% if doc.last_modified_at or doc.date %}
14+
<lastmod>{{ doc.last_modified_at | default: doc.date | date_to_xmlschema }}</lastmod>
15+
{% endif %}
16+
{% if doc.default.translations %}
17+
{% for item in doc.default.translations %}
18+
<xhtml:link rel="alternate" hreflang="{{ item[0] }}" href="{{ item[1].url | absolute_url }}"/>
19+
{% endfor %}
20+
{% endif %}
21+
{% if doc.default.fallbacks %}
22+
{% for item in doc.default.fallbacks %}
23+
<xhtml:link rel="alternate" hreflang="{{ item[0] }}" href="{{ item[1].url | absolute_url }}"/>
24+
{% endfor %}
25+
{% endif %}
26+
</url>
27+
{% endfor %}
28+
{% endfor %}
29+
30+
{% assign pages = site.html_pages | where: "locale", locale | where_exp:'doc','doc.sitemap != false' | where_exp:'doc','doc.url != "/404.html"' %}
31+
{% for page in pages %}
32+
<url>
33+
<loc>{{ page.url | replace:'/index.html','/' | absolute_url | xml_escape }}</loc>
34+
{% if page.last_modified_at or page.date %}
35+
<lastmod>{{ page.last_modified_at | default: page.date | date_to_xmlschema }}</lastmod>
36+
{% endif %}
37+
{% if page.default.translations %}
38+
{% for item in page.default.translations %}
39+
<xhtml:link rel="alternate" hreflang="{{ item[0] }}" href="{{ item[1].url | absolute_url }}"/>
40+
{% endfor %}
41+
{% endif %}
42+
{% if page.default.fallbacks %}
43+
{% for item in page.default.fallbacks %}
44+
<xhtml:link rel="alternate" hreflang="{{ item[0] }}" href="{{ item[1].url | absolute_url }}"/>
45+
{% endfor %}
46+
{% endif %}
47+
</url>
48+
{% endfor %}
49+
50+
{% assign static_files = page.static_files | where_exp:'page','page.sitemap != false' | where_exp:'page','page.name != "404.html"' %}
51+
{% for file in static_files %}
52+
<url>
53+
<loc>{{ file.path | replace:'/index.html','/' | absolute_url | xml_escape }}</loc>
54+
<lastmod>{{ file.modified_time | date_to_xmlschema }}</lastmod>
55+
</url>
56+
{% endfor %}
57+
</urlset>

_plugins/i18n.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,32 @@ def feed_source_path
66
end
77
end
88

9+
module Jekyll
10+
class JekyllSitemap < Jekyll::Generator
11+
def source_path(file = "sitemap.xml")
12+
File.expand_path "_layouts/#{file}", @site.config["source"]
13+
end
14+
15+
def sitemap
16+
site_map = PageWithoutAFile.new(@site, __dir__, "", "sitemap.xml")
17+
site_map.content = File.read(source_path).gsub(MINIFY_REGEX, "")
18+
site_map.data["layout"] = nil
19+
site_map.data["static_files"] = static_files.map(&:to_liquid)
20+
site_map.data["xsl"] = file_exists?("sitemap.xsl")
21+
site_map.data["i18n"] = false
22+
site_map
23+
end
24+
25+
def robots
26+
robots = PageWithoutAFile.new(@site, __dir__, "", "robots.txt")
27+
robots.content = File.read(source_path("robots.txt"))
28+
robots.data["layout"] = nil
29+
robots.data["i18n"] = false
30+
robots
31+
end
32+
end
33+
end
34+
935
module I18nFilter
1036
def i18n(hash, locale, key)
1137
return nil unless hash.is_a?(Hash) && locale.is_a?(String) && key.is_a?(String)

0 commit comments

Comments
 (0)