Skip to content

Commit fee9498

Browse files
committed
sitemap 多语言支持
1 parent bae4822 commit fee9498

File tree

8 files changed

+89
-5
lines changed

8 files changed

+89
-5
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
File renamed without changes.

_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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
module JekyllFeed
22
class Generator < Jekyll::Generator
33
def feed_source_path
4-
@feed_source_path ||= File.expand_path "_layouts/plugins/feed.xml", @site.config["source"]
4+
@feed_source_path ||= File.expand_path "_layouts/feed.xml", @site.config["source"]
5+
end
6+
end
7+
end
8+
9+
module Jekyll
10+
11+
class JekyllSitemap < Jekyll::Generator
12+
def source_path(file = "sitemap.xml")
13+
File.expand_path "_layouts/#{file}", @site.config["source"]
14+
end
15+
16+
def sitemap
17+
site_map = PageWithoutAFile.new(@site, __dir__, "", "sitemap.xml")
18+
site_map.content = File.read(source_path).gsub(MINIFY_REGEX, "")
19+
site_map.data["layout"] = nil
20+
site_map.data["static_files"] = static_files.map(&:to_liquid)
21+
site_map.data["xsl"] = file_exists?("sitemap.xsl")
22+
site_map.data["i18n"] = false
23+
site_map
24+
end
25+
26+
def robots
27+
robots = PageWithoutAFile.new(@site, __dir__, "", "robots.txt")
28+
robots.content = File.read(source_path("robots.txt"))
29+
robots.data["layout"] = nil
30+
robots.data["i18n"] = false
31+
robots
532
end
633
end
734
end

0 commit comments

Comments
 (0)