diff --git a/README.md b/README.md index 2f7c79e..63f258d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/mdformat_mkdocs/plugin.py b/mdformat_mkdocs/plugin.py index 92dc0a5..11377cd 100644 --- a/mdformat_mkdocs/plugin.py +++ b/mdformat_mkdocs/plugin.py @@ -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\s*)(?P[^\s]?.*)") @@ -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"] @@ -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() diff --git a/tests/fixtures.md b/tests/fixtures.md index 278e12c..6949911 100644 --- a/tests/fixtures.md +++ b/tests/fixtures.md @@ -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