Drkawashima
Drkawashima

Reputation: 9822

Github markdown that respects newlines

I've got a txt file with a list that looks like this

# Actors
Robert Mitchum
John Voight

# Musicians
Geddy Lee
Angus Young

Without modifying this text, I'd like to add it to a Github Wiki page, and make it render each name on a separate rows, like this:

enter image description here

What I do NOT want is the default Github markdown behavior where single newlines are not respected and two names can render on the same row.

enter image description here

Is there any of the predefined edit modes in Github which could achieve this? Here's the list of the edit modes available

enter image description here

I like Markdown overall, but I could consider changing to some other similar markup language if Markdown simply doesn't respect newlines.

I just want to be able to write a simple list, just like in a txt file, one row per item - and let it render as one row per item

item one
item two
item three

I don't want to have to add extra input like double line-breaks or other extra characters

Upvotes: 9

Views: 15398

Answers (4)

Drkawashima
Drkawashima

Reputation: 9822

Answering my own question: None of the answers had formatting that I liked. I ended up going with textile (https://textile-lang.com/) where line-breaks are respected, just had to change the heading syntax

h1. Actors
Robert Mitchum
John Voight

h1. Musicians
Geddy Lee
Angus Young

Upvotes: 2

Tinmarino
Tinmarino

Reputation: 4051

Add 2 spaces at the end of line :
Like that :

# Actors
Robert Mitchum  
John Voight  

Actors

Robert Mitchum
John Voight

Musicians

Geddy Lee
Angus Young

Upvotes: 9

Waylan
Waylan

Reputation: 42627

The GitHub Flavored Markdown spec defines two types of line breaks:

Hard Line Breaks

A line break (not in a code span or HTML tag) that is preceded by two or more spaces and does not occur at the end of a block is parsed as a hard line break (rendered in HTML as a <br /> tag)

Soft Line Breaks

A regular line break (not in a code span or HTML tag) that is not preceded by two or more spaces or a backslash is parsed as a softbreak. (A softbreak may be rendered in HTML either as a line ending or as a space. The result will be the same in browsers. In the examples here, a line ending will be used.)

It appears that you are expecting soft line breaks to be treated as hard line breaks. However, it's important to remember that Markdown is a subset of HTML and browsers collapse whitespace characters in HTML, including tabs, spaces, newlines and others into a single space character. Therefore, while the newline is preserved in the HTML output, your browser will not show it.

Given the above, you could then use hard line breaks to force the line breaks to be preserved (a <br /> tag is inserted into the HTML). Like this (the dot is used in place of a space for demonstration purposes):

# Actors
Robert Mitchum··
John Voight

# Musicians
Geddy Lee··
Angus Young

And that gives you the following HTML:

<h1>Actors</h1>
<p>Robert Mitchum <br />
John Voight</p>
<h1>Musicians</h1>
<p>Geddy Lee <br />
Angus Young</p>

Notice the <br /> tags inserted at the appropriate location, forcing a hard line break. The above renders as:

Actors

Robert Mitchum
John Voight

Musicians

Geddy Lee
Angus Young

But you probably want separate paragraphs

Of course, that does insert the two names into the same paragraph, which is inline with what the question asked. However, if the two names should actually be in separate paragraphs, then you can simply insert a blank line between them. In fact, you really should insert a blank line between the header and the paragraph as well. Like this:

# Actors

Robert Mitchum

John Voight

# Musicians

Geddy Lee

Angus Young

Which results in this HTML:

<h1>Actors</h1>
<p>Robert Mitchum</p>
<p>John Voight</p>
<h1>Musicians</h1>
<p>Geddy Lee</p>
<p>Angus Young</p>

And renders as:

Actors

Robert Mitchum

John Voight

Musicians

Geddy Lee

Angus Young

Upvotes: 5

Geno Chen
Geno Chen

Reputation: 5214

This is something related to Markdown grammar. If you are using StackOverflow, you may notice in the real-time preview that single newline between texts can just generate a space, and double newline can generate a real newline. You may refer to GitHub Wiki: adam-p/markdown-here about line-breaks.

For the original answer, add a new line solves this issue.

# Actors
Robert Mitchum

John Voight

# Musicians
Geddy Lee

Angus Young

This produces text below:

Actors

Robert Mitchum

John Voight

Musicians

Geddy Lee

Angus Young

Upvotes: 1

Related Questions