Jekyll include markdown file not rendering html - html

I have a Jekyll site with an include file. Within that include file I am trying to include another file:
<div class="api-doc api-off api-definition" id="debugging">{% include_relative _apiDocs/debugging.md %}</div>
but this displays the markdown as a string:
--- title: API Reference | Debugging --- #### Debugging Turn on debugging: ```Javascript pbjs.setConfig({ debug: true }); ```
Is there a way to get this to render as HTML?

If you're using Kramdown as your processor, you can handle this by adding the markdown="1" attribute to the parent div:
<div class="api-doc api-off api-definition" id="debugging" markdown="1">{% include_relative _apiDocs/debugging.md %}</div>
If not, you can apply a filter on the included Markdown to convert it to HTML (see the Jekyll docs on the markdownify filter):
{%- capture debugging-doc -%}{% include_relative _apiDocs/debugging.md %}{%- endcapture -%}
<div class="api-doc api-off api-definition" id="debugging">{{ debugging-doc | markdownify }}</div>

Related

Include another page with Jekyll without displaying Front Matter

I'm trying to clone the output of index.html in another page, thanks.html. It's working, but no matter what I try I end up with --- layout: default title: example --- rendered (from index.html's Front Matter) where the file starts.
I've tried:
---
layout: default
thanks: true
---
{% capture main %}{% include_relative /index.html %}{% endcapture %}
{{ main }}
And:
---
layout: default
thanks: true
---
{% include_relative /index.html %} <!-- Same problem with just 'include' -->
But I get the same result.
If I drop the line layout: default it works, but as expected I don't have a header or footer anymore. I can also get it to work using includes but this makes for a lot of duplication for a page with a single line that's different.
In case it's relevant, I'm running Jekyll version 4.2.2 via docker, and there are no plugins set in my _config.yml.
A fragile hack:
{% capture source %}{% include_relative included.html %}{% endcapture %}
{{ source | split: "---" | last }}
Just don’t use --- anywhere in the included document. :)

Q: Jekyll - 2 sections displaying nearly same content

I have two very similar sections on a jekyll website.
The displayed content change for only some words or resources.
I handle this using 3 files:
One markdown content without Front Matter. This file content the text with if conditions:
# Mardown Content
Here is more info about:
{% if page.section == 'sectionA' %}
[Something About Setion A](/sectionA/something)
{% elsif page.section == "sectionB" %}
[Something About Setion B](/sectionB/somethingelse)
{% endif %}
Two markdown files with front matter including the content
---
layout: myTemplate
section: sectionA/B
title: something
---
{% include content.md %}
I used to have those 3 files in the same directory using {% include_relative content.md %}. This way seems better because the files were in the same directory and the _include folder do not mix up content and html templates.
But my config of jekyll builds also a page for the content file displaying an html page.
Is there a way to prevent serving this html content?
Do you know a better way to handle this?
In _config.yml add :
exclude:
- content.md
This will instruct Jekyll not to process content.md.
Note : I don't get why you cannot put content.md in the _includes folder.

I wanna include html file on post! But Liquid say "Could not locate the included file."

I wanna include some HTML file on post!
I wanna show a reference on the right side of my blog.
Reference is HTML file, and it looks like simple doc.
So I created the layout file jekyll.html and I wrote a tag <div>.
Then I added a header variable to my post and reference file is located on _layouts.
This is the code in jekyll.html:
<div class="right">
{% for ref in page.refs %}
{% include ref %}
{% endfor %}
</div>
And this code post's YAML header.
---
layout: jekyll
title: "02. "
date: 2018-12-30 14:38:42 +0900
category: Jekyll
refs: [ bundler.html, test.html ]
---
So, Liquid says:
Liquid Exception: Could not locate the included file 'ref' in any of ["E:/Projects/Jekyll/_includes"]. Ensure it exists in one of those directories and, if it is a symlink, does not point outside your site source. in /_layouts/jekyll.html
What's wrong? Did I do anything wrong?
Or is there another way?
If you want to use a liquid variable in an include tag, you must surround it with curly braces :
{% include {{ ref }} %}

How to render two files from a single markdown

I have a collection of markdown files and I would like to render in two different set of files using two different layout files when building the page:
– html files for the website
– xml files
Is it possible to do this? It seems that a markdown file can be only parsed once. Any suggestions? Many thanks
Instead of having a markdown file that points to a layout file, you're going to need two pages that point to the markdown post file.
There are a number of ways to point to a post file. I like to use a where filter:
{% assign post = site.posts | where:"url", include.url | first %}
Now that you have a post, let's say you have fileOne.html that you want to put that post into. Here's how you'd approach that:
---
layout:main
---
<h1>Some html here</h1>
{% assign post = site.posts | where:"url", include.url | first %}
{{ post.content }}
Then you could do the exact same thing in another file named fileTwo.html.
Browse markdown files from files using each layout and use the liquid filter markdownify to process the same content in different contexts.
Markdownify
Convert a Markdown-formatted string into HTML.
{{ page.content | markdownify }}
You could use Includes for this.
Note: You can either store your source Markdown files in the _includes folder and include them into others with include.
Or you store them directly in the folder where the destination files are and use include_relative.
I'm showing the first option here.
This is the file whose content will end up in another file:
/_includes/foo.md:
**bold**
[Stack Overflow](https://stackoverflow.com)
And here's the file which includes foo.md:
a) When it's a Markdown file:
---
layout: default
title: blah
---
This is the "destination file" for the include.
{% include foo.md %}
b) Anything that's not Markdown:
(if you want the Markdown from foo.md rendered, you have to do this manually)
---
layout: default
title: blah
---
<p>This is the "destination file" for the include.</p>
{% capture tmp %}{% include foo.md %}{% endcapture %}
{{ tmp | markdownify }}
Result in both cases:
<p>This is the "destination file" for the include.</p>
<p><strong>bold</strong></p>
<p>Stack Overflow</p>

Postprocessing HTML in jekyll

Is it possible to add a post-processing step (in ruby) to run in Jekyll after it converts markup to HTML?
I'd like to add some html content, and can't see a way to do that in Jekyll files in general (though certain dialects of markup might support it), so I think it would have to be done by operating on the HTML after Jekyll converts it and before it writes it into _site/.
EDIT: Clarified that I'm looking to do this in Ruby and in arbitrary dialects of markup.
It looks like I may be able to do this by providing a Liquid filter that postprocess the html content, and changing {{ content }} to {{ content | my_postprocess }} in _layouts/post.html and _layouts/page.html.
Indeed, kramdown will not parse markdown in html element by default.
But, there is some configuration parameters that can be set to force kramdown to parse markdown in span or block elements.
Kramdown parameters in Jekyll documentation (look under the kramdown: key) but more interesting things in the kramdown documentation particularly here and here
In configuration
If you want to globally parse markdown in html, in _config.yml, add :
kramdown:
parse_block_html: true
parse_span_html: true
Or, in your markdown itself
{::options parse_block_html="true" /}
{::options parse_span_html="true" /}
<div>
## Some markdown here
**bold** and `code`
<cite>a **span** level element</cite>
</div>
You can also use markdown includes like this :
{% capture md %}{% include markdown_file.md %}{% endcapture %}
{{ md | markdownify }}
This will render any markdown as if it was in the original post/page.
Newer versions of Jekyll let you use hooks to do post-processing (and many other things).
For example, you could put a file like this in the _plugins/ directory, and it will modify the contents of posts after they've been converted to HTML but before they've been embedded in a layout file or written to disk:
Jekyll::Hooks.register :posts, :post_convert do |post|
post.content = post.content.gsub('old', 'new')
end