From 018e8d72b1e5cb8c74cac01261f504bb5ede5254 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Sun, 22 Nov 2026 20:59:43 +0800 Subject: [PATCH] Document preserving reversed ordered lists as HTML --- README.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.rst b/README.rst index 059a68f..2da47dd 100644 --- a/README.rst +++ b/README.rst @@ -233,6 +233,36 @@ function names: return IgnoreParagraphsConverter(**options).convert(html) +Preserving reversed ordered lists +------------------------------------ + +CommonMark ordered lists count upwards: writing descending Markdown markers +does not preserve descending numbers in the rendered result. For a renderer +that allows raw HTML, keep reversed lists as HTML with a custom converter: + +.. code:: python + + from markdownify import MarkdownConverter + + class ReversedListConverter(MarkdownConverter): + def convert_ol(self, el, text, parent_tags): + if el.has_attr('reversed'): + return '\n\n' + str(el) + '\n\n' + return super().convert_ol(el, text, parent_tags) + + html = '
  1. Five
  2. Two
' + result = ReversedListConverter().convert(html) + assert result == '
  1. Five
  2. Two
' + assert ReversedListConverter().convert('
  1. One
') == '1. One' + +This preserves the entire reversed list, including its attributes and nested +HTML. The converted child text is discarded, so conversion options for +descendants do not affect the preserved HTML. Use this recipe with trusted or +separately sanitized HTML and a renderer that preserves these list attributes. +Renderers that escape or remove raw HTML cannot use this recipe. The default +converter's behavior is unchanged. + + Command Line Interface ======================