Skip to content

Commit

Permalink
feat(#4): support semantic list indents
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Jun 27, 2023
1 parent 464f4df commit 815e977
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[cov-link]: https://codecov.io/gh/executablebooks/mdformat-mkdocs
-->

An [mdformat](https://github.com/executablebooks/mdformat) plugin for mkdocs.
An [mdformat](https://github.com/executablebooks/mdformat) plugin for [mkdocs](https://github.com/mkdocs/mkdocs).

## Usage

Expand Down
15 changes: 13 additions & 2 deletions mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
_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."""


def update_mdit(mdit: MarkdownIt) -> None:
"""No changes to markdown parsing are necessary."""
...
global _ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS
# FIXME: How do I add this configuration option?
_ALIGN_SEMANTIC_BREAKS_IN_NUMBERED_LISTS = mdit.options.get(
"align_semantic_breaks_in_numbered_lists", True
)


_RE_INDENT = re.compile(r"(?P<indent>\s*)(?P<content>[^\s]?.*)")
Expand All @@ -30,13 +37,15 @@ def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) ->
last_indent = ""
indent_counter = 0
indent_lookup: Dict[str, int] = {}
is_numbered = False
for line in text.split(eol):
match = _RE_INDENT.match(line)
assert match is not None # for pylint
list_match = _RE_LIST_ITEM.match(match["content"])
new_line = line
if list_match:
new_bullet = "-" if list_match["bullet"] in {"-", "*"} else "1."
is_numbered = list_match["bullet"] not in {"-", "*"}
new_bullet = "1." if is_numbered else "-"
new_line = f'{new_bullet} {list_match["item"]}'

this_indent = match["indent"]
Expand All @@ -55,6 +64,8 @@ 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]
rendered += f"{new_indent}{new_line.strip()}{eol}"
return rendered.rstrip()

Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ Hanging List (https://github.com/executablebooks/mdformat/issues/371)
Otherwise this next paragraph doesn't belong in the same list item.
.
1. Here indent width is
three.
three.
1. Here indent width is
three.
three.
1. Here indent width is
five. It needs to be so, because
five. It needs to be so, because
Otherwise this next paragraph doesn't belong in the same list item.
Otherwise this next paragraph doesn't belong in the same list item.
.
Table
Expand Down

0 comments on commit 815e977

Please sign in to comment.