Render markdown from a yaml multiline string in a Jekyll data file - yaml

When using Jekyll data files I would like to store a formatted description, primarily to that I can have links in it. It works with HTML.
- name: Project name
description: >
I want to include a link
That renders properly in the generated page when included with {{ project.description }}.
Can I use markdown instead of HTML? I would prefer to do this:
- name: Project name
description: >
I want to include a [link](http://foobar.com)

Turns out Liquid supports filters, but doesn't have one for processing markdown. Thankfully Jekyll adds it's own set of handy filters which includes markdownify so now I can do this:
{{ project.description | markdownify }}

Related

Folder _data in theme directory does not get the values

I'm making a Jekyll theme using the command jekyll new-theme mytheme. I want to add translations to this theme, so I created the file _data/translations.yml into the root directory. This is its content:
---
en:
post: "Post"
es:
post: "Artículo"
I get the information with this expressions:
{% assign t = site.data.translations %}
{{ t[site.lang]['post'] }}
This setup only works if I move the _data/translations.yml file to my Jekyll website directory, created through the command jekyll new mysite.
Are the data files out of themes territory? Should I place this yml file in another directory? If it's not possible to use it in the themes territory, I would like to set a default value: how can I achieve this?
Thanks in advance.
As of Jekyll 4.0, yes, data files inside a theme-gem are not read.
You'll need to use a plugin named jekyll-data (authored by me).
If you plan to publish the theme for other users, I recommend adding the plugin as a runtime dependency in the theme's gemspec. Otherwise, simply adding the plugin to the :jekyll_plugins group in the site's Gemfile will suffice.

Get Hugo's version number in YAML files in Hugo v0.55

Question
What's the right way to get Hugo's version number in locale files i18n/*.yaml under Hugo v0.55?
Background
I'm using Hugo with the theme Beautiful Hugo, which included the following deprecated syntax since v0.55:
.URL
.Hugo
.RSSLink
#2 is used in locale files i18n/*yaml to get Hugo's version number {{ .Hugo.Version }}, but this is deprecated.
As a result, my terminal showed messages similar to this when running hugo server.
Building sites … WARN 2019/04/09 10:14:55 Page's .URL is deprecated and will be removed in a future release. Use .Permalink or .RelPermalink. If what you want is the front matter URL value, use .Params.url.
WARN 2019/04/09 10:14:55 Page's .Hugo is deprecated and will be removed in a future release. Use the global hugo function.
WARN 2019/04/09 10:14:55 Page's .RSSLink is deprecated and will be removed in a future release. Use the Output Format's link, e.g. something like:
{{ with .OutputFormats.Get "RSS" }}{{ . RelPermalink }}{{ end }}.
Source: https://gist.github.com/chris-short/78582dc32f877d65eb388f832d2c1dfa
Goal
How to suppress warning #2 .Hugo? (I've already done #1 & #3).
Attempt
#peaceiris on Qiita suggests changing {{ .Hugo.Generator }} to {{ hugo.Generator }}.
Image source: linked blog post
I applied this to locales i18n/*.yaml. i.e. I replaced {{ .Hugo.Version }} with {{ hugo.Version }} in those YAML files. However, I got a function "hugo" not defined error. I tested {{ hugo.Version }} in a Go-HTML template file, and it's OK.
Error: "/home/vin100/beautifulhugo/i18n/zh-TW.yaml:1:1": failed to load translations: unable to parse translation #14 because template: 由 Hugo v{{ hugo.Version }} 提供 • 主題 Beautiful Hugo 移植自 Beautiful Jekyll:1: function "hugo" not defined
map[id:poweredBy translation:由 Hugo v{{ hugo.Version }} 提供 • 主題 Beautiful Hugo 移植自 Beautiful Jekyll]
Thanks to #bep on Hugo Discourse, I've found the soluton! Simply use {{ .Site.Hugo.Version }}.
Reference: #bep's answer on Hugo Discourse

Silverstripe template i18n translation

I'm trying to use i18n functionality in templates in Silverstripe.
In a template ss file, I have the following:
<h4 class="red-underline"><%t Namespace1.Replace "ToBeReplaced" %></h4>
And I have the following in /lang/en_US.yml:
en:
Namespace1:
Replace: 'ReplacedString'
I have also added this to my _config.php:
use SilverStripe\i18n\i18n;
i18n::set_locale('en_US');
And this in my config.yml:
SilverStripe\i18n\i18n:
common_locales:
en_US:
name: English (USA)
native: English
But the string "ToBeReplaced" is not replaced.
The documentation I can find about this, is:
https://docs.silverstripe.org/en/4/developer_guides/i18n/
Am I missing something?
The problem was that the lang folder was added to the wrong location.
It should be located in /mysite/lang/

How can I write a Jekyll plugin that modifies the `site` object?

I want to write a Jekyll plugin that modifies the site object. The site object is available throughout the Jekyll build process. In Jekyll, the most direct way to add an attribute to it is to define it in the _config.yml file. That works in some cases, but I need to take it a step further.
The reason I am asking this question is because I need to do more than I can do in the context of a YAML file. I want to add a method to the site object. I assumed I'd only need to add the method to the Site class, but it isn't working.
Non-working plugin:
module Jekyll
class Site
def foo
"foo"
end
end
end
I assumed from reading the Jekyll source and from how Ruby classes can be extended that the site object in Jekyll would have the new foo method, but it doesn't. For reference, here is the Site Class in the Jekyll source code.
This is as much a question required because of my poor ability to read the RUby code that Jekyll is constructed in as it is a question about Jekyll itself.
Maybe you can do it without a plugin. Inspired by Jekyll Bootstrap you can craft a global variable to target your assets folder.
_config.yml
# can be '' or '/repositoryName'
baseurl: /repositoryName
ASSET_PATH : /assets
_layouts/default.html
---
front mater ...
---
{% capture ASSET_PATH %}{{ site.baseurl }}{{ ASSET_PATH }}{% endcapture %}
Then call assets from post or page :
<link href="{{ ASSET_PATH }}/bootstrap/css/bootstrap.min.css" rel="stylesheet">
or
![img title]({{ ASSET_PATH }}/img/toto.jpg)

How to url encode in jekyll liquid?

I have problem that categories are not url encoded when I use german words with Umlauts (e.g. ä, ü). I tried the cgi_escape that Liquid seems to offer, but success with the following code:
<strong>Kategorien</strong><br/>
{% for category in site.categories do %}
<small>{{ category[0] }} <br/>
</small>
{% endfor %}
Can anyone help?
Using cgi_escape doesn't work correctly for categories with spaces. Links were generated as /category/the+category instead of /category/the%20category.
The solution I ended up using was from this blog post:
# _plugins/url_encode.rb
require 'liquid'
require 'uri'
# Percent encoding for URI conforming to RFC 3986.
# Ref: http://tools.ietf.org/html/rfc3986#page-12
module URLEncoding
def url_encode(url)
return URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end
end
Liquid::Template.register_filter(URLEncoding)
A plus is a literal plus anywhere but in the query portion of the URL, where it represents a space. Good URL encoding reference (archive.org mirror).
This can then be used in a layout or anywhere else:
<a href="{{ site.category_dir }}/{{ category | url_encode }}">
#Peterb, have you upgraded to the latest version of Jekyll? The current 1.0x version supports UTF-8 and handles URLs like this much better.
You can install the latest version by running this from your Terminal command line:
$ [sudo] gem install jekyll --pre
This GitHub Issue post will shed more light on the issue:
https://github.com/mojombo/jekyll/issues/960

Resources