Skip to content

Commit

Permalink
fix(#12): support number CLI argument
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Nov 22, 2023
1 parent a3fe20b commit 1534960
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
71 changes: 71 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1534960

Please sign in to comment.