3 回答

TA贡献1851条经验 获得超3个赞
您可以为此使用 `` 反引号:ES6 模板字符串。
let text = `this is some text${' '} \n \n and this is a new line`

TA贡献1772条经验 获得超6个赞
您需要使用代码块或原始 HTML(见下文)。但是,首先让我们解决为什么您的尝试不起作用......
Markdown 不会在代码块和跨度内解释。否则,您将如何在 Markdown 文档中演示 Markdown 语法。因此,在您的代码跨度中忽略强制换行的双空格。此外,任何<br>标签都被假定为代码的一部分并被转义(作为<br>)以显示在跨度中。
由单个反引号分隔的代码是代码跨度,而不是代码块。代码跨度不会保留所有空格。它们遵循 HTML 的正常空格行为,将所有空格折叠为一个空格。
如果要保留所有空格,包括换行符,则应将代码放在代码块中。在代码块内,所有换行符都被保留,无需添加额外的空格。有几种方法可以表示代码块。当然,缩进块的标准方法是:
Some document text
this is some text${' '}
and this is a new line
More document text
或者,如果您使用的 Markdown 实现支持受保护的代码块(并非全部都支持),那么您可以使用三重反引号来分隔您的代码块:
Some document text
```
this is some text${' '}
and this is a new line
```
More document text
以上两个渲染相同:
一些文档文本
this is some text${' '}
and this is a new line
更多文档文本
请注意,换行符被保留。不需要特别的技巧。
综上所述,如果您确实需要一个包含换行符的代码跨度,那么它就超出了 Markdown 语法的范围。但是,与往常一样,您在 Markdown 中无法完成的任何事情都可以使用原始 HTML 完成。因此,您可以执行以下操作:
<code>this is some text${' '}<br>and this is a new line</code>
呈现为:
this is some text${' '}
and this is a new line

TA贡献1798条经验 获得超3个赞
不确定这是否正是您要查找的内容,但您可以输入换行符<br />
,让您跳过任意多行。
this is some text${'<br /><br />'}
这是一些文本${'
'}
添加回答
举报