From 2e312e1da51986948da514f0e41f1dbd5a647260 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Thu, 2 Dec 2021 09:07:40 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20MyST=20role=20syntax?= =?UTF-8?q?=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match characters instead of using regex. This now allows for an unlimited name length and new lines in the content. --- mdit_py_plugins/myst_role/index.py | 37 +++++++++++++++++++++------ tests/fixtures/myst_role.md | 41 +++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/mdit_py_plugins/myst_role/index.py b/mdit_py_plugins/myst_role/index.py index 2d5577a..53ef60e 100644 --- a/mdit_py_plugins/myst_role/index.py +++ b/mdit_py_plugins/myst_role/index.py @@ -4,7 +4,7 @@ from markdown_it.common.utils import escapeHtml from markdown_it.rules_inline import StateInline -PATTERN = re.compile(r"^\{([a-zA-Z0-9\_\-\+\:]{1,36})\}(`+)(?!`)(.+?)(?' - f"{{{name}}}[{escapeHtml(token.content)}]" - "" + '' f"{{{name}}}[{escapeHtml(token.content)}]" "" ) diff --git a/tests/fixtures/myst_role.md b/tests/fixtures/myst_role.md index c047193..4b2c248 100644 --- a/tests/fixtures/myst_role.md +++ b/tests/fixtures/myst_role.md @@ -3,14 +3,30 @@ Basic: . {abc}`xyz` . -

{abc}[xyz]

+

{abc}[xyz]

+. + +Multiple: +. +{abc}`xyz`{def}`uvw` +. +

{abc}[xyz]{def}[uvw]

. Surrounding Text: . a {abc}`xyz` b . -

a {abc}[xyz] b

+

a {abc}[xyz] b

+. + +New lines: +. +{abc}`xy +z` `d +e` +. +

{abc}[xy z] d e

. In Code: @@ -20,12 +36,18 @@ In Code:

{abc}`xyz`

. +Empty content: +. +{name}`` a +. +

{name}`` a

+. Surrounding Code: . `a` {abc}`xyz` `b` . -

a {abc}[xyz] b

+

a {abc}[xyz] b

. In list: @@ -33,7 +55,7 @@ In list: - {abc}`xyz` . . @@ -42,7 +64,7 @@ In Quote: > {abc}`xyz` b .
-

{abc}[xyz] b

+

{abc}[xyz] b

. @@ -50,7 +72,14 @@ Multiple ticks: . {abc}``xyz`` . -

{abc}[xyz]

+

{abc}[xyz]

+. + +Inner tick: +. +{abc}``x`yz`` +. +

{abc}[x`yz]

. Unbalanced ticks: