风格雨果诗

问题描述 投票:0回答:3

我想和hugo一起展示诗歌。

内容文件:

So some text
with some rhyme

and another strophe

我想用这些换行符准确地显示它。


仅使用

.Content
会发生什么?同一节内的行没有换行符。


在那里换行会发生什么?

{{ (replace .Content "\n" "<br/>") | safeHTML}}
?节号之间有四个换行符。


现在我很困惑。我想知道找回原来的换行符有那么难。

事实上,我正在与 Markdown 处理器作斗争。我不能只是说:“不,不要降价这个吗?”

markdown hugo
3个回答
2
投票

从 Hugo 0.60.0 开始,使用 CommonMark 兼容的 Goldmark 库来渲染 Markdown。如果您想呈现硬换行,您可以在行尾使用两个或多个空格,或反斜杠。

输入:

So some text\
with some rhyme

and another strophe\
enter code here

输出:

<p>So some text<br>with some rhyme</p>
<p>and another strophe<br>enter code here</p>

1
投票

鉴于此答案也使用 Markdown,这是您可以做到的一种方法:

<p>So some text<br>
with some rhyme<br>
<br>
and another strophe<br>
enter code here</p>

您可以将通用 HTML 放入 Markdown 文档中,它会按原样呈现:

一些文字
有一些押韵

和另一个节节
在此输入代码


1
投票

有多种解决方案。您可以使用原生 Markdown 换行符、原始 HTML 或代码块。

Markdown 换行符

最简单的解决方案是使用原生 Markdown linebreaks(行尾的两个空格生成换行符)。

在以下示例中,出于演示目的,空格已替换为点:

So·some·text··
with·some·rhyme

and·another·strophe··
enter·code·here

生成以下 HTML(几乎与其他答案建议的原始 HTML 相同)

<p>So some text<br>
with some rhyme</p>
<p>and another strophe<br>
enter code here</p>

并渲染为

一些文字
有一些押韵

还有另一个节节
在此输入代码

当然,除非您的编辑器可以选择显示空格,否则您实际上无法看到那些尾随空格,并且编辑需要包含它们的文档可能会很烦人。

原始 HTML

在决定使用哪种 Markdown 结构来格式化某些文本之前,考虑您希望 Markdown 输出什么 HTML 通常会有所帮助。在这种情况下,查看 pre 标签很有趣。 HTML5 规范是这样描述标签的:

<pre>
元素表示预格式化文本块,其中结构由印刷约定而不是元素表示。

然后提供示例12:

下面展示了一首当代诗,它使用了 pre 元素 保留其不寻常的格式,这构成了 诗本身。

<pre>                maxling

it is with a          heart
                heavy

that i admit loss of a feline
        so           loved

a friend lost to the
        unknown
                                (night)

~cdr 11dec07</pre>

鉴于上述情况,

pre
标签似乎是标记一首诗的良好候选者。您可以根据需要设置纯文本的格式,并将其包裹在
pre
标签中,纯文本格式将保留。

当然,Markdown 没有创建不包含子

pre
标签(Markdown 代码块)的
code
标签的机制。正如原始 Markdown rules 状态:

对于 Markdown 语法未涵盖的任何标记,您只需使用 HTML 本身即可。无需在其前面或分隔符来表明您要从 Markdown 切换到 HTML;您只需使用标签即可。

鉴于上述情况,用不寻常的格式写一首诗的“正确”方法是使用原始 HTML

pre
标签。

<pre>So some text
with some rhyme

and another strophe
enter code here</pre>

呈现为:

一些文字
有一些押韵

和另一个节节
在此输入代码

代码块

但是,如果您只想使用受支持的 Markdown 语法,而不关心生成的 HTML 的语义,那么 Markdown 代码块 就可以了。虽然“

code
元素代表计算机代码片段”,但没有规则控制
code
元素的显示方式。当然,某些网站可能会对
code
元素应用一些 CSS 规则,因此您可能需要注意这一点,但是当您可以完全控制时(也许在您自己的网站上),这可能不是问题。

这是一个简单的代码块,就像原始问题中包含的代码块一样:

    So some text
    with some rhyme

    and another strophe
    enter code here

您已经知道渲染为:

So some text
with some rhyme

and another strophe
enter code here
© www.soinside.com 2019 - 2024. All rights reserved.