After using Markdown daily for many years, including all the content in this blog, I learned there was another way to do line breaks.

I had always thought that the only way to do was to either have a blank line (which is not a line break, but a paragraph break) or add the <br /> HTML tag. I avoided the latter because I think it just plain looks ugly, and erases the elegance of using markdown, formatting without tags.

Today, I learned that you can simply add two spaces at the end of the line to force a line break. When I first heard this, I thought it was one of those newfangled extended markdown syntaxes. That’s not the case, and this syntax is in John Gruber’s original syntax 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.

I must’ve completely missed that every time I’ve read the document.

My elation at learning a new thing was quickly dashed when I remembered that I hate trailing whitespace and use a trailing-whitespace pre-commit hook on every repository I manage. I like this syntax feature so much that I started considering how I would add an exception. A quick look at the hook’s source code showed that someone had already thought of that:

    # preserve trailing two-space for non-blank lines in markdown files
    if is_markdown and (not line.isspace()) and line.endswith(b'  '):
        return line[:-2].rstrip(chars) + b'  ' + eol
    return line.rstrip(chars) + eol

All I needed to do was add an argument to my .pre-commit-config.yaml file for that particular hook:

- repo:
  rev: v4.3.0
  - id: trailing-whitespace
    exclude: .gitignore
    - --markdown-linebreak-ext=md

Now, trailing whitespace is still removed, except when it is used for forcing line-breaks in markdown files.