ChatGPT解决这个技术问题 Extra ChatGPT

How to insert a line break <br> in markdown

I'm trying to create a Markdown file with some paragraphs containing both a link and a line of text on the next line. The problem I've encountered is that when I make a new line after the link, it is rendered with a separate <p> tag.

My Markdown is the following:

[Name of link](url)

My line of text

Which is rendered to the following HTML:

<p>
   <a href="url">Name of link</a>
</p>
<p>My line of text</p>

Instead I want it to render like so:

<p>
    <a href="url">Name of link</a><br>  // not necessarily with a <br> tag but on a separate line
    My line of text
</p>

I've also tried using a single line break in the Markdown:

[Name of link](url)
My line of text

But then both the link and the text is rendered on the same line, but without a line break.

Any suggestions on how to solve this?

try adding 2 spaces after [Name of link](url)<space><space>
spec.commonmark.org/0.30/#hard-line-breaks - but whether your markdown implementation support this, is unknown

A
Avatar

Try adding 2 spaces (or a backslash \) after the first line:

[Name of link](url)
My line of text\

Visually:

[Name of link](url)<space><space>
My line of text\

Output:

<p><a href="url">Name of link</a><br>
My line of text<br></p>

Alternatively you can place a <br> directly into the text. It is valid in Markdown.


I'm so glad I found this answer. Is there an official Markdown documentation with information like this included?
This is great, but with one small issue - having an editor set to remove excess whitespace ;)
@TimMalone FWIW, some editors (well, UltraEdit at least) support settings on a per-filetype basis, possibly you could disable the trimming for .md files.
Adding a backslash at the end of the line does the same.
This is cool, but in visual studio code i can't filter trim trailing whitespace by extension. So, i can't disable trim whitespace feature only for markdown. Nice but i prefer using the <br> tag.
S
Scott Giminiani

I know this post is about adding a single line break but I thought I would mention that you can create multiple line breaks with the backslash (\) character:

Hello
\
\
\
World!

This would result in 3 new lines after "Hello". To clarify, that would mean 2 empty lines between "Hello" and "World!". It would display like this:

World!

Personally I find this cleaner for a large number of line breaks compared to using <br>.

Note that backslashes are not recommended for compatibility reasons. So this may not be supported by your Markdown parser but it's handy when it is.


This is the only solution that I've tried that works in RichText component of Strapi.
I
Ilia Popivanov

After a long search, I found this solution:

\
&nbsp;
\
&nbsp;

This will produce:




This doesn't really answer the OP's question.
Please add some explanation to your answer such that others can learn from it
G
Gangula

Combining answers from multiple sources, there are mainly 3 ways to add a line break in Markdown:

1. Backslash (\)

Add a backslash at the end of a line like this:

Markdown Input HTML Output HTML Preview Test line\ Test line 2

Test line


Test Line 2

Test line Test line 2

2. HTML
tag

Lot of HTML Tags are directly supported in markdown. Add a HTML <br> or ` tag at the end of a line like this:

Markdown Input HTML Output HTML Preview Test line
Test line 2

Test line


Test Line 2

Test line Test line 2

3. Two spaces

Add 2 spaces at the end of a line like this:

Markdown Input HTML Output HTML Preview Test line Test line 2

Test line


Test Line 2

Test line Test line 2

Option3.2 for whitespace - if you would like to be able to see the spaces while editing markdown source, add \s instead of whitespace( )

Note: Option3.2 doesn't seem to work properly, but this is documented in the markdown guide

Test line\s\s
Test line 2

I prefer Option1 (backslash) & Option2 (HTML <br>), since the backslash & <br> characters are clearly visible in the source format as well
C
Chukwu3meka

Just adding a new line worked for me if you're to store the markdown in a JavaScript variable. like so

let markdown = `
    1. Apple
    2. Mango
     this is juicy
    3. Orange
`

This is working because you prefixed every line with four spaces which creates a preformatted section. So I guess that is one way to do it.