aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/extensions/toc.py5
-rw-r--r--tests/extensions-x-toc/invalid.html6
-rw-r--r--tests/extensions-x-toc/invalid.txt9
3 files changed, 19 insertions, 1 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index 3e6e075..1624ccf 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -47,8 +47,11 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
# To keep the output from screwing up the
# validation by putting a <div> inside of a <p>
# we actually replace the <p> in its entirety.
+ # We do not allow the marker inside a header as that
+ # would causes an enless loop of placing a new TOC
+ # inside previously generated TOC.
- if c.text.find(self.config["marker"][0]) > -1:
+ if c.text.find(self.config["marker"][0]) > -1 and not header_rgx.match(c.tag):
for i in range(len(p)):
if p[i] == c:
p[i] = div
diff --git a/tests/extensions-x-toc/invalid.html b/tests/extensions-x-toc/invalid.html
new file mode 100644
index 0000000..41a3b1f
--- /dev/null
+++ b/tests/extensions-x-toc/invalid.html
@@ -0,0 +1,6 @@
+<h2 id="toc">[TOC]</h2>
+<h1 id="header-1">Header 1</h1>
+<p>The TOC marker cannot be inside a header. This test makes sure markdown doesn't
+crash when it encounters this errant syntax. The unexpected output should
+clue the author in that s/he needs to add a blank line between the TOC and
+the <code>&lt;hr&gt;</code>.</p> \ No newline at end of file
diff --git a/tests/extensions-x-toc/invalid.txt b/tests/extensions-x-toc/invalid.txt
new file mode 100644
index 0000000..f6c4ec4
--- /dev/null
+++ b/tests/extensions-x-toc/invalid.txt
@@ -0,0 +1,9 @@
+[TOC]
+-----
+
+# Header 1
+
+The TOC marker cannot be inside a header. This test makes sure markdown doesn't
+crash when it encounters this errant syntax. The unexpected output should
+clue the author in that s/he needs to add a blank line between the TOC and
+the `<hr>`.