一、简介

Sitemap是网站上所有URL的列表,其中包含一些有关它们的信息。站点地图的作用是使搜索引擎更容易理解和索引网站的内容。

jekyll-sitemap插件可以为jekyll站点生成站点地图。

二、安装

  • 在Gemfile中添加gem 'jekyll-sitemap'

  • 运行bundle命令

  • _config.yml文件中添加以下内容:

url: "https://example.com" # the base hostname & protocol for your site
plugins:
  - jekyll-sitemap

三、使用

  • 配置

可以通过在页面中添加以下配置实现从sitemap中排除该页面:

sitemap: false
  • Sitemap文件

运行jekyll server,生成在_site目录下的sitemap.xml文件内容类似如下结构:

<?xml version="1.0" encoding="UTF-8"?>
<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"
>
    <url>
        <loc>http://localhost:8080/2018/02/17/welcome-01.html</loc>
        <lastmod>2018-02-17T00:00:00+08:00</lastmod>
    </url>
    <url>
        <loc>http://localhost:8080/2018/02/17/welcome-02.html</loc>
        <lastmod>2018-02-17T00:00:00+08:00</lastmod>
    </url>
    <url>
        <loc>http://localhost:8080/2018/02/17/welcome-03.html</loc>
        <lastmod>2018-02-17T00:00:00+08:00</lastmod>
    </url>
    <url>
        <loc>http://localhost:8080/2018/02/17/welcome-04.html</loc>
        <lastmod>2018-02-17T00:00:00+08:00</lastmod>
    </url>
    <url>
        <loc>http://localhost:8080/2018/02/17/welcome-05.html</loc>
        <lastmod>2018-02-17T00:00:00+08:00</lastmod>
    </url>
    <url>
        <loc>http://localhost:8080/2018/02/21/test-tags.html</loc>
        <lastmod>2018-02-21T23:00:55+08:00</lastmod>
    </url>
    <url>
        <loc>http://localhost:8080/2018/02/26/classical-books.html</loc>
        <lastmod>2018-02-26T00:00:00+08:00</lastmod>
    </url>
    <url> <loc>http://localhost:8080/about/</loc> </url>
    <url> <loc>http://localhost:8080/</loc> </url>
    <url> <loc>http://localhost:8080/search/</loc> </url>
</urlset>
  • 自定义sitemap

如果不使用此插件,也可以通过类似以下的代码来生成sitemap中的内容(下面代码省略了页面头部信息---和for、if语句前后的标签):

<?xml version="1.0" encoding="UTF-8"?>
<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">
    for post in site.posts
    <url>
        <loc>http://www.albertbamboo.cn:80</loc>
    </url>
    endfor 

    for page in site.pages
    if page.layout != nil
    if page.layout != 'feed' 
    <url>
        <loc>http://www.albertbamboo.cn:80/jekyll/docs/2018/03/03/jekyll-sitemap.html</loc>
    </url>
    endif
    endif 
    endfor
</urlset>

通过这种方法,将可以完全控制自己的站点地图,可以排除觉得不重要的任何内容或者包含希望其包含的链接。

参考资料