I use GitHub Pages for my blog, and am running into a problem with Jekyll. My post.html has a block like this:

{%    for testpost in site.posts   %}
{%           four                  %}
{%          lines of               %}
{%         processing              %}
{%         goes here               %}
{%    endfor                       %}

The part in the middle doesn't matter. The important part is the end of the line which is outside of the {% %} markup, and is therefore rendered into the html. Since this is in a loop, it's putting about 1000 blank lines into the middle of by HTML page. It doesn't affect the display, but it make a View/Source troublesome.

Any ideas on how to avoid those extra blank lines?

有帮助吗?

解决方案

Since Liquid v4 (included in Jekyll from v3.5) there is a Whitespace control, which finally resolved case with blank line, white space, etc.

Link to documentation: https://shopify.github.io/liquid/basics/whitespace/

其他提示

There's a nice workaround, that I found out in https://github.com/plusjade/jekyll-bootstrap/blob/master/_includes/JB/setup, and which is compatible with github pages.

Just enclose your loop in a capture statement, and assign nil to the resulting var.

{% capture cache %}
    {% for p in site.posts %}
       do stuff here
    {% endfor %}
{% endcapture %}{% assign cache = nil %}

How about

{{ page.content | escape | strip_newlines }}

There is Jekyll plugin that strips the whitespace.

Jekyll plugins by Aucor: Plugins for eg. trimming unwanted newlines/whitespace and sorting pages by weight attribute.

You can get it directly from its Github repository. So basically you wrap your code with {% strip %}{% endstrip %}. Even if this doesn't suit you needs, you can easily change the ruby script.

For example:

{% strip %}
    {%    for testpost in site.posts   %}
    {%           four                  %}
    {%          lines of               %}
    {%         processing              %}
    {%         goes here               %}
    {%    endfor                       %}
{% endstrip %}

However, please remember the nature of Jekyll plugins, you can't run them on the Github Pages server.

Quote from Jekyll Doccumentation:

GitHub Pages is powered by Jekyll, however all Pages sites are generated using the --safe option to disable custom plugins for security reasons. Unfortunately, this means your plugins won’t work if you’re deploying to GitHub Pages.

You can still use GitHub Pages to publish your site, but you'll need to convert the site locally and push the generated static files to your GitHub repository instead of the Jekyll source files.

Actually there is a new solution for this problem which works without any plugin.

A Jekyll layout that compresses HTML. At a glance:

  • removes unnecessary whitespace;
  • removes optional end tags;
  • removes optional start tags;
  • removes comments;
  • preserves whitespace within <pre>;
  • GitHub Pages compatible;
  • ignores development environments;
  • configurable affected elements;
  • profile mode;
  • automatically tested.

http://jch.penibelst.de/

If you - for some reason - do not want to use this here is a nice article, which describes some workarounds: Compressing Liquid generated code - sylvain durand

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top