Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions mdit_py_plugins/myst_blocks/index.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import itertools
import re

from markdown_it import MarkdownIt
from markdown_it.common.utils import escapeHtml, isSpace
from markdown_it.rules_block import StateBlock

TARGET_PATTERN = re.compile(r"^\(([a-zA-Z0-9\|\@\<\>\*\.\/\_\-\+\:]{1,100})\)\=\s*$")


def myst_block_plugin(md: MarkdownIt):
"""Parse MyST targets (``(name)=``), blockquotes (``% comment``) and block breaks (``+++``)."""
Expand Down Expand Up @@ -122,8 +119,12 @@ def target(state: StateBlock, startLine: int, endLine: int, silent: bool):
if state.sCount[startLine] - state.blkIndent >= 4:
return False

match = TARGET_PATTERN.match(state.src[pos:maximum])
if not match:
text = state.src[pos:maximum].strip()
if not text.startswith("("):
return False
if not text.endswith(")="):
return False
if not text[1:-2]:
return False

if silent:
Expand All @@ -133,17 +134,17 @@ def target(state: StateBlock, startLine: int, endLine: int, silent: bool):

token = state.push("myst_target", "", 0)
token.attrSet("class", "myst-target")
token.content = match.group(1)
token.content = text[1:-2]
token.map = [startLine, state.line]

return True


def render_myst_target(self, tokens, idx, options, env):
content = tokens[idx].content
return (
'<div class="myst-target">' f"target = <code>{escapeHtml(content)}</code></div>"
)
label = tokens[idx].content
class_name = "myst-target"
target = f'<a href="#{label}">({label})=</a>'
return f'<div class="{class_name}">{target}</div>'


def render_myst_line_comment(self, tokens, idx, options, env):
Expand Down
40 changes: 33 additions & 7 deletions tests/fixtures/myst_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,58 @@ a
<hr class="myst-block">
.


Target:
.
(a)=
.
<div class="myst-target">target = <code>a</code></div>
<div class="myst-target"><a href="#a">(a)=</a></div>
.

Target characters:
.
(a bc |@<>*./_-+:)=
.
<div class="myst-target"><a href="#a bc |@<>*./_-+:">(a bc |@<>*./_-+:)=</a></div>
.

Empty target:
.
()=
.
<p>()=</p>
.

Escaped target:
.
\(a)=
.
<p>(a)=</p>
.

Indented target:
.
(a)=
.
<div class="myst-target"><a href="#a">(a)=</a></div>
.

Target terminates other blocks:
.
a
(a)=
(a-b)=
- b
(a)=
(a b)=
> c
(a)=
.
<p>a</p>
<div class="myst-target">target = <code>a</code></div><ul>
<div class="myst-target"><a href="#a-b">(a-b)=</a></div><ul>
<li>b</li>
</ul>
<div class="myst-target">target = <code>a</code></div><blockquote>
<div class="myst-target"><a href="#a b">(a b)=</a></div><blockquote>
<p>c</p>
</blockquote>
<div class="myst-target">target = <code>a</code></div>
<div class="myst-target"><a href="#a">(a)=</a></div>
.

Comment:
Expand Down