I created my website using Jekyll, using Beatiful-Jekyll theme to be precise.
For the syntax highlighting I used Rouge. When I don't show line numbers everything work great. When I show line numbers, sometimes the line numbers do not start from the first line of code (same at the end, they stop some lines before the end). And sometimes they are aligned with the code, sometimes not.
Here is an example where everything works fine:
works_fine
Here is an example where line numbers are aligned with line codes but first and last line numbers are missing (in another example, the first three and last three are missing).
lines_missing
And the last example, is where line numbers are not aligned with line codes:
lines_not_aligned
I believe that the problem comes from linenos. To show line numbers I use
{% highlight <language> linenos %}
<code>
{% endhighlight %}
Because I tried the following configuration in the _config.yml file:
kramdown:
input: GFM
syntax_highlighter: rouge
syntax_highlighter_opts:
css_class: 'highlight'
span:
line_numbers: false
block:
line_numbers: true
start_line: 1
And in this case, line numbers shown by default are shown correctly, but line numbers shown using {% highlight linenos %} are still bad.
default_line_numbering_without_linenos
Thanks in advance
I know this is an old post, but I had difficulties finding an answer so I'm posting my findings here.
TL;DR set minify_html to false and minify html pages with liquid below. This worked in Jekyll version 4.0.0
Edit 1.
Further investigation revealed that trailing space inside code brackets also causes similar behaviour.
text
text
the last empty line breaks the pre tags while minifying the html. This happens when the output html is captured and new line characters transformed to br and the whole html string is splitted using the br tag, hence the trailing empty line gets lost. (would probably work with a whitespace instead of just an empty line but I didn't try)
End edit 1.
I was having this exact same issue. My local "jekyll serve" server was fine but Gitlab pages showed it just like the examples in the question. This lead me to realize that I usually set "minify_html" to false while running the site locally and as it turns out the local version also broke when I set minify_html to true. I removed jekyll default minify_html completely and used the code below.
Create a new layout, doesn't matter what you call it.
paste the following code into it.
{%- capture to-compress -%}
{{ content }}
{%- endcapture -%}
{%- assign inside-code-block = false -%}
{%- capture to-remove-br -%}
{%- assign lines = to-compress | newline_to_br | split: '<br />' -%}
{%- for line in lines -%}
{%- if line contains "<code>" -%}
{%- assign inside-code-block = true -%}
{%- elsif line contains "</code>" -%}
{%- assign inside-code-block = false -%}
{%- endif -%}
{%- if inside-code-block == true -%}
{{ line }}
{%- else -%}
{{ line | lstrip }}
{%- endif -%}
{%- endfor -%}
{%- endcapture -%}
{{ to-remove-br }}
Go to your existing layouts and add the layout you just created to their front matter as follows:
layout: <name of the layout you made>
the code is my personal experiment to minify html and it seems to work. The code ignores everything inside <code> tags, so the ``` markdown syntax should work just fine.
I got the idea while trying to get https://jch.penibelst.de/ layout to work (which I wasn't able to do).
Liquid syntax help:
- https://shopify.github.io/liquid/basics/whitespace/
- https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
I just ran into this with these options:
kramdown:
syntax_highlighter_opts:
block:
line_numbers: true
I was using a code block that had an empty line at the beginning. The empty line was the issue. Because I wanted the empty line (for a post about code formatting issues) I found that simply putting a space on the line would fix the alignment issue.
Related
i want to know what this symbol | mean and when we use it
{{ entity.date|date('d-m-Y')}
Could someone explain it for me?
This is called a filter. It applies a filter to a variable or expression to the left. For example
{{ name|striptags }}
will apply a striptags filter to a name variable.
In your case a date formatting filter is applied, to make the date look according to a certain format.
The full list of builtin filters can be found here.
Filters can be chained, for example
{{ name|striptags|title }}
will apply a striptags filter to a name variable and then apply a title filter to the resulting value.
I am trying to keep newlines in a yaml file.
In a messages.en.yml file, I have.
test:|
Here is line 1
Here is line 2
And in form.html.twig:
{{'test'|trans}}
Both lines are rendered on the same line.
Here is line 1 Here is line 2
In this link, the literal style for Yaml is explained.
and in this other one, it says "Notable lacking features are: document directives, multi-line quoted messages ..."
So is it possible in Symfony Yaml to use multilines? and how?
You could use |nl2br filter
{{ 'test'|trans|nl2br }}
Or, if you are sure your translation can't get altered by any user, you could use some few html in your translation.
test:|
Here is line 1<br/>
Here is line 2<br/>
<br/><br/>
Here is line 5
Then
{{ 'test'|trans|raw }}
I am trying to use a non-breaking space character in a translation.
# messages.cs.yml
"City near river": "Město u řeky"
However, the non-breaking space character is escaped when translating in template.
{{ 'City near river'|trans }} # prints "Město u řeky"
Is there any way how to make this work?
You may try to add raw filter like:
{{ 'City near river'|trans|raw }}
This will prevent Twig from automatic escaping.
Also I think that it would be better to prevent breaking string in CSS instead of putting a there.
You can achieve that with CSS rule:
white-space:nowrap;
assigned to the element containing translated string.
This way you will get more flexible translations as you may not want to get this html entity every time you use this string.
I just found a solution using Unicode character:
# messages.cs.yml
"City near river": "Město u\xA0řeky"
I have product name, which contains only one number.
Product.name="Pack of apples (12 pcs).";
Product.price=6;
What I'm trying to do - is to put 12 to variable, to do a little math later (ex: show how many costs 1 apple).
I was tried to do following:
{{ set apple = attribute(product, 'title'~lang)|replace('/[^0-9]/', '') }}
but with no luck.
I only have access to modifying .twig files, so any workaround would be appreciated
I would not reccommend using this way, however in this circumstance, the required data could be retrieved using simple split commands.
{% set bar = "Pack of apples (12 pcs)." %}
{% set foo = bar|split('(')[1]|split(' p')[0] %}
{{ foo }}
This would lead to foo containing 12.
However again, this would not be the best way to approach this and the use of an amount variable would be more approproate.
My goal is to mask one digit from a 4-digit string. Instead of having 2451, I'd like to have 24*1.
I tried {{ my_var|replace(slice(2, 1): '*') }} but this raises the following error: The function "slice" does not exist in My:Bundle:file.html.twig.
The weirdest thing being that {{ my_var|slice(2, 1) }} works perfectly. So the function does exists.
How can I do what I want to achieve?
Many thanks.
Create Your own Twig extension - filter:
SymfonyCookbook
IMHO it would be cleanest way to do this.
slice is a filter not a function, you can try to pipe them but in your case i do not see something achievable without creating your custom twig function or filter to mask your needs: