Currently, I have this line in a Markdown file detailing command output:
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)
However, when I preview the Markdown file, all of the whitespace is disregarded.
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)
How do I preserve this whitespace?
Another option, if your Markdown processor supports HTML, is to use the HTML entity for non-breaking space ( ).
As a workaround I would suggest inserting a vertical bar (|) followed by hard spaces (Alt-Code on Windows: Alt+0160). This preserves the indent after the bar resulting in a visually acceptable solution for raw and rendered Markdown. This is a normal line of text. | This is an indented line of text.
Mostly, Markdown is just regular text with a few non-alphabetic characters thrown in, like # or *. Markdown coverts text with four leading spaces into a code block; with GFM you can wrap your code with ``` to create a code block without the leading spaces.
Markdown is used primarily to generate HTML, and HTML collapses white spaces by default. Use
instead of space characters.
To preserve spaces in a markdown document use a non-breaking space:
"a space character that prevents consecutive whitespace characters from collapsing into a single space, and also prevents an automatic line break at its position".
See an online and editable example here.
This line uses non-breaking spaces in many places; they are not collapsed.
There is no need to use code blocks.
This line uses many consecutive spaces in many places; they are all collapsed.
Note:
Copy and paste the previous example could not work because sometimes the non-breaking spaces are changed to normal spaces in a copy-paste operation :‑(.
However, if you intend to use non-breaking spaces to align text, prefer to use tables.
Example code:
| Country | Capital |
| -------- | --------|
| Portugal | Lisbon |
| Spain | Madrid |
| Cuba | Havana |
But not all Markdown implementations recognize the previous syntax.
The beauty of this solution is that you don't need to use any code in your Markdown document. For example, in HTML you must use
.
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.
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
. I use them both, depending on needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With