vmg / redcarpet

The safe Markdown parser, reloaded.
MIT License
4.99k stars 526 forks source link

Optional Reference-Style Link Space #610

Open deckar01 opened 7 years ago

deckar01 commented 7 years ago

Markdown allows a space between the square brackets ] [ of a reference style link, but that behavior creates ambiguity and interference with extensions like task lists.


Reference

You can optionally use a space to separate the sets of brackets:

This is [an example] [id] reference-style link.

http://daringfireball.net/projects/markdown/syntax#link


Ambiguity

[red] [blue]

[red]: /red
[blue]: /blue

Red Carpet renders this as:

<a href="/blue">red</a>

but, other markdown engines (like GitHub Flavored Markdown) have disabled the optional space and render it as:

<a href="/red">red</a> <a href="/blue">blue</a>

Interference

 - [ ] [bug] test
 - [x] [bug] test

[bug]: /bug

Extensions like task lists work by filtering list items looking for lines starting with [ /x/X], but Red Carpet will have already transformed them into links:

<ul>
  <li><a href="/bug"> </a> test</li>
  <li><a href="/bug">x</a> test</li>
</ul>

If Red Carpet had a way to disable the optional space the ambiguity would be resolved.

<ul>
  <li>[ ] <a href="/bug">bug</a> test</li>
  <li>[x] <a href="/bug">bug</a> test</li>
</ul>

Proposal

Add an option named reference_space that defaults to true, that determines if the optional space is recognized.

deckar01 commented 7 years ago
/* skip any amount of whitespace or newline */
/* (this is much more laxist than original markdown syntax) */
while (i < size && _isspace(data[i]))
    i++;

https://github.com/vmg/redcarpet/blob/8db31cb/ext/redcarpet/markdown.c#L1068-L1071

Wrapping this in a conditional should achieve the desired behavior.

deckar01 commented 7 years ago

I am closing this since the space is explicitly included in the markdown spec. It should be possible to fix the link after redcarpet gets done mangling it.

deckar01 commented 7 years ago

@robin850 Reopening per https://github.com/vmg/redcarpet/issues/471#issuecomment-299321564. This causes problems with normal markdown usage and popular extensions like task lists. Would you consider the option to disable the space inside links, or even disable by default it in a future major version?

deckar01 commented 7 years ago

@robin850 The [reference][] solution from https://github.com/vmg/redcarpet/issues/471#issuecomment-299321564 does not help with task lists.

 - [ ] [blue][]

[blue]: /blue

[ ] [blue] takes priority over [blue][] resulting in:

<ul>
  <li><a href="/blue"> </a][]</li>
</ul>
robin850 commented 7 years ago

Hi @deckar01 !

Yes unfortunately ! I don't know what we can do here ; I would love to try to avoid as much as possible the option path, there are still enough options to enable. GitHub Flavored Markdown is actually a super set of CommonMark and the latter explicitly forbids spaces:

A full reference link consists of a link text immediately followed by a link label that matches a link reference definition elsewhere in the document.

This is why this is not a problem in their case. I will try to think about a possible solution but I guess your best option here for now is to escape the checkbox brackets with a slash ; sorry about that. 😕

deckar01 commented 7 years ago

Thanks for your insight.