gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.37k stars 172 forks source link

Markdown Library Panic #257

Closed ivanspasov99 closed 2 years ago

ivanspasov99 commented 2 years ago

If I have markdown as follows:

## Resources
- [**Guidelines:** Formatting Time]([URL](https://host.host.host.host/design/format/))

When ToHTML is executed the application panics.

miekg commented 2 years ago

Can't reproduce with mmark (which uses this library, but might be still different):

% ./mmark -html -fragment x.md 
<h2 id="resources">Resources</h2>

<ul>
<li><a href="[URL](https://host.host.host.host/design/format/)"><strong>Guidelines:</strong> Formatting Time</a></li>
</ul>
ivanspasov99 commented 2 years ago

I have found how it is reproduced. It is partially connected with our own custom RenderNodeFunction.

Github supports empty links. If you try to render [link]() it won't be recognise as a link from the library.

But we are having partial node update, we are assigning a specific node.Destination (in the this case with the empty link, we are making node.Destination = ""), then leaving the library to write it to the buffer and it makes the markdown panic index out of range [0] with length 0.

So I guess the solution is to support such empty links

miekg commented 2 years ago

[ Quoting @.***> in "Re: [gomarkdown/markdown] Markdown ..." ]

Github supports empty links. If you try to render link it won't be recognise as a link from the library.

What's an empty link? Something like [url]() ? Is that in common mark?

miekg commented 2 years ago

found it, this is indeed a bit jarring:

[hello](http://bla)

[](http://bla)

the latter renders as: <p>[](<a href="http://bla">http://bla</a>)</p>, CommonMark seems to render to <p><a href="http://bla"></a></p>

miekg commented 2 years ago

https://github.com/gomarkdown/markdown/pull/258 makes [](bla) work, won't fix your panic I think, you probably need to catch that in your RenderNode hook.

ivanspasov99 commented 2 years ago

By empty link I mean one without reference. Example [link]()