diff --git a/mdit_py_plugins/myst_blocks/index.py b/mdit_py_plugins/myst_blocks/index.py index 16baaaa..d0e4cf6 100644 --- a/mdit_py_plugins/myst_blocks/index.py +++ b/mdit_py_plugins/myst_blocks/index.py @@ -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 (``+++``).""" @@ -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: @@ -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): diff --git a/tests/fixtures/myst_block.md b/tests/fixtures/myst_block.md index 38d0771..90bf9d3 100644 --- a/tests/fixtures/myst_block.md +++ b/tests/fixtures/myst_block.md @@ -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: