From f09e8b89fd354f6fd5a5d26a206b719319a2cec3 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Tue, 25 Jan 2022 13:50:55 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20NEW:=20Save=20ordered=20list=20numb?= =?UTF-8?q?ering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- markdown_it/port.yaml | 6 +++--- markdown_it/rules_block/list.py | 3 +++ markdown_it/token.py | 1 + tests/test_port/test_misc.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/markdown_it/port.yaml b/markdown_it/port.yaml index dd707761..6c3b06c1 100644 --- a/markdown_it/port.yaml +++ b/markdown_it/port.yaml @@ -1,7 +1,7 @@ - package: markdown-it/markdown-it - version: 12.1.0 - commit: e5986bb7cca20ac95dc81e4741c08949bf01bb77 - date: Jul 15, 2021 + version: 12.2.0 + commit: 6e2de08a0b03d3d0dcc524b89710ce05f83a0283 + date: Aug 2, 2021 notes: - Rename variables that use python built-in names, e.g. - `max` -> `maximum` diff --git a/markdown_it/rules_block/list.py b/markdown_it/rules_block/list.py index a5318c96..59789350 100644 --- a/markdown_it/rules_block/list.py +++ b/markdown_it/rules_block/list.py @@ -230,6 +230,8 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool): token = state.push("list_item_open", "li", 1) token.markup = chr(markerCharCode) token.map = itemLines = [startLine, 0] + if isOrdered: + token.info = state.src[start : posAfterMarker - 1] # change current state, then restore it after parser subcall oldTight = state.tight @@ -313,6 +315,7 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool): posAfterMarker = skipOrderedListMarker(state, nextLine) if posAfterMarker < 0: break + start = state.bMarks[nextLine] + state.tShift[nextLine] else: posAfterMarker = skipBulletListMarker(state, nextLine) if posAfterMarker < 0: diff --git a/markdown_it/token.py b/markdown_it/token.py index 36646e07..78599a1d 100644 --- a/markdown_it/token.py +++ b/markdown_it/token.py @@ -55,6 +55,7 @@ class Token: # Additional information: # - Info string for "fence" tokens # - The value "auto" for autolink "link_open" and "link_close" tokens + # - The string value of the item marker for ordered-list "list_item_open" tokens info: str = attr.ib(default="") # A place for plugins to store any arbitrary data meta: dict = attr.ib(factory=dict) diff --git a/tests/test_port/test_misc.py b/tests/test_port/test_misc.py index f5f821e9..8e7a5239 100644 --- a/tests/test_port/test_misc.py +++ b/tests/test_port/test_misc.py @@ -12,3 +12,34 @@ def highlight_func(str_, lang, attrs): conf["options"]["highlight"] = highlight_func md = MarkdownIt(config=conf) assert md.render("``` a b c d \nhl\n```") == "
==hl\n==
\n"
+
+
+def test_ordered_list_info():
+ def type_filter(tokens, type_):
+ return [t for t in tokens if t.type == type_]
+
+ md = MarkdownIt()
+
+ tokens = md.parse("1. Foo\n2. Bar\n20. Fuzz")
+ assert len(type_filter(tokens, "ordered_list_open")) == 1
+ tokens = type_filter(tokens, "list_item_open")
+ assert len(tokens) == 3
+ assert tokens[0].info == "1"
+ assert tokens[0].markup == "."
+ assert tokens[1].info == "2"
+ assert tokens[1].markup == "."
+ assert tokens[2].info == "20"
+ assert tokens[2].markup == "."
+
+ tokens = md.parse(" 1. Foo\n2. Bar\n 20. Fuzz\n 199. Flp")
+ assert len(type_filter(tokens, "ordered_list_open")) == 1
+ tokens = type_filter(tokens, "list_item_open")
+ assert len(tokens) == 4
+ assert tokens[0].info == "1"
+ assert tokens[0].markup == "."
+ assert tokens[1].info == "2"
+ assert tokens[1].markup == "."
+ assert tokens[2].info == "20"
+ assert tokens[2].markup == "."
+ assert tokens[3].info == "199"
+ assert tokens[3].markup == "."