Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve semantic errors #8

Merged
merged 6 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion mdformat_mkdocs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""An mdformat plugin for mkdocs."""

__version__ = "1.0.2"
__version__ = "1.0.3"

from .plugin import ( # noqa: F401
POSTPROCESSORS,
Expand Down
26 changes: 16 additions & 10 deletions mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@
_MKDOCS_INDENT_COUNT = 4
"""Use 4-spaces for mkdocs."""

_ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS = False
"""use 3-space on subsequent lines in semantic lists."""
_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

"""


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 Down Expand Up @@ -60,7 +65,7 @@ def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) ->
this_indent = match["indent"]
if this_indent:
indent_diff = len(this_indent) - len(last_indent)
if indent_diff == 0:
if not indent_diff:
...
elif indent_diff > 0:
indent_counter += 1
Expand All @@ -73,8 +78,9 @@ def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) ->
indent_counter = 0
last_indent = this_indent
new_indent = indent * indent_counter
if _ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS and not list_match and is_numbered:
new_indent = new_indent[:-1]
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}"
return rendered.rstrip()

Expand Down
22 changes: 20 additions & 2 deletions mkdocs-demo/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See discussion on: https://github.com/KyleKing/mdformat-mkdocs/issues/4
three.

1. Here indent width is
five (three). It needs to be so, because
five (three). The following indent needs to be four (but it is 3 with semantic change).

Otherwise this next paragraph doesn't belong in the same list item.

Expand All @@ -30,6 +30,24 @@ See discussion on: https://github.com/KyleKing/mdformat-mkdocs/issues/4
three (four).

1. Here indent width is
five (four). It needs to be so, because
five (four). The following indent needs to be four.

Otherwise this next paragraph doesn't belong in the same list item.

---

### With proposed change for bullets

1. Line
semantic line 1 (3 spaces deep)
- Bullet (4 spaces deep)
semantic line 2 (6 spaces deep)

---

### With proposed change for bullets nested in a numbered list

- Line
semantic line 1 (2 spaces deep)
- Bullet (4 spaces deep)
semantic line 2 (6 spaces deep)
Loading
Loading