Actually this diff seems hard to understand but I'm just wrapping the previous code with the following if statement:

~~~c
if (level <= options->toc_data.max_level)
~~~