Jekyll Internal Post Links - jekyll

This is a pretty niche problem, but... I run a blog that runs on Jekyll and I post very regularly. To keep my editing sane I regularly archive posts, and those archived posts get a pretty strict structure. But, I go a full year before archiving.
Where this hurts is in links to other posts. I used to be able to absolutely reference the file name (per jekyll markdown internal links), but this appears to be being deprecated:
Deprecation: A call to '{% post_url 2018-09-06-peppermint %}' did not match a post using the new matching method of checking name (path-date-slug) equality. Please make sure that you change this tag to match the post's name exactly.
Now, if I have to include the full path to the file, then when I archive my posts for the year I have to parse all of the posts for the entire year and update any links between them to include the new file path for their archived location, defeating the point of using this tool at all. A direct link to the page would actually be better, given that I change my URL structure less often.
Is there a better solution for internal links that doesn't depend on the file structure, allowing a file to be moved without having to update every link to that file?
Example File structure:
_posts
-2018
-post1
-post2
-etc
-Archive
-2017
-2016
If there's no better answer, I may just have to go back to using absolute external links.

Solution 1. Use your own include
Create an post_url.html file and write this:
{% include post_url.html slug="2018-09-06-peppermint" %}
The include (called post_url.html) should find the post with the right slug and echo the link, like this:
{% assign link = site.posts | where:'slug',include.slug %}
{{ link[0].title }}
Solution 2. Search and replace
You have to parse posts? A simple search and replace on all files, looking for (/2018/ and replace with (/Archive/2018/ should do the trick (if you use markdown links). This should take just a few seconds.

Related

Jekyll include content from another collection

Hope someone can help me, I'm really stuck working with collections.
At the moment I have two setup, :guides :places.
What I'd like to do is setup a place once in the places collection and include attributes like location, website, image etc.
Then I want to be able to reference the place in the guide collection. My guide could be coffee shops so I can reference the place one and pull in all the content. A place might be in a few different guides so I'll be able to update the image of the place and automatically update all the guides.
I've tried using the following:-
{% include_relative coffee-shop.md %}
But it tries to find it within the same collection? Not sure how I can get out of the current collection and grab this from the place collection?
Any advice would be awesome!
Working with your:
collections:
places:
...
guides:
...
You'll get the matching data in site.places and site.guides. If you just want to insert the content, you'd have to do something like:
{% assign place = site.places | where: "name","Toronto" | first %}
{{ place.content }}

Jekyll: How to include specific file from collection?

I have a collection of css files I combine into one with Jekyll. This is very easy:
{% for file in site.css %}
{{ file }}
{% endfor %}
However, there is a special case where I want to include just a single one of those files. The pages.css file.
site.css is a collection, so I simply put all my css files in a folder called _css and in _config.yml added:
collections:
css:
output: false
I was able to include only the _css/pages.css files using {{ site.css | where:"title", "Pages" | first }}. But I was wondering if there is a nicer way of doing it. Something like {{ site.css/pages.css }}
Is there a way to do it like that? Wihtout a where and first clause?
For the exception I would use an if statement and not loop over the files.
However, I would consider cache while using this solution. If one pages uses 'superlargecombined.css' and another one uses 'superlargecombinedcsswithoutonepart.css' the client needs to download them both (fully). That is in not an optimization. If you keep your css files seperated they can be cached and will be downloaded once, independent of your page css configuration. Combining things that always need to stick together is a good solution though (to minimize requests), but note that with HTTP2 the need to minimize requests will reduce drastically.
Hope this helps!

How can I use macros/variables/scripts in articles with Pelican?

I have just started with Pelican. It's awesome, I just can't figure out how to use macros in my articles (and pages). I know I can use Jinja when making my own theme, but I can't seem to be able to use it in articles. I'd like to be able to define a macro/function/template/whatever that I'd put directly in the markdown of the article, possibly with parameters, and it would get expanded when the pages are generated. For example a function to generate an image with a caption of given size and css class that would also be a link directly to the image. I'd like to be able to access these macros from all articles, to be able to reuse them everywhere. Something I'd normally do with PhP.
I could probably use JS to do this, but if I'd like to avoid it and keep everything static if possible. Can this be done?
UPDATE:
I found a pelican plugin that solves this - jinja2content.
OLD SOLUTION:
I found a solution here. You can implement a filter in Python to process all texts in articles/pages like this:
Create a python file filters.py in which you write the filter function process_text to expand my macros (or generally do anything with the article/page text), for example to test the function write something like:
def process_text(input_text):
return "TEST " + input_text
In the Pelican config file (pelicanconfig.py) register this function as a possible filter to be used with Jinja:
import sys
sys.path.append('.')
import filters
JINJA_FILTERS = {'process_text':filters.process_text}
Now you have to edit the templates to apply this filter to article/page texts before adding them to output. In my case I edited two files: themes/themename/templates/article.html and themes/themename/templates/post.html and changed {{ article.content }} to {{ article.content|process_text }} and {{ page.content }} to {{ page.content|process_text }} in them to apply the filter.
Now all texts in articles and pages should be prefixed with "TEST".
The not so convenient thing about this is I have to write my own macro expander, which shouldn't be extremely hard with regular expression in Python, but if there is a nicer way to do this, feel free to post here.

Jekyll pagination on multiple pages

I am new to html/css but am attempting to create a blog using Jekyll and this theme I found here https://github.com/rosario/kasper
The homepage index.html has the all the posts in a paginated list. This is cool. However i would like to group my posts into different categories and have an additional page for each group which would have a paginated list of just posts of that groups.
I can create the additional pages but can't get the lists using any sort of variant of the code in index.html but specifying a group.
Is this possible?
There is also another way to do that. Is using Jekyll Collections.
For each collection you can have a _folder containing your markdown files. Then you can call your posts within this folder from whatever page you want.
To do so, you will need to: 1st. add your collections to your _config.yml file:
collections:
example1:
permalink: /example1/:path/
example2:
permalink: /example2/:path/
example3:
permalink: /example3/:path/
2nd. create a folder to each collection, like: _example1, _example2 and _example3
3rd. create a new html file from which you call each collection:
{% for article in site.example1 %} ... {% endfor %}
That's it! I hope to have helped. If I have, please mark this answer as useful. If you need more assistance, feel free to contact me.
You should share your code with your answer if you want a more detailed answer for your question. As far as I understood you are having trouble with creating a list of blog posts that are all same category. If this is correct then you can achieve it by using liquid for loop. If you look into the code on your index.html it has this for loop
{% for post in paginator.posts %}
If you modify it like below
{% for post in site.categories.comedy %}
Where comedy is a category name. This way we access the category within the site object and get all the posts under this category. If you place similar loops on your separate pages while changing the category names you can have different category lists on different pages. Make sure that you correctly input the category names in your post's front matter. If I succeeded in answering your question please mark the answer as correct.

Hide an entry from the TOC (table of contents) in Jekyll

I have a site that has a lot of markdown files sorted into appropriate folders.
Jekyll creates their HTML versions and the TOC (table of contents) automatically.
In the _config.yml file I can rename some folders, rearrange them (e.g., if I don't want them sorted alphabetically).
I went through their documentation (http://jekyllrb.com/docs/home/) and I did not see a way to hide a file/folder from the TOC. I hope I have missed something.
What I want is to hide some folders and files from the TOC, but keep them live so people with the correct URL can still read the articles. As to why - legacy stuff I don't want people to find by themselves, but old links must still work and I must keep the information online.
Thus, I cannot use the published: false approach in the heading of the markdown file itself, as this will bring it offline.
Here is an example of my config file:
"someFolderWithChildren":
title: "Name of my folder"
position: 10
"someFolderWithChildren/child-folder-I-want-hidden":
title: "hidden folder 1"
published: false
visible: false
noToc: true
hidden: true # these did not work (I admit to guessing in frustration a lot)
"someFolderWithChildren/another-folder-I-want-hidden":
title: "hidden folder 2"
position: 8
"someFolderWithChildren/folder-i-want-in-the-toc":
title: "some live folder"
position: 1
"someFolderWithChildren/folder-i-want-in-the-toc/child-folder-i-want live":
title: "yet another live foder"
position: 0
I really hope someone can point me in the right direction.
EDIT: to answer the comment and answer - I do not use posts, I am afraid, I am tied with other types of content. Further digging showed that the TOC tree is actually a custom JS widget and it seems I need to look into the way its data source is generated by the existing plugins. Thank you for your assistance and your time.
Well, depends what you exactly want. If you just want to have a unpaginated list of all your posts (TOC), then you a are fine and that can be done easily. But if you want a paginated list, you might be comfortable with a workaround.
Without paginator
In your index.html modify {% for post in site.posts %} to
{% assign posts = site.posts | where:"hide", "false" %}
{% for post in posts %}
...
and add a entry hide:[true,false] in the yaml-header of your posts. You can work with front matter defaults so you just have to set hide:false in the posts you want to exclude.
With paginator
First of all, up to now (Jekyll 2.5.3) and to my knowledge there is no possibility to filter the output of the paginator without using a plugin. But as far, as i remember things might change in the upcoming versions of jekyll.
I would recommend, to work with a collection of documents for the posts that should not show up. Collections are rendered like posts (if you pass the same layout to the collection), but since the paginator only works on posts your documents (posts) in a separate collection won't show up in the paginated list of posts as rendered by the paginator.
I hope, i could help...
It turned out that the site i had had a custom plugin that goes through all md files, creates a list for the TOC and serializes it to a json file that is then used by a client-side treeview widget (kendo ui, btw) for its data source. So, i ended up with a few lines of ruby code that skipped adding the folders i want hidden to that json.
While that worked for me, i see the idea in the posted answer and it is perhaps the way to go in a more oob scenario.