radude/mdx_truly_sane_lists

mkdocs ordered list not starting at 1 behaviour

mark-heptinstall opened this issue · 2 comments

Not sure on the correct place to report this, ordered lists which do not start at 1 are not working for me with mkdocs material.

mkdocs-1.5.2
mkdocs-material-9.2.3

The behaviour works if using the sane_lists extension but not with mdx_truly_sane_lists

Example:

This is an ordered list that doesn't start at 1:

5. Item 1
6. Item 2
7. Item 3
8. Item 4

sane_lists

image

mdx_truly_sane_lists

image

I see that this behaviour is working as expected on https://rentry.co/

Caution

This following is misleading. After some debugging, I find that only one extension will be used — MkDocs only picks the one that appears later.

We can use both extensions… The order of them matters, and every order has a caveat.

Solution A

markdown_extensions:
  - mdx_truly_sane_lists
  - sane_lists

Reason: sane_lists respects numbers in <ol> (ordered lists), which are ignored by default. However mdx_truly_sane_lists also touches <ol>, and we should let sane_lists make the final decision.

Cons: Nested lists will be flattened.

<!-- This will be flattened -->
- A
  - 1
  - 2
  - 3
- B
<!-- But this will work -->
- A

  - 1
  - 2
  - 3

- B

Solution B

markdown_extensions:
  - sane_lists
  - mdx_truly_sane_lists

Pros: Nestsed lists are fine.

Cons: Numbers are not respected.

The feature is built into Python-Markdown, and all we need to do is to enable it, as in sane_lists.

Possible Fix

class TrulySaneOListProcessor(OListProcessor, TrulySaneBlockProcessorMixin):
SIBLING_TAGS = ["ol"] # from sane lists

 class TrulySaneOListProcessor(OListProcessor, TrulySaneBlockProcessorMixin):
     SIBLING_TAGS = ["ol"]  # from sane lists
+    LAZY_OL = False

Workaround for MkDocs Users

Here is a simple dirty hack.

  1. Edit mkdocs.yml, add a hook.

    hooks:
      - hooks/patch_mdx_truly_sane_lists.py  # the path is relative to `mkdocs.yml`
  2. Create hooks/patch_mdx_truly_sane_lists.py, write the following.

    """Patch mdx_truly_sane_lists to recognize the number used in ordered lists
    
    Usage: [Sane Lists — Python-Markdown documentation](https://python-markdown.github.io/extensions/sane_lists/)
    """
    
    # https://github.com/radude/mdx_truly_sane_lists/issues/21#issuecomment-2028015918
    from mdx_truly_sane_lists.mdx_truly_sane_lists import TrulySaneOListProcessor
    
    TrulySaneOListProcessor.LAZY_OL = False