4ae1e1
4ae1e1

Reputation: 7634

break long lines in markdown code

I'm using the original-flavored markdown as described here.

I'm wondering if it's possible to break a long line in markdown code while resulting in no syntactic effect. (In other languages, for instance shell scripts and C, \ would be used to continue onto the next line.) I'm asking this because newlines sometimes break the syntax, as in

[StackOverflow]
(http://stackoverflow.com)

This would end up as "[StackOverflow] (http://stackoverflow.com)" in the actual html rather than a hyperlink, since newline is interpreted as whitespace, and whitespace is not allowed between ] and ( in the [text](url) syntax for links.

Upvotes: 62

Views: 32775

Answers (2)

kizu
kizu

Reputation: 43224

If you'd like to have line breaks in the code, then, as comments tell, use the line breaks inside either the text part, or inside the url part: you can actually do this

[StackOverflow](
http://stackoverflow.com)

And while the result would have the extra whitespace inside the href, it would still work, so no worries. This works also for spreading long links over multiple lines

[StackOverflow interesting discussion](
    http://stackoverflow.com/this/discussion/has/a/very/
very/very/very/long/long/long/title/title/title

Be sure to not let any leading whitespace on lines that start within the URL (here the last line). (only the first line break works with GitHub-flavored markdown, because GitHub treats the line breaks as whitespace).

However, if the goal is to make links more readable, I would recommend to use the link references, so you could place the actual hrefs anywhere in your document and have short and readable link titles in the text:

[StackOverflow][]

[StackOverflow]: http://stackoverflow.com

or

[StackOverflow][1]

[1]: http://stackoverflow.com

Note that you can place the reference anywhere: it is often rather readable and easy to maintain when all the references are placed at the bottom of the readme.

Also, this method would allow you to add title attribute to links, if you'd need them:

[StackOverflow][]

[StackOverflow]: http://stackoverflow.com (Here is this link's title!)

Upvotes: 59

OregonTrail
OregonTrail

Reputation: 9039

Adding on to @kizu's answer, another type of link is a "shortcut reference link", which is even fewer characters, and in my opinion, looks nicer.

They look like this:

Here's a link to [Stack Overflow]. You should be able to find an
answer there. If not, let me know.

[Stack Overflow]: https://stackoverflow.com

For additional information, check out [this answer].

[this answer]:
https://stackoverflow.com/a/59956334/384954

renders as follows

Here's a link to Stack Overflow. You should be able to find an answer there. If not, let me know.

For additional information, check out this answer.

Upvotes: 13

Related Questions