29

I have an HTML text input <input type="text" /> that users enter a string into. Because it is a text input and not a text area, users can not enter newlines.

Users can enter Markdown elements in the text input for formatting the data when it is re-displayed later. Their options are limited, however, because they can not enter newline characters (ex. headers, lists).

Lastly, users are not allowed to enter HTML elements in the field (for protecting against XSS attacks), so they can not enter <br/> elements into the text input.

I wanted to know if there is a way in Markdown to mimic a newline in a single-line text input field that, when re-displayed as formatted text later, will display as a newline or line break.


Example (assume @cr represents this desired newline tag in Markdown):

Input: #A Header @cr ##A Subheader @cr Some text

Output:

A Header

A Subheader

Some Text

Community
  • 1
  • 1
CalMlynarczyk
  • 705
  • 2
  • 7
  • 12

4 Answers4

50

Per the Markdown documentation:

When you do want to insert a <br /> break tag using Markdown, you end a line with two or more spaces, then type return.

One  \nTwo  \nThree  \nFour

Will then render into four separate lines.

Note that between each entry and newline, there are two spaces.

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
aratno
  • 616
  • 6
  • 3
  • 1
    this worked for me with Github-style markdown, thanks! – verboze Feb 14 '18 at 16:23
  • This worked in a markdown cell inside a jupyter notebook. Thank you – parallelogram Jan 08 '19 at 18:30
  • 1
    This works inside a string rendered in `react-markdown` as well. If you need a double line break, `Line 1 \n  \nLine 2` works. – ericsoco Aug 21 '20 at 22:50
  • for some reason this did not work for me when I tried it in different markdown online editors for testing purposes like: https://dillinger.io/ and https://stackedit.io/app# any idea why? – Omri Shneor Aug 15 '22 at 09:50
15

In some markdown flavors, you may end the line with two or more spaces which will produce a newline.

nd34567s32e
  • 148
  • 1
  • 7
4

I wanted to know if there is a way in Markdown to mimic a newline in a single-line text input field ...

AFAIK, no.

There are various flavors of Markdown, of course, but there's no such feature in the original Markdown syntax.

Jason Blevins
  • 658
  • 5
  • 17
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • 2
    The markdown spec you link to specifically says this: "When you do want to insert a
    break tag using Markdown, you end a line with two or more spaces, then type return."
    – swrobel Jan 01 '20 at 02:41
-5

Add an empty code block




Depending on how mark up is rendered you can see it but it definitely has no text

ubz8a
  • 7