gpt4 book ai didi

jekyll - 用 Liquid 去除双换行符

转载 作者:行者123 更新时间:2023-12-02 04:58:02 25 4
gpt4 key购买 nike

在 Liquid 中,您可以捕获一个变量:

{% capture header %}

<!-- My header content -->

{% endcapture %}

然后这个变量中的任何内容都可以用过滤器进行转换:

{{ header | strip_newlines }}

现在,假设您在 <head> 中有一些引用/元标记在网页上:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

{% if page.demo %} <link href="/css/demo.css" rel="stylesheet" type="text/css" media="screen">{% endif %}
<link href="/css/hello.css" rel="stylesheet" type="text/css" media="print">

如何只去除双换行符?我想要结束的是一个干净的 <head>每行一个“引用”。 demo.css 文件的“if”结构将使非演示页面的源看起来有点像这样:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">


<link href="/css/hello.css" rel="stylesheet" type="text/css" media="print">

我不希望行与行之间有多余的空格 - 在某些情况下,在较大的网站上,它可能有 10 多行空格。寻找有关如何通过过滤内容来消除此空白的建议。

最佳答案

Jekyll plugin去除空白。

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

你可以直接从它的 Github repository .所以基本上你用 {% strip %}{% endstrip %} 包装你的代码。即使这不适合您的需要,您也可以轻松更改 ruby​​ 脚本。

例如:

{% strip %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% if page.demo %} <link href="/css/demo.css" rel="stylesheet" type="text/css" media="screen">{% endif %}
<link href="/css/hello.css" rel="stylesheet" type="text/css" media="print">
{% endstrip %}

但是,请记住 Jekyll 插件的性质,您不能在 Github Pages 服务器上运行它们。

引自 documentation :

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.

关于jekyll - 用 Liquid 去除双换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17609446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com