Skip to content

Commit

Permalink
Merge pull request #26 from KyleKing/fix-24-zero-lists
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Apr 25, 2024
2 parents ebf6da5 + d7cfd70 commit 6db15cc
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Supports:

- Indents are converted to 4-spaces instead of 2
- *Note*: see caveats when using the optional Semantic Indents on numbered lists which may not be a full multiple of 4
- List bullets are converted to dashes instead of `*`
- Unordered list bullets are converted to dashes (`-`) instead of `*`
- By default, ordered lists are standardized on a single digit (`1.` or `0.`) unless `--number` is specified, then `mdformat-mkdocs` will apply consecutive numbering to ordered lists [for consistency with `mdformat`](https://github.com/executablebooks/mdformat?tab=readme-ov-file#options)
- standardized as all `1.` rather than incrementing
- Admonitions (extends [`mdformat-admon`](https://pypi.org/project/mdformat-admon))
- MKDocs-Material Content Tabs (<https://squidfunk.github.io/mkdocs-material/reference/content-tabs>)
- Note: the markup (HTML) rendered by this plugin is sufficient for formatting but not for viewing in a browser. Please open an issue if you have a need to generate valid HTML.
Expand Down
6 changes: 5 additions & 1 deletion mdformat_mkdocs/_normalize_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ def format_new_content(line: LineResult, inc_numbers: bool, is_code: bool) -> st
assert list_match is not None # for pyright # noqa: S101
new_bullet = "-"
if line.parsed.syntax == Syntax.LIST_NUMBERED:
counter = len(line.prev_list_peers) + 1 if inc_numbers else 1
first_peer = (
line.prev_list_peers[-1] if line.prev_list_peers else line.parsed
)
base_num = 0 if first_peer.content.startswith("0.") else 1
counter = len(line.prev_list_peers) + base_num if inc_numbers else base_num
new_bullet = f"{counter}."
new_content = f'{new_bullet} {list_match["item"]}'

Expand Down
45 changes: 45 additions & 0 deletions tests/format/fixtures/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -1495,3 +1495,48 @@ Ignore multiple empty lines within code blocks
* Asterisk
```
.

0-indexed markdown list (https://github.com/KyleKing/mdformat-mkdocs/issues/24)
.
0. xyz
4. abc
1. inner
3. next
.
0. xyz
0. abc
1. inner
1. next
.

Unsupported versions of 0-indexed markdown list (Within unordered list)
.
* unordered
0. xyz
5. abc
1. inner
4. next
.
- unordered
0\. xyz
5\. abc
1\. inner
4\. next
.

Unsupported versions of 0-indexed markdown list (Within ordered list)
.
0. ordered
0. xyz
5. abc
1. inner
4. next
1. next
.
0. ordered
0\. xyz
5\. abc
1\. inner
4\. next
0. next
.
65 changes: 61 additions & 4 deletions tests/format/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,54 @@

from ..helpers import print_text

CASE_0 = """
0. One
1. AAA
1. BBB
1. CCC
0. Two
0. Three
0. Four
1. AAA
1. BBB
1. CCC
0. Five
0. Six
1. AAA
1. BBB
1. CCC
1. aaa
1. bbb
1. ccc
1. ddd
0. Seven
"""


CASE_0_NUMBERED = """
0. One
1. AAA
2. BBB
3. CCC
1. Two
2. Three
3. Four
1. AAA
2. BBB
3. CCC
4. Five
5. Six
1. AAA
2. BBB
3. CCC
1. aaa
2. bbb
3. ccc
4. ddd
6. Seven
"""


CASE_1 = """
1. One
1. AAA
Expand Down Expand Up @@ -54,17 +102,26 @@
@pytest.mark.parametrize(
("text", "expected"),
[
(CASE_0, CASE_0_NUMBERED),
(CASE_1, CASE_1_NUMBERED),
],
ids=[
"CASE_1_NUMBERED",
"CASE_0",
"CASE_1",
],
)
def test_number(text: str, expected: str):
output = mdformat.text(
"""Test CLI argument for ordered lists, `--number`."""
# Check that when --number is set, ordered lists are incremented
output_numbered = mdformat.text(
text,
options={"number": True},
extensions={"mkdocs"},
)
print_text(output, expected)
assert output.strip() == expected.strip()
print_text(output_numbered, expected)
assert output_numbered.strip() == expected.strip()

# Check when not set that ordered lists use a constant 0 or 1
output = mdformat.text(text, extensions={"mkdocs"})
print_text(output, text)
assert output.strip() == text.strip()

0 comments on commit 6db15cc

Please sign in to comment.