Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate sitemap in Jekyll without plugin

I just shifted my entire blog from WordPress to Jekyll. There are still some pieces that need fixing here and there, but I am facing this problem at the moment. I am unable to generate sitemaps in Jekyll. I saw that there are a couple of plugins which can do the work for me.

Information on the site:

  • Site hosted via Github pages
  • Entire site handcoded - not using JB or Octopress

It would be great if you I could get some pointers towards how to do the required.

Note: This question is not the same as it's predecessors. I am not looking for options which use _site.

like image 782
Aniket Avatar asked Nov 24 '12 18:11

Aniket


4 Answers

As explained by John Day in this article, you can create a sitemap.xml file in the site's root with this content:

---
layout: nil
title : "Sitemap"
sitemap_exclude: y
---
<?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.production_url}}{{ post.url | remove: 'index.html' }}</loc>
    </url>
    {% endfor %}

    {% for page in site.pages %}
    {% if page.sitemap_exclude != 'y' %}
    <url>
        <loc>{{site.production_url}}{{ page.url | remove: 'index.html' }}</loc>
    </url>
    {% endif %}
    {% endfor %}
</urlset>

In _config.yml, define a variable named production_url with the site's full base URL (e.g. http://example.com).

If you want to exclude any links from the sitemap, include sitemap_exclude: y in the front matter of the page or post.

For a more advanced example refer to this article: Building a Better Sitemap.xml with Jekyll.

like image 171
Fernando Correia Avatar answered Sep 19 '22 18:09

Fernando Correia


You could create a new file in your site root called 'sitemap.xml', and within that page use liquid tags to iterate over all pages and posts extracting the necessary data. Seems like that would do the job pretty easily, assuming that you just want to generate a page which lists every page on the site in an xml format that conforms to the appropriate standard.

like image 40
heliotrope Avatar answered Sep 20 '22 18:09

heliotrope


Just came across this because my site is hosted on github-pages. It seems github pages now supports this.

Just had to add to _config.yml:

gems:
  - jekyll-sitemap

(You can optionally add to your Gemfile, but mine was already included via dependency)

(fun fact: jekyll-redirect-from is also supported by github pages)

like image 35
Mike Schroll Avatar answered Sep 18 '22 18:09

Mike Schroll


You can not automagically generate a full sitemap in jekyll without using plugins.

What you could do is make your own ruby (or else) script that generates a sitemap as JSON data and then insert it in your config.yml. From there you could access that data from jekyll and display it.

But there is no built-in way to do that just with jekyll.

like image 24
StandByUkraine Avatar answered Sep 22 '22 18:09

StandByUkraine