Custom permalinks for documents in Jekyll collections - jekyll

I want to add collections to my Jekyll site. In my _config.yml file I set output: true:
collections:
faq:
output: true
In my source folder I added _faq folder and created a file named 1.md in it.
When I generate the site I can see my document at http://localhost:4000/faq/1.html. But when I customize the permalink for this document and add permalink: /faq/1 in its YAML front-matter, it doesn't display at http://localhost:4000/faq/1 and downloads to the computer instead.
Is there any way to customize permalinks for documents in Jekyll collections (to show without .html extension)?

The permalink should be permalink: /faq/1/, then it works.

Permalink should be permalink: /faq/:path/
That will set the correct relative path for all files and subfolders of that collection and drop the .html suffix.

Related

Can you change the root directory in jekyll

I've been trying to change the root directory of my jekyll site to another folder but I can't seem to find a way to do this and was wondering if it's possible?
What I want is for all of my html pages to be in a folder named _pages, and for my website to load index.html from this folder. Currently I can only get this working by changing the baseurl and accessing the website by www.domain.com/pages/index.hmtl. Can I have it so the html files remain in this folder, and have jekyll render the site so that I can access index.html from www.domain.com/ ?
Thank you
In your _config.yml file you'll need to include the directory where your HTML pages live like this:
include: [_pages]
In your _pages/index.html file include a permalink in the frontmatter like this:
---
permalink: /
---
Run your Jekyll build command again. If I understand your question correctly, this will output your homepage without needing to have or adjust your baseurl.

Jekyll 3.1.0 Generates HTML files instead of folders

When I was using Jekyll 2.x, the folder structure within _blog was like this:
/blog/post1/index.html
/blog/post2/index.html
But since upgrading it's like this:
/blog/post1.html
/blog/post2.html
So although the permalink setting in _config.yml is permalink: /blog/:title, all the links I use are broken. What's going on? Is there a setting I'm missing?
Permalink config should be permalink: /blog/:title/ with a trailing slash.

Automagic Slug-like links?

I saw a ghpages site that linked to other pages without a .html extension. Is there a way to get this same link working on my local Jekyll?
Ex.
Root folder:
index.md
example.md
My Example Page
On github pages you need an index.html. With Jekyll, an index.md, generated as index.html will also work. With no index.html generated file, you will have a folder listing as a result.
Did you have some example of repository linking to files with no extensions ?
The only way to get a http://host.tld/index link with Jekyll is to set a [permalink][1] on page's front matter : eg for index.html permalink: index/ will generate your page at http://host.tld/index/index.html with http://host.tld/index/ as a shortcut.
The only problem will be that http://host.tld/ will point to nothing except a folder listing. So, you can live index.html alone and put permalinks on all other pages.

Jekyll : Using links to internal markdown files

md file with a link to Folder/file.md
When jekyll generates the index the link to the file is still folder/file.md and so doesn't connect to the generated file.html. Can jekyll replace links in markdown with their corresponding html files?
I really want to to maintain my folder structure (7 or so subfolders, each with 3 markdown files).
The answer since December 2016 is to use the jekyll-relative-links plugin.
It is a white-listed plugin if you are hosting on GitHub pages so you probably already have it.
If you are not using GitHub pages you will need the following installation instructions (from the README):
1.Add the following to your site's Gemfile:
gem 'jekyll-relative-links'
2.Add the following to your site's config file:
gems:
- jekyll-relative-links
The tag you're looking for is {% link %} and it arrived in 2016.
If you had {% link _funkyCollection/banjo.md %} it would generate the right path to the output file funkyCollection/banjo.html, or funkyCollection/banjo/index.html, or whatever, wherever it ends up being.
This is what I did to solve this problem with jekyll 3.8.5.
For link in root directory: /file.md
---
layout: page
title: Root File
permalink: /file/
---
This file is in root directory.
For link in subdirectory: /folder/file.md
---
layout: page
title: SubDir File
permalink: /folder/file/
---
This file is in sub-directory.
Now to link these file:
[Root File]({{site.baseurl}}/file/))
[Sub Dir File]({{site.baseurl}}/folder/file/)
Hope this helps someone.
i've written a simple plugin to solve this. put this in _plugins/, and make links refer to the *.md files (so github rendering linking works); if you build it with jekyll (when you are able to run plugins) the links are changed to *.html. since github doesn't run plugins, this isn't applied.
module ChangeLocalMdLinksToHtml
class Generator < Jekyll::Generator
def generate(site)
site.pages.each { |p| rewrite_links(site, p) }
end
def rewrite_links(site, page)
page.content = page.content.gsub(/(\[[^\]]*\]\([^:\)]*)\.md\)/, '\1.html)')
end
end
end
it's not perfect (i'm sure you could fool the regex) but it has been good enough for my purpose.
A Folder/file.md page will result in creation of a _site/Folder/file.html page.
So when your link to this page it's [Link to page]({{site.baseurl}}/Folder/file.html) not [Link to page]({{site.baseurl}}/Folder/file.md).
Jekyll will never rewrite file.md to file.html in url. So you have to set your links targets yourself to the resulting page.url which is usually a html file but can be css, je, json, ...
If you use permalink: /folder/folder/ in any file.md, it will generate a /folder/folder/index.html file which can be reached with [Link to page]({{site.baseurl}}/folder/folder/)
I've run into this and written a basic Jekyll/Kramdown plugin. It's less likely to break than the regular expression approach.
As long as your link doesn't start with http:// or something like it, and ends with .md, it will convert links to their lowercased and hyphenated names.
Of course, you could always modify the behavior to fit your needs.

How can I change the blog post URL of a Jekyll website?

I'm using Jekyll to create a blog/website. I have it setup so that it displays the URLs like this:
http://example.com/blog/title-of-post
I want to modify it to change the the "blog" part of the URL. For example:
http://example.com/writing/title-of-post
How can I do this?
You can do this by setting the permalink in your _config.yml file. For example:
permalink: /writing/:title
Note that this assumes that jekyll is building your entire site and isn't just powering the "blog" directory. If jekyll is only writing to your "blog" directory, then all you would need to do is rename that to "writing".