From 448ea838176db1c150475f6812226d92e053919f Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Mon, 24 Jan 2022 17:12:21 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Crash=20when=20file?= =?UTF-8?q?=20ends=20with=20empty=20blockquote=20line=20(#186)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- markdown_it/rules_block/blockquote.py | 9 +++++++-- tests/test_port/test_no_end_newline.py | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/markdown_it/rules_block/blockquote.py b/markdown_it/rules_block/blockquote.py index c3e2d5c2..543c1f9a 100644 --- a/markdown_it/rules_block/blockquote.py +++ b/markdown_it/rules_block/blockquote.py @@ -154,8 +154,13 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool): # set offset past spaces and ">" initial = offset = state.sCount[nextLine] + 1 + try: + next_char: Optional[int] = state.srcCharCode[pos] + except IndexError: + next_char = None + # skip one optional space after '>' - if state.srcCharCode[pos] == 0x20: # /* space */ + if next_char == 0x20: # /* space */ # ' > test ' # ^ -- position start of line here: pos += 1 @@ -163,7 +168,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool): offset += 1 adjustTab = False spaceAfterMarker = True - elif state.srcCharCode[pos] == 0x09: # /* tab */ + elif next_char == 0x09: # /* tab */ spaceAfterMarker = True if (state.bsCount[nextLine] + offset) % 4 == 3: diff --git a/tests/test_port/test_no_end_newline.py b/tests/test_port/test_no_end_newline.py index b35423e1..5e7cf822 100644 --- a/tests/test_port/test_no_end_newline.py +++ b/tests/test_port/test_no_end_newline.py @@ -18,6 +18,7 @@ ("p", "

p

\n"), ("[reference]: /url", ""), (" indented code block", "
indented code block\n
\n"), + ("> test\n>", "
\n

test

\n
\n"), ], ) def test_no_end_newline(input, expected): From 99cde4362199d760db39fdaa8bac7cd9a8dc5b75 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 24 Jan 2022 18:47:59 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20NEW:=20Add=20`inline=5Fdefiniti?= =?UTF-8?q?ons`=20option=20(#187)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This option allows for `definition` token to be inserted into the token stream, at the point where the definition is located in the source text (in addition to the standard extraction to `env`). It is not currently part of the Markdown-It JS implementation, but is useful for cases where one wishes to capture a "loseless" syntax tree of the parsed Markdown (in conjunction with the `store_labels` option). --- markdown_it/rules_block/reference.py | 11 +++ tests/test_port/test_references.py | 8 ++ .../test_inline_definitions.yml | 94 +++++++++++++++++++ tox.ini | 2 +- 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 tests/test_port/test_references/test_inline_definitions.yml diff --git a/markdown_it/rules_block/reference.py b/markdown_it/rules_block/reference.py index bdd96ead..1704d806 100644 --- a/markdown_it/rules_block/reference.py +++ b/markdown_it/rules_block/reference.py @@ -187,6 +187,17 @@ def reference(state: StateBlock, startLine, _endLine, silent): state.line = startLine + lines + 1 + # note, this is not part of markdown-it JS, but is useful for renderers + if state.md.options.get("inline_definitions", False): + token = state.push("definition", "", 0) + token.meta = { + "id": label, + "title": title, + "url": href, + "label": string[1:labelEnd], + } + token.map = [startLine, state.line] + if label not in state.env["references"]: state.env["references"][label] = { "title": title, diff --git a/tests/test_port/test_references.py b/tests/test_port/test_references.py index eb669c12..32e389de 100644 --- a/tests/test_port/test_references.py +++ b/tests/test_port/test_references.py @@ -43,3 +43,11 @@ def test_store_labels(data_regression): src = "[a]\n\n![a]\n\n[a]: ijk" tokens = md.parse(src) data_regression.check([token.as_dict() for token in tokens]) + + +def test_inline_definitions(data_regression): + md = MarkdownIt() + md.options["inline_definitions"] = True + src = '[a]: url "title"\n- [a]: url "title"' + tokens = md.parse(src) + data_regression.check([token.as_dict() for token in tokens]) diff --git a/tests/test_port/test_references/test_inline_definitions.yml b/tests/test_port/test_references/test_inline_definitions.yml new file mode 100644 index 00000000..5ec210b1 --- /dev/null +++ b/tests/test_port/test_references/test_inline_definitions.yml @@ -0,0 +1,94 @@ +- attrs: null + block: true + children: null + content: '' + hidden: false + info: '' + level: 0 + map: + - 0 + - 1 + markup: '' + meta: + id: A + label: a + title: title + url: url + nesting: 0 + tag: '' + type: definition +- attrs: null + block: true + children: null + content: '' + hidden: false + info: '' + level: 0 + map: + - 1 + - 2 + markup: '-' + meta: {} + nesting: 1 + tag: ul + type: bullet_list_open +- attrs: null + block: true + children: null + content: '' + hidden: false + info: '' + level: 1 + map: + - 1 + - 2 + markup: '-' + meta: {} + nesting: 1 + tag: li + type: list_item_open +- attrs: null + block: true + children: null + content: '' + hidden: false + info: '' + level: 2 + map: + - 1 + - 2 + markup: '' + meta: + id: A + label: a + title: title + url: url + nesting: 0 + tag: '' + type: definition +- attrs: null + block: true + children: null + content: '' + hidden: false + info: '' + level: 1 + map: null + markup: '-' + meta: {} + nesting: -1 + tag: li + type: list_item_close +- attrs: null + block: true + children: null + content: '' + hidden: false + info: '' + level: 0 + map: null + markup: '-' + meta: {} + nesting: -1 + tag: ul + type: bullet_list_close diff --git a/tox.ini b/tox.ini index ff57b2a2..158faff1 100644 --- a/tox.ini +++ b/tox.ini @@ -13,7 +13,7 @@ usedevelop = true extras = linkify testing -commands = pytest tests/ {posargs} +commands = pytest {posargs:tests/} [testenv:py{36,37,38,39,310}-plugins] extras = testing From d6adf6652498ba8b4adf48911eccf61a4f382e3c Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 24 Jan 2022 18:57:05 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=9A=80=20RELEASE:=20v2.0.1=20(#188)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 +++++++ markdown_it/__init__.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd1e04da..1e463bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## 2.0.1 - 2022-24-01 + +- 🐛 FIX: Crash when file ends with empty blockquote line. +- ✨ NEW: Add `inline_definitions` option. + This option allows for `definition` token to be inserted into the token stream, at the point where the definition is located in the source text. + It is useful for cases where one wishes to capture a "loseless" syntax tree of the parsed Markdown (in conjunction with the `store_labels` option). + ## 2.0.0 - 2021-12-03 - ⬆️ Update: Sync with markdown-it v12.1.0 and CommonMark v0.30 diff --git a/markdown_it/__init__.py b/markdown_it/__init__.py index fc1cca90..887f4f53 100644 --- a/markdown_it/__init__.py +++ b/markdown_it/__init__.py @@ -1,5 +1,5 @@ """A Python port of Markdown-It""" __all__ = ("MarkdownIt",) -__version__ = "2.0.0" +__version__ = "2.0.1" from .main import MarkdownIt