Jekyll - HTML 中不呈现带换行的 Markdown
Jekyll - Markdown with line feed is not rendered in HTML
Jekyll 3.0.1
未呈现单个换行符(换行符)。它忽略了它。令人惊讶的是,它正在按原样渲染双行。我在 Ubuntu 16.04
上使用 Jekyll
。有人可以帮我解决这个问题吗?
输入
已渲染
您在寻找 this 吗?
When you do want to insert a <br />
break tag using Markdown, you end a line with two or more spaces, then type return.
此来源
This line ends with two spaces
And this follows immediately
应该渲染到
这一行以两个空格结束
紧随其后的是
好问题。有(一如既往)多种方法可以做到这一点。每种解决方案都有其优点和缺点。选择最适合您的风格或项目的一款。
解决方案 1:换行到 br
这是迄今为止最优雅的解决方案。它就像 PHP 一样工作。你可以这样写:
<div id="content">{{ content | newline_to_br }}</div>
并添加此 CSS:
#content br {display: none;}
#content p br {display: inline;}
它会稍微污染您的输出(在段落标记之外有未呈现的中断),但它确实可以满足您的要求。它易于阅读,不需要对您的 content/markdown.
进行任何更改
方案二:每行后两个空格
一行末尾的两个空格在输出中被替换为 <br>
。我不喜欢在代码编辑器中很难看到这些空格,但这是真正的降价方式。 Source
解决方案 3:手动添加 HTML 中断
由于markdown中允许HTML,你可以手动写HTML个断点,像这样:<br>
。这些中断写起来很麻烦,它们会用 HTML 污染你的降价,但它们有效。
这是我肮脏的解决方法。
我将此代码移至单独的帮助程序 include 以在使用时提供更好的可读性。
辅助文件。 _includes/linebreaks.html
:
{% capture devnull %}
{% assign parts = include.input | newline_to_br | strip_newlines | split:"<br />" %}
{% capture output %}{% for item in parts %}{% assign item = item | strip %}{%
if item.size > 0 %}{{ item | append: '<br />' | replace:"</p><br />","</p>" }}{% endif %}
{% endfor %}{% endcapture %}
{% endcapture %}{{ output }}
在模板中的用法。 _layouts/default.html
:
<h1>{{ page.title }}</h1>
{% include linebreaks.html input=content %}
源降价文件。 _posts/2020-02-20-marys-lamb.md
:
---
title: Mary’s Lamb
---
Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go.
结果:
<h1>Mary’s Lamb</h1>
<p>Mary had a little lamb,<br />
Little lamb, little lamb,<br />
Mary had a little lamb<br />
Whose fleece was white as snow.</p>
<p>And everywhere that Mary went,<br />
Mary went, Mary went,<br />
Everywhere that Mary went<br />
The lamb was sure to go.</p>
如果您使用的是 Kramdown(Jekylls 默认的 Markdown 渲染器),请将以下内容添加到您的 _config.yml
:
kramdown:
hard_wrap: true
hard_wrap - Interprets line breaks literally
一个解决方案可能是:
在行尾添加两个 br
标签:
This is line one without any space at end.<br><br>This is line two.
输出:
This is line one without any space at end
This is line two
Jekyll 3.0.1
未呈现单个换行符(换行符)。它忽略了它。令人惊讶的是,它正在按原样渲染双行。我在 Ubuntu 16.04
上使用 Jekyll
。有人可以帮我解决这个问题吗?
输入
已渲染
您在寻找 this 吗?
When you do want to insert a
<br />
break tag using Markdown, you end a line with two or more spaces, then type return.
此来源
This line ends with two spaces
And this follows immediately
应该渲染到
这一行以两个空格结束
紧随其后的是
好问题。有(一如既往)多种方法可以做到这一点。每种解决方案都有其优点和缺点。选择最适合您的风格或项目的一款。
解决方案 1:换行到 br
这是迄今为止最优雅的解决方案。它就像 PHP 一样工作。你可以这样写:
<div id="content">{{ content | newline_to_br }}</div>
并添加此 CSS:
#content br {display: none;}
#content p br {display: inline;}
它会稍微污染您的输出(在段落标记之外有未呈现的中断),但它确实可以满足您的要求。它易于阅读,不需要对您的 content/markdown.
进行任何更改方案二:每行后两个空格
一行末尾的两个空格在输出中被替换为 <br>
。我不喜欢在代码编辑器中很难看到这些空格,但这是真正的降价方式。 Source
解决方案 3:手动添加 HTML 中断
由于markdown中允许HTML,你可以手动写HTML个断点,像这样:<br>
。这些中断写起来很麻烦,它们会用 HTML 污染你的降价,但它们有效。
这是我肮脏的解决方法。
我将此代码移至单独的帮助程序 include 以在使用时提供更好的可读性。
辅助文件。 _includes/linebreaks.html
:
{% capture devnull %}
{% assign parts = include.input | newline_to_br | strip_newlines | split:"<br />" %}
{% capture output %}{% for item in parts %}{% assign item = item | strip %}{%
if item.size > 0 %}{{ item | append: '<br />' | replace:"</p><br />","</p>" }}{% endif %}
{% endfor %}{% endcapture %}
{% endcapture %}{{ output }}
在模板中的用法。 _layouts/default.html
:
<h1>{{ page.title }}</h1>
{% include linebreaks.html input=content %}
源降价文件。 _posts/2020-02-20-marys-lamb.md
:
---
title: Mary’s Lamb
---
Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go.
结果:
<h1>Mary’s Lamb</h1>
<p>Mary had a little lamb,<br />
Little lamb, little lamb,<br />
Mary had a little lamb<br />
Whose fleece was white as snow.</p>
<p>And everywhere that Mary went,<br />
Mary went, Mary went,<br />
Everywhere that Mary went<br />
The lamb was sure to go.</p>
如果您使用的是 Kramdown(Jekylls 默认的 Markdown 渲染器),请将以下内容添加到您的 _config.yml
:
kramdown:
hard_wrap: true
hard_wrap - Interprets line breaks literally
一个解决方案可能是:
在行尾添加两个 br
标签:
This is line one without any space at end.<br><br>This is line two.
输出:
This is line one without any space at end
This is line two