GithubHelp home page GithubHelp logo

Comments (1)

waylan avatar waylan commented on May 25, 2024

Actually, the invalid link is irrelevant. Remove it and the problem still exists.

In [8]: markdown.markdown('[TOC]\n\n### bar\n\n#foo', extensions=['toc'])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

/home/waylan/code/md/<ipython console> in <module>()

/home/waylan/code/md/markdown/__init__.py in markdown(text, *args, **kwargs)
    384     """
    385     md = Markdown(*args, **kwargs)
--> 386     return md.convert(text)
    387
    388

/home/waylan/code/md/markdown/__init__.py in convert(self, source)
    285         # Run the tree-processors

    286         for treeprocessor in self.treeprocessors.values():
--> 287             newRoot = treeprocessor.run(root)
    288             if newRoot:
    289                 root = newRoot

/home/waylan/code/md/markdown/extensions/toc.py in run(self, doc)
    109                     c.append(anchor)
    110
--> 111                 list_stack[-1].append(last_li)
    112         if not marker_found:
    113             # searialize and attach to markdown instance.


IndexError: list index out of range

A look at what happens when we increase the level of the first header by one (subtract 1) should shed some light on the problem:

In [7]: markdown.markdown('[TOC]\n\n## bar\n\n#foo', extensions=['toc'])
Out[7]: u'<div class="toc">\n<ul>\n<li><a href="#bar">bar</a></li>\n</ul>\n<li><a href="#foo">foo</a></li>\n</div>\n<h2 id="bar">bar</h2>\n<h1 id="foo">foo</h1>'

While that works, it doesn't give exactly good output. In fact, a known limitation of the TOC extension is that it doesn't handle lower level (higher numbered) headers before lower level headers in a document. Make sure your first header is of the highest used and the problem goes away.

In [9]: markdown.markdown('[TOC]\n\n#baz\n\n### bar\n\n#foo', extensions=['toc'])
Out[9]: u'<div class="toc">\n<ul>\n<li><a href="#baz">baz</a><ul>\n<li><a href="#bar">bar</a></li>\n</ul>\n</li>\n<li><a href="#foo">foo</a></li>\n</ul>\n</div>\n<h1 id="baz">baz</h1>\n<h3 id="bar">bar</h3>\n<h1 id="foo">foo</h1>'

Yes, it would be nice if we could support all possible combinations. Patches are welcome.

That said, we should never be crashing, so this is a bug in that respect.

I'll fix the bug by catching the exception and simply not outputting a TOC on error for now. Getting the TOC to handle more complex input could be a feature request for a future release.

from markdown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.