yuin / goldmark

:trophy: A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.
MIT License
3.68k stars 255 forks source link

renderer: Don't panic for unknown NodeKind #107

Closed abhinav closed 4 years ago

abhinav commented 4 years ago

When rendering a Node, Renderer looks up the NodeRenderer for that node's kind in a slice. That will panic with an "index out of range" error if a node renderer for that node kind has not been registered.

This change verifies that a node renderer is available for that node kind before attempting to use it, and instead of panicking, returns an informative error message.

yuin commented 4 years ago

This kind of PR has been submitted before.

Works as intended. This is coding problem so it should panic.

abhinav commented 4 years ago

I can see the argument for this being a panic. The error message can be hard to decipher, though.

panic: runtime error: index out of range [1] with length 1

It definitely stumped me for a minute, which is what lead to this PR. The issue on my end was installing an extension on the parser but not on the renderer, which was instantiated separately.

I can update the PR to throw a panic with a more helpful error message if that's preferable.