【问题标题】:Jekyll - Markdown with line feed is not rendered in HTMLJekyll - 带有换行符的 Markdown 不会在 HTML 中呈现
【发布时间】:2018-10-11 14:19:42
【问题描述】:

Jekyll 3.0.1 未呈现单个换行符(换行符)。它忽略它。令人惊讶的是,它正在按原样进行双换行。我在Ubuntu 16.04 上使用Jekyll。有人可以帮我解决这种行为吗?

输入

渲染

【问题讨论】:

  • IIRC markdown 需要两个换行符(或手动使用<br>)才能实际显示换行符
  • 两个换行符不会呈现为一个换行符。添加
    作品,我正在寻找其他一些替代解决方案。
  • @rashok:源中的两个换行符被检测为一个新段落(在 HTML 中,标记为<p></p>),而不是一个换行符。

标签: markdown jekyll


【解决方案1】:

好问题。 (一如既往)有多种方法可以做到这一点。每种解决方案都有其优点和缺点。选择最适合您的风格或项目的一款。


解决方案 1:br 换行

这是迄今为止最优雅的解决方案。它就像 PHP 一样工作。你可以写:

<div id="content">{{ content | newline_to_br }}</div>

并添加这个 CSS:

#content br {display: none;}
#content p br {display: inline;}

它会稍微污染您的输出(在段落标签之外有未渲染的中断),但它完全符合您的要求。它易于阅读,不需要对您的内容/降价进行任何更改。


解决方案 2:每行后两个空格

一行末尾的两个空格在输出中被&lt;br&gt; 替换。我不喜欢在代码编辑器中很难看到这些空格,但这是真正的降价方式。 Source


解决方案 3:手动添加 HTML 中断

由于 Markdown 中允许使用 HTML,您可以手动编写 HTML 中断,如下所示:&lt;br&gt;。这些中断写起来很麻烦,而且它们会用 HTML 污染你的 markdown,但它们确实有效。

【讨论】:

  • 我喜欢解决方案 2,尽管它在编辑器上看起来很丑。谢谢你的回答。
  • 对于解决方案1,当你写两段时,你有一行分隔它们,但解决方案会在这两段之间创建两条新行..你有什么建议? .. 顺便说一句,解决方案 2 对我不起作用。
  • 解决方案2非常优雅。
【解决方案2】:

如果您使用的是 Kramdown(Jekylls 默认 Markdown 渲染器),请将以下内容添加到您的 _config.yml

kramdown:
  hard_wrap: true

hard_wrap - 从字面上解释换行符

Jekyll Docs

【讨论】:

    【解决方案3】:

    你在找this吗?

    当您确实想使用 Markdown 插入 &lt;br /&gt; 中断标记时,请以两个或更多空格结束一行,然后键入 return。

    此来源

    This line ends with two spaces  
    And this follows immediately
    

    应该渲染到

    此行以两个空格结尾
    紧接着就是

    【讨论】:

      【解决方案4】:

      这是我肮脏的解决方法。

      我将此代码移动到单独的帮助程序包含中,以便在使用时提供更好的可读性。

      帮助文件。 _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 %}
      

      源 Markdown 文件_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>
      

      【讨论】:

        【解决方案5】:

        对此的一种解决方案可能是: 在行尾添加两个br 标签:
        This is line one without any space at end.&lt;br&gt;&lt;br&gt;This is line two.
        输出:
        This is line one without any space at end
        This is line two

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-23
          • 1970-01-01
          • 1970-01-01
          • 2013-12-03
          • 2020-02-25
          • 1970-01-01
          • 2017-05-27
          相关资源
          最近更新 更多