didix21 / mdutils

Python package contains a set of basic tools that can help to create a markdown file.
https://pypi.org/project/mdutils
MIT License
206 stars 49 forks source link

Varying Title level leads to IndexError in __add_new_item_table_of_content #68

Closed 303sec closed 2 years ago

303sec commented 3 years ago

There are various issues with using title levels in a non-standard order which seems to trigger an IndexError.

To Reproduce Steps to reproduce the behavior:

from mdutils.mdutils import MdUtils
md.new_header(level=3, title=infile)
md = MdUtils(file_name='test.md',title='test')
md.new_header(level=3, title='Level 3 header')
python3.7 mdutilstest.py

Traceback (most recent call last):
  File "faraday_markdown_parser.py", line 19, in <module>
    md.new_header(level=3, title=host_title)
  File "/usr/local/lib/python3.7/dist-packages/mdutils/mdutils.py", line 113, in new_header
    self.__add_new_item_table_of_content(level, title)
  File "/usr/local/lib/python3.7/dist-packages/mdutils/mdutils.py", line 130, in __add_new_item_table_of_content
    curr = curr[-1]
IndexError: list index out of range

Expected behavior It should be possible to have title levels in arbitrary order/

Desktop (please complete the following information):

didix21 commented 2 years ago

Maybe you should add another example because you're using md instance before creating it. So your code will always crash 😅.