summaryrefslogtreecommitdiff
path: root/markdown
diff options
context:
space:
mode:
authorWaylan Limberg <waylan.limberg@icloud.com>2021-02-24 14:20:29 -0500
committerGitHub <noreply@github.com>2021-02-24 14:20:29 -0500
commit613ad502a4cf2293901e8f67690c198fc22c8f88 (patch)
tree881dcb80d9a2ba31f6e7e72cccb932c05fc25d6f /markdown
parent1858c1b601ead62ed49646ae0d99298f41b1a271 (diff)
downloadpython-markdown-613ad502a4cf2293901e8f67690c198fc22c8f88.tar.gz
Ensure permalinks and ankorlinks are not restricted by toc_depth
This fixes a regression which was introduced with support for toc_depth. Relevant tests have been moved and updated to the new framework. Fixes #1107. The test framework also received an addition. The assertMarkdownRenders method now accepts a new keyword expected_attrs which consists of a dict of attrs and expected values. Each is checked against the attr of the Markdown instance. This was needed to check the value of md.toc and md.toc_tokens in some of the included tests.
Diffstat (limited to 'markdown')
-rw-r--r--markdown/extensions/toc.py19
-rw-r--r--markdown/test_tools.py16
2 files changed, 22 insertions, 13 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index b2564c9..d64ec16 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -269,8 +269,6 @@ class TocTreeprocessor(Treeprocessor):
for el in doc.iter():
if isinstance(el.tag, str) and self.header_rgx.match(el.tag):
self.set_level(el)
- if int(el.tag[-1]) < self.toc_top or int(el.tag[-1]) > self.toc_bottom:
- continue
text = get_name(el)
# Do not override pre-existing ids
@@ -278,14 +276,15 @@ class TocTreeprocessor(Treeprocessor):
innertext = unescape(stashedHTML2text(text, self.md))
el.attrib["id"] = unique(self.slugify(innertext, self.sep), used_ids)
- toc_tokens.append({
- 'level': int(el.tag[-1]),
- 'id': el.attrib["id"],
- 'name': unescape(stashedHTML2text(
- code_escape(el.attrib.get('data-toc-label', text)),
- self.md, strip_entities=False
- ))
- })
+ if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom:
+ toc_tokens.append({
+ 'level': int(el.tag[-1]),
+ 'id': el.attrib["id"],
+ 'name': unescape(stashedHTML2text(
+ code_escape(el.attrib.get('data-toc-label', text)),
+ self.md, strip_entities=False
+ ))
+ })
# Remove the data-toc-label attribute as it is no longer needed
if 'data-toc-label' in el.attrib:
diff --git a/markdown/test_tools.py b/markdown/test_tools.py
index 5f33619..21ae1a7 100644
--- a/markdown/test_tools.py
+++ b/markdown/test_tools.py
@@ -23,7 +23,7 @@ import os
import sys
import unittest
import textwrap
-from . import markdown, util
+from . import markdown, Markdown, util
try:
import tidylib
@@ -54,15 +54,25 @@ class TestCase(unittest.TestCase):
default_kwargs = {}
- def assertMarkdownRenders(self, source, expected, **kwargs):
+ def assertMarkdownRenders(self, source, expected, expected_attrs=None, **kwargs):
"""
Test that source Markdown text renders to expected output with given keywords.
+
+ `expected_attrs` accepts a dict. Each key should be the name of an attribute
+ on the `Markdown` instance and the value should be the expected value after
+ the source text is parsed by Markdown. After the expected output is tested,
+ the expected value for each attribute is compared against the actual
+ attribute of the `Markdown` instance using `TestCase.assertEqual`.
"""
+ expected_attrs = expected_attrs or {}
kws = self.default_kwargs.copy()
kws.update(kwargs)
- output = markdown(source, **kws)
+ md = Markdown(**kws)
+ output = md.convert(source)
self.assertMultiLineEqual(output, expected)
+ for key, value in expected_attrs.items():
+ self.assertEqual(getattr(md, key), value)
def dedent(self, text):
"""