非插件生成jekyll的 sitemap

网上搜到的jekyll生成网站地图 sitemap 大多是使用插件的,而我懒得研究插件,按照 sitemap 格式生成也并不复杂,使用 {% for post in site.posts %}{% for page in site.pages %} 遍历,自个儿生成即可。

一、sitemap

sitemap.xml文件是严格按照xml语言编写的网站地图,用来引导搜索蜘蛛对本站点文章等内容的索引,它是由google提出来的概念。

sitemap的格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://blog.kelu.org/tech/2021/12/25/git-config-list.html</loc>
        <lastmod>2021-12-25T00:00:00+08:00</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>

语法简单。

  • 首尾格式

    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    ...
    </urlset>
    
  • <loc></loc>

    必填。格式为: http://www.xxx.com/xxx,此网址应以协议开始(例如:http)并以斜线结尾。此值应少于 2048 个字符。

  • <lastmod>

    可选标签 标签含义:该文件上次修改的日期。此日期应采用 W3C Datetime 格式。

    如果需要的话,此格式允许省略时间部分,而仅使用 YYYY-MM-DD。

  • <changefreq>

    可选标签 标签含义:页面可能发生更改的频率。此值为搜索引擎提供一般性信息,与搜索引擎抓取页面的频率不完全相关。有效值为:

    always 
    hourly 
    daily 
    weekly 
    monthly 
    yearly 
    never
    
  • <priority>

    可选标签 有效值范围从 0.0 到 1.0。标签含义: 告诉搜索引擎您认为您的那个网页最重要,从而它们对您页面的抓取可以按照您最喜欢的方式进 行排序。

  • changefreq则是指内容更新的频率。

有了这些设置,等于告诉搜索引擎机器人,网站的更新情况如何,以及希望搜索引擎优先收录哪些内容。

二、创建文件

  1. jekyll 环境变量设置为 production

    参考:https://jekyllrb.com/news/2016/10/06/jekyll-3-3-is-here/#3-siteurl-is-set-by-the-development-server

    我是容器启动的,所以增加类似的变量:

    services:
      blog:
        command: jekyll serve
        image: jekyll/jekyll:latest
        container_name: blog
        restart: always
        volumes:
          - ./:/srv/jekyll
        environment:
          JEKYLL_UID: 0
          JEKYLL_GID: 0
          JEKYLL_ENV: production
        ports:
          - '4000:4000'
    
  2. 配置文件_config.yml增加域名信息。

    参考: https://jekyllrb.com/docs/variables/

    例如我的:

    url: https://blog.kelu.org
    
  3. 在目录下创建 sitemap.xml 文件。

    以下是我根据自己个人情况的写法,供参考。

    一些变量和表达式可以参考:

    ---
    ---
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        {% for post in site.posts %}
        <url>
            <loc>{{ site.url }}{{ post.url | remove: 'index.html' }}</loc> {% if post.sitemap.lastmod %}
            <lastmod>{{ post.sitemap.lastmod | date: "%Y-%m-%d" }}</lastmod> {% elsif post.date %}
            <lastmod>{{ post.date | date_to_xmlschema }}</lastmod> {% else %}
            <lastmod>{{ site.time | date_to_xmlschema }}</lastmod> {% endif %}
            <changefreq>weekly</changefreq>
            <priority>0.7</priority>
        </url> {% endfor %}
        {% for page in site.pages %} {% if page.layout != nil %} {% if page.layout != 'redirect' %}
        <url>
            <loc>{{ site.url }}{{ page.url | remove: 'index.html' }}</loc> {% if page.sitemap.lastmod %}
            <lastmod>{{ page.sitemap.lastmod | date: "%Y-%m-%d" }}</lastmod> {% elsif page.date %}
            <lastmod>{{ page.date | date_to_xmlschema }}</lastmod> {% else %}
            <lastmod>{{ site.time | date_to_xmlschema }}</lastmod> {% endif %}
            <changefreq>weekly</changefreq> {% assign uri_array = page.url | remove: 'index.html' | split: "/" %}{% assign cata1 = uri_array.last | slice: 0,4 %} {% assign cata2 = uri_array.last | slice: -4,4 %} {% if page.url == '/' %}
            <priority>1</priority> {% elsif uri_array.size > 2 %}
            <priority>0.2</priority> {% elsif cata1 == 'page' %}
            <priority>0.1</priority> {% elsif cata2 == 'html' %}
            <priority>1</priority> {% else %}
            <priority>0.3</priority> {% endif %}
        </url> {% endif %} {% endif %} {% endfor %}
    </urlset>
    

三、生成

jekyll serve

参考链接


公文材料 张小龙:微信背后的产品观