By default BTreemap is sorted ( in this case by title), so by default get_toc() returns the table of contents sorted alphabetically instead of the order they occur in the document.
For most parsing use cases the order of TOC matters.
Changes are minimal and just use IndexMap instead of BTreeMap.
Replace the use of BTreeMap with IndexMap in TOC.
By default BTreemap is sorted ( in this case by title), so by default get_toc() returns the table of contents sorted alphabetically instead of the order they occur in the document.
For most parsing use cases the order of TOC matters.
Changes are minimal and just use IndexMap instead of BTreeMap.