Comments (1)
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)
- API docs are not being properly indexed for search HOT 18
- Strange and inconsistent parsing of lists with headers and multiple lines HOT 6
- The title from `toc_tokens` ignores the `smarty` extension HOT 3
- BlockProcessor output wrapped in p tag HOT 9
- Trying to migrate from `markdown2`, lists without blank lines not working HOT 3
- "Unterminated character set" exception when using extra extension HOT 6
- <table> improperly wrapped by <p> when inside a list HOT 1
- Add Support for Tab Customization in Code Blocks HOT 3
- List not rendering correctly when preceded by word HOT 1
- add a buildin Mermaid extention HOT 1
- No table support? HOT 8
- GFM alerts HOT 2
- TOC extension: ignore headers above [TOC] marker HOT 2
- Fenced code blocks with newlines not parsed into html <code> blocks HOT 2
- convert \u2028 to <br>? HOT 2
- `abbr` extension breaks title attribute added using `attr_list` HOT 1
- Separate extensions from core
- no anchors are created HOT 8
- A line ending that is preceded by a backslash is not parsed as a hard line break HOT 3
- Abbreviation processing order alternative HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from markdown.