From 523c584d2ccfa95426d619145b62c9d36d77c122 Mon Sep 17 00:00:00 2001 From: ShivsaranshThakur1-Coder <237750418+ShivsaranshThakur1-Coder@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:45:52 +0100 Subject: [PATCH] Add opt-in minimum list indentation for Python-Markdown compatibility --- README.rst | 8 ++++++++ markdownify/__init__.py | 4 +++- markdownify/__init__.pyi | 2 ++ markdownify/main.py | 3 +++ tests/test_lists.py | 41 ++++++++++++++++++++++++++++++++++++++++ tests/types.py | 3 +++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 059a68f..c536ad2 100644 --- a/README.rst +++ b/README.rst @@ -79,6 +79,14 @@ bullets lists are nested. Otherwise, the bullet will alternate based on nesting level. Defaults to ``'*+-'``. +list_indent_width + Minimum width, in spaces, of a list item's marker and following whitespace. + Defaults to ``0``, which uses the marker's natural width. Set to ``4`` to + preserve nested lists when the output is parsed by Python-Markdown. + Extra spaces are added after the marker, and continuation lines use the + same indentation. Markers wider than this minimum are never truncated. + Available as ``--list-indent-width`` on the command line. + strong_em_symbol In markdown, both ``*`` and ``_`` are used to encode **strong** or *emphasized* texts. Either of these symbols can be chosen by the options diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 28cdaf6..2e27bfe 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -187,6 +187,7 @@ class DefaultOptions: escape_misc = False heading_style = UNDERLINED keep_inline_images_in = [] + list_indent_width = 0 newline_style = SPACES strip = None strip_document = STRIP @@ -647,7 +648,8 @@ def convert_li(self, el, text, parent_tags): el = el.parent bullets = self.options['bullets'] bullet = bullets[depth % len(bullets)] - bullet = bullet + ' ' + # Pad the marker and continuation lines to the requested minimum width. + bullet = (bullet + ' ').ljust(self.options['list_indent_width']) bullet_width = len(bullet) bullet_indent = ' ' * bullet_width diff --git a/markdownify/__init__.pyi b/markdownify/__init__.pyi index 2494011..71f71ac 100644 --- a/markdownify/__init__.pyi +++ b/markdownify/__init__.pyi @@ -29,6 +29,7 @@ def markdownify( escape_misc: bool = ..., heading_style: str = ..., keep_inline_images_in: list[str] = ..., + list_indent_width: int = ..., newline_style: str = ..., strip: Union[list[str], None] = ..., strip_document: Union[str, None] = ..., @@ -57,6 +58,7 @@ class MarkdownConverter: escape_misc: bool = ..., heading_style: str = ..., keep_inline_images_in: list[str] = ..., + list_indent_width: int = ..., newline_style: str = ..., strip: Union[list[str], None] = ..., strip_document: Union[str, None] = ..., diff --git a/markdownify/main.py b/markdownify/main.py index ba70671..15ab3e6 100755 --- a/markdownify/main.py +++ b/markdownify/main.py @@ -35,6 +35,9 @@ def main(argv=sys.argv[1:]): parser.add_argument('-b', '--bullets', default='*+-', help="A string of bullet styles to use; the bullet will " "alternate based on nesting level.") + parser.add_argument('--list-indent-width', type=int, default=0, + help="Minimum list item indentation. Use 4 for compatibility " + "with Python-Markdown; 0 uses the marker width.") parser.add_argument('--strong-em-symbol', default=ASTERISK, choices=(ASTERISK, UNDERSCORE), help="Use * or _ to convert strong and italics text"), diff --git a/tests/test_lists.py b/tests/test_lists.py index e9480ab..fec1331 100644 --- a/tests/test_lists.py +++ b/tests/test_lists.py @@ -1,3 +1,8 @@ +import subprocess +import sys + +import pytest + from .utils import md @@ -87,3 +92,39 @@ def test_bullets(): def test_li_text(): assert md('
first
second
quote
first
second
test\n foo\nbar', code_language_callback=callback) markdownify('
test\n foo\nbar', code_language_callback=lambda el: None) +markdownify('