add subfolders to _includes directory in Jekyll - jekyll

I'm trying to add folders inside the "_includes" directory on Jekyll. It doesn't work. I've tried adding an underscore to the folder but it doesn't work either. What am I doing wrong? I'm using Jekyll 3.7.0
The most useful link I found about it was this one
Here are some screenshots:
And the stacktrace is:
Incremental build: disabled. Enable with --incremental
Generating...
Pagination: Pagination is enabled, but I couldn't find an index.html page to use as the pagination template. Skipping pagination.
Liquid Exception: Could not locate the included file 'sidebar.html' in any of [".../_includes"]. Ensure it exists in one of those directories and, if it is a symlink, does not point outside your site source. in /_layouts/page.html
jekyll 3.7.0 | Error: Could not locate the included file 'sidebar.html' in any of [".../_includes"]. Ensure it exists in one of those directories and, if it is a symlink, does not point outside your site source.
I think I'm supposed to be able to use sub-directories. What am I doing wrong? Thank you in advance!

Apparently, I found the problem. I don't know why but Jekyll is somehow able to parse comments. It detected I had commented a piece of code referencing the old sidebar.html.
As #DavidJaquel mentions in a comment below, HTML comments <!-- --> are not stopping Liquid to parse our page content, so we should be using Liquid syntax:
{% comment %}
...
{% endcomment %}

Related

Fail build if two posts resolve to the same permalink in Jekyll

In my Jekyll site I have a permalink structure like this in _config.yml:
permalink: /blog/:title:output_ext
And I have two posts (in _posts) that resolve to the same permalink silently, for example:
2020-10-15-my-post.md
2020-10-16-my-post.md
Only one is written to _site/blog/my-post.html.
Is there a way to halt the building of the site, and throw an error, if two posts resolve to the same permalink (and then, one post overwriting the other)?
The Jekyll CLI has a command called doctor that outputs any deprecation or configuration issues with your site and, among other things, it detects when two posts resolve to the same permalink.
You can run it similarly to how you build your site today:
bundle exec jekyll doctor
In the example you gave in your question, jekyll doctor will show you an error message about a conflict between two pages, similar to this:
Configuration file: /your-website/_config.yml
Jekyll Feed: Generating feed for posts
Conflict: The URL '/your-website/_site/blog/my-post.html' is the destination
for the following pages: /your-website/_posts/2020-10-15-my-post.md,
/your-website/_posts/2020-10-16-my-post.md
When jekyll doctor finds a conflict like the above, its exit code will be non-zero which you can use to fail the build.

Image does not appear if in _posts/ directory

Image does not appear in local host if saved in the _posts/ directory but appears if stored in another directory, e.g. posts/. Does anyone know why?
.md file:
![pressure plot]({{ site.baseurl }}/posts/fig/pressure-1.png) # appears
![pressure plot]({{ site.baseurl }}/_posts/fig/pressure-1.png) # doesnt appear
.html file from the .md file:
<img src="/my-awesome-site/posts/fig/pressure-1.png" alt="pressure plot" />
<img src="/my-awesome-site/_posts/fig/pressure-1.png" alt="pressure plot" />
config_yml:
baseurl: /my-awesome-site
Error message in Terminal after jekyll serve for _posts/:
ERROR `/my-awesome-site/_posts/fig/pressure-1.png' not found.
And the path in the error is actually correct (yes, the images can be found in _posts/fig/, I was testing things out by saving in different directories), but somehow the image just doesn't appear. Can anyone explain?
I dont think all these explain: OSF, OSF, OSF, Jekyll, unless I'm not understanding this whole thing.
Looking in Jekyll's documentation, _posts is one of the "reserved" directory, parsed by Jekyll for files named with the format YEAR-MONTH-DAY-title.MARKUP. Apart from the particular directories:
Every other directory and file except for those listed above [...] will be copied verbatim to the generated site.
So your image, while existing in your sources, will not be copied in the generated site (see the content of _site/), since it is in the directory _posts while not being named with the wanted format.
I ran into the same issue today. Turns out there's a useful plugin for resolving this issue called jekyll-postfiles!
Link to jekyll-postfiles
To install jekyll postiles, add the following to your GEMFILE:
source 'https://rubygems.org'
gem 'jekyll'
group :jekyll_plugins do
gem 'jekyll-postfiles'
end
Then run bundle. That's it!
Suppose you have the following structure:
_posts
|
|--> folder_a
|
|--> year-month-date-filename1.md
|--> picture.png
|
|--> folder_b
|
|--> year-month-date-filename2.md
|--> another_picture.ong
Then in filename1.md, you can invoke the image as:
<img src="picture1.png" width="700" height="500" class="center">
This is a very neat way instead of just putting pictures in a separate assets folder IMO.
Note: This plugin is not supported by Github Pages, host your site on services like https://netlify.com which support third party plugins.

Jekyll variable site.static_files is null

I have an ultra simple Jekyll site that builds without error and Jekyll variables seem to be populated as expected except site.static_files which is null, despite there being a bunch of static files.
Here is a link to the source code. And here is a link to the output.
Edit:
Github pages uses Jekyll 1.5 and static_files are only available since version 2.0.0 (see history)
Edit #2 08-24-14
Github is currently at 2.2.0, so we can use site.static_files

How can a Jekyll page access its filename?

My goal is to create links from any published jekyll page back to its location on Github.
And so, I'd need access to a page's pathname when constructing this url. However, I don't see anything in the api for templates that provides this info. I looked at the source for page, but none of the file/path attributes had values, when accessed via the template.
Update: nowadays you can use {{page.path}}.
It seems like you can construct your link just fine using the liquid code: {{ page.url }}
For instance, if you want this page: http://railsdocs.org/pages/get-involved.html to link to this page: https://github.com/dogweather/railsdocs.org/blob/gh-pages/pages/get-involved.md
Seems like you could add something like:
[source](https://github.com/dogweather/railsdocs.org/blob/gh-pages/{{page.url | replace:'.html','.md'}})
to the markdown and get the link you want.
A more general solution:
Alternatively, we could write a generator that allows a page to access its file name directly. Add this to a .rb file in your _plugins directory:
module Jekyll
class PagePathGenerator < Generator
safe true
## See post.dir and post.base for directory information.
def generate(site)
site.posts.each do |post|
post.data['path'] = post.name
end
end
end
end
and then we can reliably get the post's filename as {{ page.path }}. This solution is more robust than converting from the URL, since page names can have characters that get 'sanitized' out when converting them to URLs. (Posts would also need their date information, etc added back in). Instead, this plugin gives us direct access to a post's name.
A similar strategy could allow us to get the path to the post if that data is also needed.
I'm not sure when this was added, but page.path gives the source file's path relative to the Jekyll root directory.

Jekyll use config.yml values in the HTML

Is it possible to setup configuration values within the config.yml file and have them be printed within the HTML page within Jekyll? I would like to set the default title and description of my website within the config.yml file and have them printed out in the header of all my layouts.
Every template has a site variable, which contains the settings from _config.yml. For example, if you have something like my_setting: "string" in _config.yml, you can access the value in a template using {{ site.my_setting }}.
Note: if you are using jekyll serve, you will need to restart the process for changes to take place. Indeed, _config.yml is not reloaded with the watch option.