limit jekyll posts to specified folder - jekyll

I am using Jekyll to build a web site,
I want to have some folder structure like
root
- _posts
Kinds of MD files
Index.html
- v1.0
- _posts
Kinds of MD files
Index.html
I want to build a web site, the URL like http://host/root/ points to current version document,the URL like http://host/root/v1.0 points to version 1.0 document.
In both index.html, I use code like below,
{% assign sortedCategories = site.categories | sort %}
{% for category in sortedCategories %}
<h1 class="post-title" id="{{ category[1] | replace:' ','-' | replace:'.','' }}">{{ category[1] | join: "/" | upcase }}</h1>
<ul class="post-list">
{% for posts in category %}
{% for post in posts reversed %}
{% if post.title != null%}
<li>
<h2 class="post-title" id="{{ post.id | remove_first:'/'}}">{{ post.title }}</h2>
<article class="post-content">
{{ post.content }}
</article>
</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
{% endfor %}
The issues I met is,
As both _post folder has similar posts and category, in the http://host/root/, each post is shown twice.
In URL http://host/root/, it has an additional category which contains all the posts (in both folder _posts and v1.0/posts)
The same issues for URL http://host/root/v1.0
I want to know how I can restrict the site.categories to only search specified folder? Or there is some other suggestions on this?
thanks

posts can not be limited to a particular folder as it is site wide. You can create custom collection. Suppose if you have v1, v2 and root(pointing to v2). You can use the same collection name for v2 and root.

As mentioned by #ganesshkumar, use jekyll collections for this

Related

Creating multiple blog like lists using Jekyll hydeout theme

I'm using the hydeout theme in jekyll and I am trying to have 3 sections in the side bar;
Home - A list of all posts including pagination (working already)
College - A list of all college category posts with pagination
Fun - A list of all fun category posts with pagination
So far using the theme I have the sections in the side bar and the home page is showing all posts from both categories including pagination.
However my issue is that the category layout is only setup to show post titles and I cannot get it to act as a full list of posts let alone include pagination.
category.html:
{% unless page.content == '' %}
{{ content }}
{% endunless %}
<ul class="posts-list">
{% assign category = page.category | default: page.title %}
{% for post in site.categories[category] %}
<li>
<h3>
<a href="{{ site.baseurl }}{{ post.url }}">
{{ post.title }}
<small>{{ post.date | date_to_string }}</small>
</a>
</h3>
</li>
{% endfor %}
</ul>
Here is a link to the hosted site and the code on github.
I essentially want the college and fun subsections to display posts the exact same way the home page does but only posts that belong in their given category
Any help would be greatly appreciated.

Posts not showing correctly on jekyll (multiple site) blog -- only post code

I have a personal website built with jekyll and hosted on Github pages. I am trying to add a sub-site blog within the same domain. For this, I set up a blog.md page and followed the instructions from this website: https://www.garron.me/en/blog/multi-blog-site-jekyll.html. The idea is that if I access http://<mydomain>.com it will go to my personal website, and if I go to http://<mydomain>.com/blog it will go to a different site also set up with jekyll.
My file structure is different than what they suggest in the link above. It is like this:
/personalwebsite
config.yml
index.md
(other personal website pages).md
blog.md
/_site
/_layouts
/_posts
My index.md page is completely customized, and I wrote my own layout for that website. It is a static site and everything in _posts is ignored by it. My blog.md page is also on the root folder and it changes according to _config.yml. I am trying to use Github jekyll themes for it. The theme loads, but instead of showing the posts, it shows the code:
This is what blog.md looks like:
---
layout: blog
title: the blog
permalink: blog
---
{% raw %}
{% for post in site.posts %}
{% if post.categories contains 'blog' %}
<div class="post">
<h3 class="title">{{ post.title }}</h3>
<p class="meta">Date: {{ post.date }}</p>
<div class="entry">
{{ post.content | strip_html | truncatewords: 100 }}
</div>
</div>
{% endif %}
{% endfor %}
{% endraw %}
And this is what a post looks like:
---
layout: post
title: New test
category: blog
---
This is a test post
If I remove the {% raw %} parts in blog.md, the posts show up like this:
I have already checked that my posts are in the right place, the category parameter is filled in, the dates and post filenames are properly formatted. What am I doing wrong? Jekyll does not show any error messages other than a Github metadata warning:
GitHub Metadata: No GitHub API authentication could be found. Some fields may be missing or have incorrect data
blog.md is a markdown file.
In markdown a four space indentation represents code or preformatted text.
Kramdown will wrap this code in <pre> tag, resulting on what you actualy see on your site.
If you remove your indentation (or keep it under 4 spaces), your problem is solved.
{% for post in site.posts %}
{% if post.categories contains 'blog' %}
<div class="post">
<h3 class="title">{{ post.title }}</h3>
<p class="meta">Date: {{ post.date }}</p>
<div class="entry">
{{ post.content | strip_html | truncatewords: 100 }}
</div>
</div>
{% endif %}
{% endfor %}

Sorting posts by category using Jekyll works locally but not on Github pages?

I have posts in Jekyll sorted by categories that wont display on github pages. The yaml font matter in the post has the categories set to CSS and design but don't display on the category page with the code below:
{% for post in site.categories.CSS %}
{% if post.url %}
<a id="h1a" href="{{ post.url }}">{{ post.title }}</a>
<p id="date">{{ post.author }} • {{ post.date | date: "%b %-d, %Y" }}</p>
<div id="excerpt">{{ post.excerpt }} </div>
<div id="readmore">Read More</div>
{% endif %}
{% endfor %}
It works locally, and the URL path (/css/design/2016/01/10/responsive-web-design-css-viewport.html) shows that the categories are there, but does not display in the link above. Here is my repository, the code above can be found in the css folder of the root directory.
Jekyll 3.x uses categories "as is" : CSS stays CSS.
Jekyll 2.x is down-casing categories : CSS becomes css.
So, on Github pages site.categories.CSS == nil
In order to work locally in Github pages configuration, you can follow install instructions here.

Get page variable from post variable in Jekyll?

I'm iterating over all the posts in my site like so
{% for post in site.posts %}
// code
{% endfor %}
I want to access some variable that I have stored at the page level. How can access it? I wasn't able to find anything after googling for awhile. I want to do something like
{% for post in site.posts %}
post.page.special_var
{% endfor %}
Jekyll support both post and page, so it is depend on you, which type of variable you want to access.
For example here is your post front matter.
---
layout:post
title: jekyll test
categories: jekyll
---
So in head.html, I am using this.
<title>{% if page.title %}{{ page.title }}{% endif %}</title>
I am using page to access that variable because there are too many pages like about or contact or privacy policy that does not belongs to jekyll post,so there you can't use post for example post.title to access that variable.
Now, look out these codes
{% for post in site.categories.jekyll reversed limit:10 %}
<span><a href="{{ post.url }}">{{ post.title}}<a/></span>
{% endfor %}
Here you note that, I am using loop, because I want to access same variable from multiple post, and that variable was jekyll .
Here I am using post.title but you can even use page.title, because it is globally accessible.
For fun :
I am using reversed, so you can order your post in which date you are created, older post will show at first.
I am using limit:10 because, I want to show only 10 post per page.
If you have a special_var variable defined in a post front matter you can get it like this :
{% for post in site.posts %}
<p>This is my var {{ post.special_var }}.</p>
{% endfor %}
See Jekyll documentation here.

Jekyll: code is getting highligheted unwantingly

I've got the following page:
---
layout: default
status: publish
published: true
title: Categories
author:
display_name: lucas
---
{% for category in site.categories %}
<li><a name="{{ category | first }}">{{ category | first }}</a>
<ul>
{% for posts in category %}
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
{% endfor %}
</ul>
</li>
{% endfor %}
Instead of listing the hrefs per categery, it somehow gets code highlighting:
Can anyone help me out preventing the code getting highlighted? Thanks!
As mentioned by #David Jacquel, .md files are automatically parsed as markdown, in which four space indentations are treated as preformatted code.
I followed his suggestion on my website: change all files to .html. This disabled Markdown parsing in the file. However, if you are unwilling to write in pure HTML and would still like to use Markdown, there is a workaround.
For each element that you would like Markdown to ignore, give it the attribute markdown="0".
For each element that you would like Markdown to parse, give it the attribute markdown="1".
In your case, the following is a possible implementation.
...
<div markdown="0">
{% for category in site.categories %}
<li><a name="{{ category | first }}">{{ category | first }}</a>
<ul>
{% for posts in category %}
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
{% endfor %}
</ul>
</li>
{% endfor %}
</div>
Is it a .md file ? If yes, try to change for an .html extension.
In markdown four space indentation is used to print code tag.