How to use Jekyll site.pages to generate sitemap.xml - jekyll

I'm trying to use site.pages to automatically generate sitemap.xml in Jekyll (GitHub Pages), This is the sitemap.xml code I got:
---
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for page in site.pages %}
<url>
<loc>https://example.com{{ page.url | remove: 'index.html' }}</loc>
</url>
{% endfor %}
</urlset>
It's output is something similar to this:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/samplepage.html</loc>
<!--<loc>https://example.com/samplepage</loc>-->
</url>
</urlset>
My goal is to generate a sitemap.xml without the trailing .html as in the commented line. I've tried gsub (I assumed Jekyll takes Ruby syntax: Replace words in string - ruby) but it seems either doesn't change anything or remove page.url completely.
I'd appreciate if anyone can
modify the Jekyll syntax so that it generates URLs without the trailing .html.
explain the syntax of | remove: 'index.html' (which removes the URL https://example.com/index.html from the generated sitemap.xml).
I'm very unfamiliar with Jekyll so apologies if the question seems trivial.

Jekyll uses Liquid to process templates. The pipe syntax is a Liquid filter:
Filters are simple methods. The first parameter is always the output of the left side of the filter. The return value of the filter will be the new left value when the next filter is run. When there are no more filters, the template will receive the resulting string.
remove is one of the standard Liquid filters, so the Jekyll documentation does not list it.
If you have this file in your root folder, the page URL is straightforward:
samplepage.html # https://example.com/samplepage.html
Instead, if you have:
samplepage/
index.html # https://example.com/samplepage/index.html
# https://example.com/samplepage/
The page URL ends up being the folder name, and the server will automatically serve the index.html file inside if you use the second link.
site.pages will give you the first link. As you have a filter that removes index.html from your paths, you end up with extension-free URLs.

Any file in Jekyll folder is generated with his extension, except if you use permalink.
If you create a sitemap.xml file like this :
---
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for page in site.pages %}
<url>
<loc>https://example.com{{ page.url | remove: 'index.html' }}</loc>
</url>
{% endfor %}
</urlset>
It will be generated as sitemap.xml.
You can also use jekyll-sitemap wich is supported by github pages.

Related

Jekyll redirects to a 404 and does not render post.md

I want my index.md seen at
http://jtm-lis.github.io/Julien_Tremblay_McLellan/
to redirect to pages I have written in markdown
I tried implementing, post_url variable , such as documented
in order to successful redirect to a page written in markdown without success, as it points to a 404 at
[Name of Link]({% post_url 2010-07-21-name-of-post %})
http://www.jekyllrb.com/docs/liquid/tags/#linking-to-posts
At first I thought this was an error from the for loop, so I added the link manually as detailed in the documentation specifically for pages written in markdown.
index.md
# Index of all my content
[Library Carpentry Workshop July 2020]({% post_url 2020-07-27-library-carpentry-workshop-american-university-notes %})
<ul>
{% for post in site.posts %}
<li>
{{ post.title }}
</li>
{% endfor %}
</ul>
_config.yml
theme: jekyll-theme-slate
url: https://jtm-lis.github.io
baseurl: /Julien_Tremblay_McLellan #NO TRAILING SLASH
title: Julien Tremblay McLellan's Website
author: Julien Tremblay McLellan
email: jtremc#gmail.com
description: > # this means to ignore newlines until "baseurl:"
Write an awesome description for your new site here. You can edit this
line in _config.yml. It will appear in your document head meta (for
Google search results) and in your feed.xml site description.
# social links
twitter_username: jtm-lis # DO NOT include the # character, or else the build will fail!
github_username: jtm-lis # DO NOT include the # character, or else the build will fail!
show_excerpts: true # set to false to remove excerpts on the homepage
What am I doing wrong?
The problem here is that the link does not use the site.baseurl,
so instead of linking to
https://jtm-lis.github.io/Julien_Tremblay_McLellan/2020/07/27/library-carpentry-workshop-american-university-notes.html
you are linking to
https://jtm-lis.github.io/2020/07/27/library-carpentry-workshop-american-university-notes.html which does not exist.
The fix is easy though, you just need to add site.baseurl to your link as in
[Name of Link]({% post_url 2010-07-21-name-of-post | prepend: site.baseurl %})
When doing this, you may encounter an other problem on your localhost environment, since most likely your site.baseurl folder does not exist there. So it will work on your live site, but has broken links in your local environment.
To work normally on localhost just override the baseurl property when you serve it:
jekyll serve --baseurl ""
or if you work with bundler
bundler exec jekyll serve --baseurl ""

ignore a line in a markdown file with Jekyll

Is there a way to ignore a text line in a markdown document from the Jekyll engine?
On the main README.md, I have a link to my generated pages url ala,
View the [Docs as a Website](https://gitpages.mycompany.com/myrepo/) which links to our enterprise equivalent of github.io pages powered by Jekyll reading the /docs/ folder.
For obvious reasons, I would like to not show this on the pages site as the viewer is already there and it ends up in an endless loop if users were to keep clicking it.
Is there a way to have it show on the code-view readme.md but not on the rendered jekyll version?
Solution :
If you want Jekyll to skip processing lines (or even a single character) into the baked /_site/ output, use the Liquid {% comment %} tag:
{% comment %}
Character or lines for Jekyll to skip.
{% endcomment %}
Example :
Before:
Code w/o {% comment %} + HTML render
After:
Code w/ {% comment %} + HTML render
Explanation :
If a markdown.md page has Jekyll Front Matter at the top, then it will be processed by Jekyll into a markdown.html page (see Jekyll's docs for more info).
Pages processed by Jekyll can contain Liquid code (specifically Jekyll's implementation of Liquid).
Liquid features a {% comment %} tag. And it works for Jekyll.
From Liquid's documentation of the comment tag:
Comment
Allows you to leave un-rendered code inside a Liquid template. Any
text within the opening and closing comment blocks will not be
printed, and any Liquid code within will not be executed.
If Jekyll processes your markdown.md page, it will process all Liquid statements, and will totally omit the {% comment %} tag + {% endcomment %} tag + and everything in between from the output file.
The text wrapped by a {% comment %} tag does not specifically need to contain Liquid for Jekyll to exclude it. Everything inside will be omitted from the output page: e.g. <html> elements, other code, plaintext, etc.
Word of Caution :
Jekyll will still throw an error if you have improper Liquid syntax, even if it is inside a comment tag.
The following results in an error, and Jekyll will not build:
{% comment %}
Character or lines for Jekyll to skip.
{% assign abc
{% endcomment %}
To prevent this, either ensure (1) all the code inside your comment tag is valid Liquid, or (2) prevent Jekyll from evaluating the code by wrapping it inside a {% raw %} tag:
{% comment %}
{% raw %}
Character or lines for Jekyll to skip.
{% assign abc
{% endraw %}
{% endcomment %}
Then everything inside the comment will be successfully excluded from the /_site/ files Jekyll outputs.
For more information, see Liquid's documentation of the raw tag.
Alternatively:
If you just want to link from the site's GitHub repository -> to the site generated by Jekyll + GitHub Pages
Log in
Go to https://github.com/ user-or-org / repository-name
Click the "Edit" button (above "Clone or download" and below "Settings")
Add the URL to your GitHub Pages website in the 2nd text field that prompts "Website for this repository (optional)", and click "Save"
Remove the URL from your README.md

Jekyll Collection Output True

I'm using siteleaf for my jekyll site.
Here's my problem: I created a metadata field called "image" inside siteleaf cms. This will allow the site publisher to add an image.
Here's a visual - https://ibb.co/kHcHdG
With out an image the users posts won't show on the site. After creating this metadata field and uploading an image, siteleaf will then create an _uploads folder in my jekyll directory, a folder for all images.
Jekyll ignores folders beginning with an underscore, so I have to input this yaml code inside the config file to fix this. Code Below.
collections:
uploads:
title: Uploads
output: true
posts:
title: posts
output: true
Inside my _posts folder, I have a markdown file with front matter that looks like this, code below:
---
title: popcorn
date: 2017-11-06 15:33:00 Z
image: "/uploads/15023253524_589c7b137f_k-ab220c.jpg"
layout: post
---
lorum ipsum.
So far, I followed the right directions, I'm not getting any errors in the console or in jekyll. The posts will not show. I've ran into a wall. I've asked on the jekyll and siteleaf forums, no solution.
Here's a link to the repo - https://github.com/pizzapgh/kevins_site
Help would be appreciated so much, thanks.
Fixed, finally!
The solution: Go inside my index file and change the following code:
original code
{% if post.img %}
<img src={{ "/assets/img/" | prepend: site.baseurl | append: post.img
}} alt="{{post.title}}" />
{% if post.img %}
new code
{% if post.image %}
<img src={{ post.image | prepend: site.baseurl }} alt="{{post.title}}" />
{% if post.image %}
Thanks for including a link to your repo.
Here's what I did:
$ bundle install
$ bundle exec siteleaf serve
A few warnings show up in the console, but your site is able to build regardless.
I'm able to access your posts now in the web browser just fine, for example: http://localhost:4000/popcorn/
If you were not expecting this URL pattern, you can change your config file. Right now it says:
permalink: ":title/"
For info on customizing permalinks see: https://jekyllrb.com/docs/permalinks/

Dynamic Links in jekyll

currently I'm working on static website, so I'm using jekyll to generate it. To have a nice structure and fancy URLs, I use permalinks.
permalink: /impressum/
So for example the impressum.html is rendered to impressum/index.html. And in my HTML i can simply link to that file with
<a href="/impressum">
That works for me very well. But you know, I'm a programmer. What if I want for example to change the URL to /imprint/? Well, I can change the permalink without any problems. But what's about all the other links on the site? Yeah, sure, I can use the search & replace function of my editor to change the linked URLs and check the whole site with a site checker for broken links, but that's not the fancy way I want to go. That's why I tried to create some kind of global variables with the permalink.
_config.yml:
lnk_impressum: /impressum/
impressum.html
---
layout: layout
title: Your New Jekyll Site
permalink: {{ site.lnk_impressum }}
---
But that does not work. I get this error:
Generating... error: no implicit conversion of Hash into String. Use --trace to view backtrace
So what's wrong or is there a better way?
It doesn't seem to be possible to place Liquid tags inside the YAML Frontmatter or _config files, per this SO answer.
Something else you could try is based on the approach used by the documentation pages for Bootstrap, which uses a Page Variable they call slug that provides a unique, unchanging reference to each page.
For instance, if you'd like to place a link to your impressum.html page (for which the permalink could change), you can place this code on another page, such as index.html:
{% for mypage in site.pages %}
{% if mypage.slug == 'impressum' %}
Link to Impressum page
{% endif %}
{% endfor %}
Then, in the YAML frontmatter for each of your pages, place code similar to the following:
---
slug: impressum
permalink: /my-permalink-to-impressum/
---
To change your permalinks in the future, you would just make the change to the Page Variable permalink in each page. The URLs referenced in your other pages would be automatically updated by Jekyll, as long as the slug variable remains unchanged.

Can jekyll use GET parameters?

I would like to make a categories page.
{% for post in site.categories[CATEGORY_NAME] %}
<li>{{ post.title }} ({{post.date|date:"%-d %B %Y"}})</li>
{% endfor %}
Is it possible to use a page parameter to fill in CATEGORY_NAME? Then I could have one file category.html which could serve as the index page for multiple categories (i.e. category.html?name=food and category.html?name=animals.
I've found a few plugins that handle this, but it seems like overkill to require a plugin.
https://github.com/zroger/jekyll-categories
http://blog.nitrous.io/2013/08/30/using-jekyll-plugins-on-github-pages.html
Here's the most related forum post I could find.
https://groups.google.com/forum/#!topic/jekyll-rb/y-dq-63Uvy4
If I can't do this without a plug in, is there a good reason?
I think the correct answer is that Jekyll pages must be compiled to html before they are served. This is not possible if the liquid language takes a parameter.