I'd like to preview my drafts in my templates, but the views depend on posts having a category assigned. I've tried setting the front-matter defaults for the _drafts folder, but this doesn't seem to work.
How can I have default frontmatter added to my drafts when I run jekyll s -D?
Try
defaults:
-
scope:
path: ""
type: "drafts"
values:
layout: "default"
Edit: Reading jekyll's it this is supposed to work but it doesn't because drafts are merged in posts collection. So when it comes to retrieve defaults only type: posts is applied and never drafts. See Jekyll::Document.
A trick it to put your drafts in /drafts/_drafts folder.
And to set your default like this :
-
scope:
path: "drafts"
values:
layout: "default"
The only drawback is that this will add a draft category to the post.categories array.
Related
i'm kinda new to jekyll. in my project i have few .md files(each file relate to a portfolio project). some of those need to generate separate .html files when built(which jekyll already does). but i want to exclude some files from being creating separate html files.
But the important thing is, even though i want to exclude those .md files from creating seperate .html files i still want to use the front matter of those files.(as i'm using a for loop to generate a list of all the portfolio projects)
i tryied adding those .md files to the _config.yml under exclude. but that will stop parsing the front matter as well.
is there a way in jekyll which i can achieve this.
edit:
project1.md
---
title: project1
display: true
category: portfolioProjects
---
This is a test content
project2.md
---
title: project2
display: false
category: portfolioProjects
---
if i have 2 files like above, i would like to render project1.md as a .html file(as it has a content to be shown in a page) and not generate project2.md as a separate file.But i still want to access front matters of both files to make a list of projects like,
{% for project in site.projects %}
{{project.title}}
{% endfor %}
I believe i can use 2 types of collections and set output to true
for one type of collection and false to other type of collection.
But I'm wondering whether there is a much cleaner way like conditionally setting output to true or false depending on a front matter value.
eg. output : (display)? true : false
I want all the files in the post folder to use the posts.nest.sass.css stylesheet, so I tried putting that in as a default in the _config.yml file
defaults:
-
scope:
path: "_posts"
value:
layout: "post"
stylesheet: "css/posts.nest.sass.css"
Then in post.html, which is the layout file for these posts I included this in the head:
<link rel="stylesheet" href="{{site.stylesheet}}">
In the generated page there is nothing inside the href attribute. How should this be written so the stylesheet variable is passed in?
I have not been able to find documentation about how this should be done, I just guessed. Links to good places to learn about formatting in Jekyll would be appreciated.
Edit: After more fiddling, including running the file through this YAML linter, I abandoned putting this in the _config.yml file, and instead just filled in the href info directly - which up reflection is fine, since it isn't going to change with any of the posts and the layout file will fill it in. However the jekyll server says every time I save that there is invalid front-matter:
Defaults: An invalid front-matter default set was found:
{"scope"=>{"path"=>"", "type"=>"posts"}, "value"=>{"layout"=>"post"}}
I'm putting in the whole file now in case a syntax error elsewhere is the
issue, though I can't see it.
defaults:
-
scope:
path: ""
type: "pages"
values:
layout: "default"
stylesheet: "css/content.nest.sass.css"
-
scope:
path: ""
type: "posts"
value:
layout: "post"
destination: docs
exclude:
- archive
- graphics
- webplayer
- browsergame
- blend4web
- details-shim-master
- blenderstuff
- working
- LICENSE.txt
- README.md
- TODO.txt
- basetemplate.html
markdown: kramdown
url: "https://www.moonwards.com/"
Your default values are added to every posts variables, like the ones in front matter. So, the right syntax is :
<link rel="stylesheet" href="{{page.stylesheet}}">
According to Jekyll Docs, you can add categories to the permalink like so: /:categories, but what if I want to use front matter that isn't categories? For example, I have a front matter named state. I tried adding /:state to no avail.
For example: state/:state is my permalink. In my front matter I have the following:
---
state: tx
---
So then my url will be state/tx.
I realize that I can create a custom permalink in the front matter of each page, but I am looking for something automatic as I am having less savvy users update the site. Also, categories isn't an obvious indicator that a state abbreviation should go there for my content managers.
as you may have already noticed. using a default front matter for states' page can achieve that. can achieve the automatic permalink generation.
by adding the following to _config.yml:
defaults:
-
scope:
path: "_states" # states' page location
values:
permalink: /state/:categories/:slug/
and in each page, using a yaml front matter like:
---
category: tx
---
But if you want to get ride of the "category" here, and you can use local plaguing with your website (for example github page doesn't support them by default) then add a generator that run before the site is generated, looks for the state value from the yaml front mater of each page, and put it in the appropriate folder.
That "act as a placeholder", you won't see /:categories in your URLs, it says that if a post you made belonging to a category, the name of the post category will go there.
For example: having a post with the following front matter :
---
categories: mycat
---
Then that post URL will start with /mycat/....
If you want custom variables included in the front matter to be replaced in perm a links, that can't be done. You can just add custom strings to the permalinks but not variables.
I'm using a Jekyll collections to render a group of pages using:
collections:
mycoll:
output: true
permalink: /:collection/:path/
And a set of files:
_mycoll/
el1.md
el2.md
el3.md
...
But I need to avoid the generation of some pages of this collection, just like post drafts.
Is it possible?
Under jekyll 2.x, use _config.yml exclude array :
exclude:
- _mycoll/el2.md
With Jekyll 3.x, use post/collection item published front matter variable
---
title: toto
published: false
---
content
I would like to use Jekyll to create a site. not a blog. Is there a way to avoid to have the creation date specified in the url and in the page's file name?
I think that the idea behind Jekyll is brilliant, but it seems too tied to blog generation content while it could be useful also in a more general use case.
In the _config file you can change the permalink to anything you like, for example mine is
permalink: /blog/:title
As for the date you can choose your own date using the YAML front matter, again in mine i have
title: example
date: you can pick what ever date you want
What the docs say:
You configure permalinks in your _config.yml file like this:
permalink: /:categories/:year/:month/:day/:title.html
If you don’t specify any permalink setting, Jekyll uses the above pattern as the default. The permalink can also be set using a built-in permalink style:
permalink: date
Although you can specify a custom permalink pattern using template variables, Jekyll also provides the following built-in styles for convenience.
date = /:categories/:year/:month/:day/:title.html
pretty = /:categories/:year/:month/:day/:title/
ordinal = /:categories/:year/:y_day/:title.html
none = /:categories/:title.html
Source: https://jekyllrb.com/docs/permalinks/
This is the basic setting I use:
permalink: pretty
This sets pages to the pretty permalink style. Thus '/contact.md' will become '/contact/'.
How I use it for blog posts:
permalink: /blog/:title/
This makes sure the path contains the (sluggified) title.
How I use it for collections:
permalink: /desiredpath/:name/
This makes sure the path contains the filename.
If you aren't producing blog pages, you can create files in the directory structure mapping to certain URLs. Running on localhost, if your directory has the structure
- _layouts/
- config.yml
- index.html
- some_other_page.html
- some_directory/
- index.html
- some_sub_page.html
You'll have content at the following locations after jekyll has processed the files:
0.0.0.0:4000 (index.html)
0.0.0.0:4000/some_other_page.html (some_other_page.html)
0.0.0.0:4000/some_directory (some_directory/index.html)
0.0.0.0:4000/some_directory/some_sub_page.html (some_directory/some_sub_page.html)
You can also use the permalink attribute on each post to set one manually, or set a different default in config.yml Permalinks only have a small subset of variables available to use and need to be defined in every single file you want to put in a non-standard location.
This directory structure will automatically categorize your posts too. So you can have:
- some_category (defined in the yaml front matter or the _config.yml
- index.html
- _posts/
- some_post.md
- some_other_post.md
And posts will automatically have the category 'some category', and index.html will appear at 0.0.0.0:4000/some-category, with the default permalink format. The category variable is available as :category in the permalink format string.
I came across this old question while looking for a way to organize jekyll pages in a _pages directory, similarly to _posts. then access this pages without displaying the whole path in the url.
The approach that worked better for me, is to use jekyll collections as follows:
1 - Add a pages collection in _config.yml :
collections:
pages:
output: true
permalink: /:path/
2 - create a new directory named _pages (it should have the same collection name, prefixed by _)
3 - add the pages in the _pages folder, as .md or .html files starting with YAML Front Matter.
eg. /_pages/about.md will looks like:
---
layout: page
---
<!-- about page content -->
after building that, the URL of the about page will be <your-web-site>/about .
Alternatively, to display a collection name, you have to define its permalink as:
permalink: /:collection/:path/