From d1fab4eb3e3070eafabd7eb4cc9298dc723aac17 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sat, 13 Apr 2024 15:18:27 -0400 Subject: [PATCH 1/5] fix(#21): possible patch --- mdformat_mkdocs/_normalize_list.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mdformat_mkdocs/_normalize_list.py b/mdformat_mkdocs/_normalize_list.py index 1e7634b..6a326f9 100644 --- a/mdformat_mkdocs/_normalize_list.py +++ b/mdformat_mkdocs/_normalize_list.py @@ -122,9 +122,21 @@ def parse_line(line_num: int, content: str) -> ParsedLine: def acc_line_results(parsed_lines: list[ParsedLine]) -> list[LineResult]: results: list[LineResult] = [] + fenced_block = False for parsed in parsed_lines: parent_idx = 0 parents = [] + + if fenced_block and parsed.syntax != Syntax.EDGE_CODE: + parsed = ParsedLine( + line_num=parsed.line_num, + indent=parsed.indent, + content=parsed.content, + syntax=None, + ) + if parsed.syntax == Syntax.EDGE_CODE: + fenced_block = not fenced_block + with suppress(StopIteration): parent_idx, parent = next( (len(results) - idx, line) From e5762992a96a869fdbc43020c6d4cb3d8abaa25c Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sat, 13 Apr 2024 15:18:46 -0400 Subject: [PATCH 2/5] test(#21): add test for #21 --- tests/format/fixtures/text.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/format/fixtures/text.md b/tests/format/fixtures/text.md index 17e9965..55d0f1a 100644 --- a/tests/format/fixtures/text.md +++ b/tests/format/fixtures/text.md @@ -1457,3 +1457,18 @@ Without CLI argument, does not support Python mkdocstring cross-references (http \[package.module.object\]\[\] \[Object\]\[package.module.object\] . + +Do not format lists in code blocks +. +* Item 1 + + ```text + * sample plain text block. + ``` +. +- Item 1 + + ```text + * sample plain text block. + ``` +. From c840b8438b642e35a26b254263662a6a8d2dacca Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sat, 13 Apr 2024 15:22:18 -0400 Subject: [PATCH 3/5] docs: add some docstrings --- mdformat_mkdocs/_normalize_list.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mdformat_mkdocs/_normalize_list.py b/mdformat_mkdocs/_normalize_list.py index 6a326f9..5f7e848 100644 --- a/mdformat_mkdocs/_normalize_list.py +++ b/mdformat_mkdocs/_normalize_list.py @@ -288,6 +288,8 @@ def parse_text(text: str, inc_numbers: bool) -> ParsedText: class SemanticIndent(Enum): + """Possible states for evaluating semantic indents. The values aren't relevant.""" + INITIAL = "Hack for MyPy and map_lookack, which returns initial..." EMPTY = "" ONE_LESS_ON_NEXT = "⤓(←)" @@ -297,6 +299,7 @@ class SemanticIndent(Enum): def parse_semantic_indent(last: SemanticIndent, line: LineResult) -> SemanticIndent: + """Conditionally evaluate when semantic indents are necessary.""" # TODO: This works, but is very confusing if not line.parsed.content: result = SemanticIndent.EMPTY @@ -358,6 +361,7 @@ def normalize_list( context: RenderContext, check_if_align_semantic_breaks_in_lists: Callable[[], bool], # Attach with partial ) -> str: + """Format markdown list.""" # FIXME: Is this filter working correctly? # If it is, the test for "Formats non-root lists" should be failing if node.level > 1: From c7fb09070cf342dcf1ddf56cbb405a01ee3d4788 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sat, 13 Apr 2024 15:45:31 -0400 Subject: [PATCH 4/5] refactor: use code_indents instead --- mdformat_mkdocs/_normalize_list.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/mdformat_mkdocs/_normalize_list.py b/mdformat_mkdocs/_normalize_list.py index 5f7e848..612b36d 100644 --- a/mdformat_mkdocs/_normalize_list.py +++ b/mdformat_mkdocs/_normalize_list.py @@ -122,21 +122,10 @@ def parse_line(line_num: int, content: str) -> ParsedLine: def acc_line_results(parsed_lines: list[ParsedLine]) -> list[LineResult]: results: list[LineResult] = [] - fenced_block = False for parsed in parsed_lines: parent_idx = 0 parents = [] - if fenced_block and parsed.syntax != Syntax.EDGE_CODE: - parsed = ParsedLine( - line_num=parsed.line_num, - indent=parsed.indent, - content=parsed.content, - syntax=None, - ) - if parsed.syntax == Syntax.EDGE_CODE: - fenced_block = not fenced_block - with suppress(StopIteration): parent_idx, parent = next( (len(results) - idx, line) @@ -250,9 +239,12 @@ class ParsedText(NamedTuple): debug_block_indents: list[BlockIndent | None] -def format_new_content(line: LineResult, inc_numbers: bool) -> str: +def format_new_content(line: LineResult, inc_numbers: bool, is_code: bool) -> str: new_content = line.parsed.content - if line.parsed.syntax in {Syntax.LIST_BULLETED, Syntax.LIST_NUMBERED}: + if not is_code and line.parsed.syntax in { + Syntax.LIST_BULLETED, + Syntax.LIST_NUMBERED, + }: list_match = RE_LIST_ITEM.fullmatch(line.parsed.content) assert list_match is not None # for pyright # noqa: S101 new_bullet = "-" @@ -275,7 +267,10 @@ def parse_text(text: str, inc_numbers: bool) -> ParsedText: block_indents = [_c or _h for _c, _h in zip_equal(code_indents, html_indents)] new_indents = list(starmap(format_new_indent, zip_equal(lines, block_indents))) - new_contents = [format_new_content(line, inc_numbers) for line in lines] + new_contents = [ + format_new_content(line, inc_numbers, ci is not None) + for line, ci in zip_equal(lines, code_indents) + ] return ParsedText( lines=lines, new_lines=[*zip_equal(new_indents, new_contents)], From 3eec79f9c23ae4a1ded27410a0804091f9ea949c Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sat, 13 Apr 2024 15:52:50 -0400 Subject: [PATCH 5/5] release: v2.0.8 --- mdformat_mkdocs/__init__.py | 2 +- mdformat_mkdocs/_normalize_list.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mdformat_mkdocs/__init__.py b/mdformat_mkdocs/__init__.py index 606ab2f..3addf31 100644 --- a/mdformat_mkdocs/__init__.py +++ b/mdformat_mkdocs/__init__.py @@ -1,6 +1,6 @@ """An mdformat plugin for mkdocs.""" -__version__ = "2.0.7" +__version__ = "2.0.8" # FYI see source code for available interfaces: # https://github.com/executablebooks/mdformat/blob/5d9b573ce33bae219087984dd148894c774f41d4/src/mdformat/plugins.py diff --git a/mdformat_mkdocs/_normalize_list.py b/mdformat_mkdocs/_normalize_list.py index 612b36d..f4492a7 100644 --- a/mdformat_mkdocs/_normalize_list.py +++ b/mdformat_mkdocs/_normalize_list.py @@ -125,7 +125,6 @@ def acc_line_results(parsed_lines: list[ParsedLine]) -> list[LineResult]: for parsed in parsed_lines: parent_idx = 0 parents = [] - with suppress(StopIteration): parent_idx, parent = next( (len(results) - idx, line)