73

目前,我在 Markdown 文件中有这一行,详细说明了命令输出:

1\. Work (00:10:00)  
    1\. Mail letter (00:05:00, Est. 00:03:00)  
      Send letter to Foo Bar  
2\. Personal (00:02:00)

但是,当我预览 Markdown 文件时,所有的空格都会被忽略。

1. 工作 (00:10:00)
1. 邮寄信件 (00:05:00, Est. 00:03:00)
寄信给 Foo Bar
2. 个人 (00:02:00)

如何保留此空白?

4

6 回答 6

92

Markdown 主要用于生成 HTML,默认情况下 HTML 会折叠空格。使用 而不是空格字符。

于 2013-03-30T17:31:55.773 回答
38

使用不间断的空格

要在 Markdown 文档中保留空格,请使用不间断空格:
“防止连续空白字符折叠成单个空格的空格字符,并防止在其位置自动换行”。

例子

在此处查看在线和可编辑的示例。

这条线在很多地方都使用了不间断的空格;他们没有倒塌。
                                   无需使用代码块。

这条线在很多地方使用了很多连续的空格;他们都崩溃了。

注意:
复制和粘贴前面的示例无法正常工作,因为有时不间断空格会在复制粘贴操作中更改为普通空格:-(。

或托盘桌

但是,如果您打算使用不间断空格来对齐文本,则更喜欢使用表格。

示例代码:

| Country  | Capital |
| -------- | --------|
| Portugal | Lisbon  |
| Spain    | Madrid  |
| Cuba     | Havana  | 

但并不是所有的 Markdown 实现都能识别前面的语法。

如何引入非中断空间?

  • 在 macOS 中,您需要按 ⌥ Opt+Space
  • 在 Windows 中,有时工作 Alt+ 0+ 1+ 6+0Alt+ 2+ 5+5
  • 在众多商业软件中 Ctrl+Space
  • 在 Linux 中,Compose 键已启用 Compose Space Space

这个解决方案的美妙之处在于您不需要在 Markdown 文档中使用任何代码。例如,在 HTML 中,您必须使用 .

PS:
Reader, please, let us know in the comments is this method does not work in your particular markdown editor. I have tested this method in two apps and several online editors.

于 2019-01-14T00:23:54.477 回答
22

One alternative is to use

<pre></pre>

like:

<pre>

    1 
   / \ 
  2   2 
 / \ / \ 
3  4 4  3 
</pre>

the pyramid will be preserved.

Of cause, you can use &nbsp; . I use them both, depending on needs.

于 2020-03-05T12:31:17.037 回答
3

I find &nbsp; very cumbersome to use, ie. if you have large document, it may become ugly for edit, and you would need lot's of copy pasting of &nbsp;, and in the end you will also need to adjust indentation.

instead use 3 accents (```) to denote code (well, you only care about indentation and whitespace here).

for example this is how text looks without any formatting:

Enabled = "True"

Profile = ("Domain", "Private")

Direction = "OutBound"

RemotePort = ("8080", "8081")

LocalPort = ("9080", "9081")

And this is how it looks with &nbsp; doing quick copy paste

Enabled      = "True"

Profile      = ("Domain", "Private")

Direction      = "OutBound"

RemotePort      = ("8080", "8081")

LocalPort      = ("9080", "9081")

And here is my sulution, very simple, quick and effective:

Profile               = ("Domain", "Private")
Direction             = "OutBound"
RemotePort            = ("8080", "8081")
LocalPort             = ("9080", "9081")```


**EDIT:**

This last example is surrounded by 3 accents at the beginning and at the end, ex:
(```)
your text here
(```)
于 2019-12-20T20:35:19.457 回答
-1

In vscode, add the extension "Trailing Spaces." Then to exclude by syntax, go to Preferences > Settings and check Markdown>Preview:Breaks or in settings.json, add "markdown.preview.breaks": true

于 2020-02-10T21:06:56.637 回答
-5
  1. 如果它尚未退出,则Markdown.sublime-settingsPackages > User(在 mac OSX 中)中创建一个新文件。Preferences → Browse Packages...

  2. 将以下内容添加到Markdown.sublime-settings

    {  
        "trim_trailing_white_space_on_save": false  
    }

注意:您可以使用您想要创建的任何其他语言特定设置来执行相同的技巧)。

于 2013-10-09T12:34:33.000 回答