Skip to content

Commit

Permalink
feat: generalize semantic line breaks for bulleted lists as well
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Jul 18, 2023
1 parent ffac669 commit c8b4784
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
- id: mdformat-with-semantic-arg
name: mdformat-with-semantic-arg
entry: mdformat
args: [--align-semantic-breaks-in-numbered-lists]
files: tests/pre-commit-test-align_semantic_breaks_in_numbered_lists.md
args: [--align-semantic-breaks-in-lists]
files: tests/pre-commit-test-align_semantic_breaks_in_lists.md
types: [markdown]
language: system
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ pipx inject mdformat mdformat-mkdocs

## CLI Options

`mdformat-mkdocs` adds the CLI argument `--align-semantic-breaks-in-numbered-lists` to optionally align line breaks in numbered lists to 3-spaces. If not specified, the default of 4-indents is followed universally.
`mdformat-mkdocs` adds the CLI argument `--align-semantic-breaks-in-lists` to optionally align line breaks in numbered lists to 3-spaces. If not specified, the default of 4-indents is followed universally.

```txt
# with: mdformat
1. Semantic line feed where the following line is
three spaces deep
# vs. with: mdformat --align-semantic-breaks-in-numbered-lists
# vs. with: mdformat --align-semantic-breaks-in-lists
1. Semantic line feed where the following line is
three spaces deep
```

Note: the `align-semantic-breaks-in-numbered-lists` setting is not supported in the configuration file yet (https://github.com/executablebooks/mdformat/issues/378)
Note: the `align-semantic-breaks-in-lists` setting is not supported in the configuration file yet (https://github.com/executablebooks/mdformat/issues/378)

## Caveats

Expand Down
19 changes: 8 additions & 11 deletions mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
_MKDOCS_INDENT_COUNT = 4
"""Use 4-spaces for mkdocs."""

_ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS = False
_ALIGN_SEMANTIC_BREAKS_IN_LISTS = False
"""user-specified flag for toggling semantic breaks.
- 3-spaces on subsequent lines in semantic numbered lists
- and 2-spaces on subsequent bulleted items within a numbered list
- and 2-spaces on subsequent bulleted items
"""


def add_cli_options(parser: argparse.ArgumentParser) -> None:
"""Add options to the mdformat CLI, to be stored in `mdit.options["mdformat"]`."""
parser.add_argument(
"--align-semantic-breaks-in-numbered-lists",
"--align-semantic-breaks-in-lists",
action="store_true",
help="If specified, align semantic indents in numbered lists to the text",
help="If specified, align semantic indents in numbered and bulleted lists to the text",
)


def update_mdit(mdit: MarkdownIt) -> None:
"""No changes to markdown parsing are necessary."""
global _ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS
_ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS = mdit.options["mdformat"].get(
"align_semantic_breaks_in_numbered_lists", False
global _ALIGN_SEMANTIC_BREAKS_IN_LISTS
_ALIGN_SEMANTIC_BREAKS_IN_LISTS = mdit.options["mdformat"].get(
"align_semantic_breaks_in_lists", False
)


Expand All @@ -52,7 +52,6 @@ def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) ->
indent_counter = 0
indent_lookup: Dict[str, int] = {}
is_numbered = False
use_semantic_breaks = _ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS
for line in text.split(eol):
match = _RE_INDENT.match(line)
assert match is not None # for pylint
Expand All @@ -77,11 +76,9 @@ def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) ->
raise NotImplementedError(f"Error in indentation of: `{line}`")
else:
indent_counter = 0
# Handle bulleted items within a numbered list
use_semantic_breaks = use_semantic_breaks and is_numbered
last_indent = this_indent
new_indent = indent * indent_counter
if use_semantic_breaks and not list_match:
if _ALIGN_SEMANTIC_BREAKS_IN_LISTS and not list_match:
removed_indents = -1 if is_numbered else -2
new_indent = new_indent[:removed_indents]
rendered += f"{new_indent}{new_line.strip()}{eol}"
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures-semantic-indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ List with (what should be converted to a) code block
.
- item 1

code block
code block
.

List with explicit code block (that should keep indentation)
Expand All @@ -153,9 +153,9 @@ List with explicit code block (that should keep indentation)
.
- item 1
```txt
code block
```
```txt
code block
```
.


Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ Bulleted semantic lines (https://github.com/KyleKing/mdformat-mkdocs/issues/7)
semantic line 2 (6 spaces deep)
.
- Line
semantic line 1 (2 spaces deep)
semantic line 1 (2 spaces deep)
- Bullet (4 spaces deep)
semantic line 2 (6 spaces deep)
semantic line 2 (6 spaces deep)
.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Testing `--align-semantic-breaks-in-numbered-lists`
# Testing `--align-semantic-breaks-in-lists`

## Semantic Line Indents

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
@pytest.mark.parametrize(
"line,title,text,expected", fixtures, ids=[f[1] for f in fixtures]
)
def test_fixtures(line, title, text, expected):
def test_align_semantic_breaks_in_lists(line, title, text, expected):
output = mdformat.text(
text,
options={"align_semantic_breaks_in_numbered_lists": True},
options={"align_semantic_breaks_in_lists": True},
extensions={"mkdocs"},
)
assert output.rstrip() == expected.rstrip()

0 comments on commit c8b4784

Please sign in to comment.