diff --git a/mdformat_mkdocs/plugin.py b/mdformat_mkdocs/plugin.py index e01c9d2..c387476 100644 --- a/mdformat_mkdocs/plugin.py +++ b/mdformat_mkdocs/plugin.py @@ -59,12 +59,21 @@ class _MarkdownList: is_semantic_indent = False is_list_match = False + _numbered = None + def __init__(self, increment_number_mode: bool) -> None: """Store relevant 'mdformat' context.""" self.increment_number_mode = increment_number_mode def _number(self) -> int: """Return the number.""" + if self.increment_number_mode: + idx = len(self.this_indent) + pad = [0] * (idx + 1) + # FYI: on de-dent, clip previously saved _numbered + self._numbered = [*(self._numbered or []), *pad][: (idx + 1)] + self._numbered[idx] += 1 + return self._numbered[idx] return 1 def add_bullet(self, line: str) -> str: diff --git a/tests/test_number.py b/tests/test_number.py new file mode 100644 index 0000000..b30c777 --- /dev/null +++ b/tests/test_number.py @@ -0,0 +1,71 @@ +import mdformat +import pytest + +CASE_1 = """ +1. One + 1. AAA + 1. BBB + 1. CCC +1. Two +1. Three +1. Four + 1. AAA + 1. BBB + 1. CCC +1. Five +1. Six + 1. AAA + 1. BBB + 1. CCC + 1. aaa + 1. bbb + 1. ccc + 1. ddd +1. Seven +""" + + +CASE_1_NUMBERED = """ +1. One + 1. AAA + 2. BBB + 3. CCC +2. Two +3. Three +4. Four + 1. AAA + 2. BBB + 3. CCC +5. Five +6. Six + 1. AAA + 2. BBB + 3. CCC + 1. aaa + 2. bbb + 3. ccc + 4. ddd +7. Seven +""" + + +@pytest.mark.parametrize( + "text,expected", + [ + (CASE_1, CASE_1_NUMBERED), + ], + ids=[ + "CASE_1_NUMBERED", + ], +) +def test_number(text: str, expected: str): + output = mdformat.text( + text, + options={"number": True}, + extensions={"mkdocs"}, + ) + + print(output.strip()) + print("-- Expected --") + print(expected.strip()) + assert output.strip() == expected.strip()