medfreeman / markdown-it-toc-and-anchor

markdown-it plugin to add a toc and anchor links in headings
MIT License
60 stars 35 forks source link

Fix a bug which occurs when the heading has no children #34

Closed asmsuechan closed 7 years ago

asmsuechan commented 7 years ago

Hi. I hit an error TypeError: Cannot read property 'type' of undefined when I write such a markdown below:

# hoge

##
screen shot 2017-03-19 at 23 50 22 screen shot 2017-03-19 at 23 33 27
coveralls commented 7 years ago

Coverage Status

Coverage increased (+1.04%) to 100.0% when pulling bdc81f535574b51e3848f7d0dcb90bbcbe2a9e51 on asmsuechan:fix-a-bug into 64a3eb3752882ea34115006d7a5845134f1eef74 on medfreeman:master.

asmsuechan commented 7 years ago

Hi, @medfreeman. What do you think?

medfreeman commented 7 years ago

Thank you !

thematlin commented 7 years ago

@medfreeman I'm experiencing the same bug. Would it be possible to get this released and published to npm? Would be greatly appreciated.

leopoldchen commented 6 years ago

@asmsuechan @medfreeman the solution should be like this

if (heading.children &&
    heading.children.length > 0 &&
    heading.children[0].type === "link_open") 

because heading.children might be null as well

here is an online demo

Just enable the TOC, and type a '#' on a new line, and see the error log in the console