22
33from markdown_it import MarkdownIt
44from markdown_it .rules_block import StateBlock
5- from markdown_it .common .utils import charCodeAt , isSpace
5+ from markdown_it .common .utils import charCodeAt , isSpace , escapeHtml
66
77
88TARGET_PATTERN = re .compile (r"^\(([a-zA-Z\_\-\+\:]{1,36})\)\=\s*$" )
99
1010
1111def myst_block_plugin (md : MarkdownIt ):
12+ md .block .ruler .before (
13+ "blockquote" ,
14+ "myst_line_comment" ,
15+ line_comment ,
16+ {"alt" : ["paragraph" , "reference" , "blockquote" , "list" , "footnote_def" ]},
17+ )
1218 md .block .ruler .before (
1319 "hr" ,
1420 "myst_block_break" ,
@@ -22,6 +28,37 @@ def myst_block_plugin(md: MarkdownIt):
2228 {"alt" : ["paragraph" , "reference" , "blockquote" , "list" , "footnote_def" ]},
2329 )
2430 md .add_render_rule ("myst_target" , render_myst_target )
31+ md .add_render_rule ("myst_line_comment" , render_myst_line_comment )
32+
33+
34+ def line_comment (state : StateBlock , startLine : int , endLine : int , silent : bool ):
35+
36+ pos = state .bMarks [startLine ] + state .tShift [startLine ]
37+ maximum = state .eMarks [startLine ]
38+
39+ # if it's indented more than 3 spaces, it should be a code block
40+ if state .sCount [startLine ] - state .blkIndent >= 4 :
41+ return False
42+
43+ marker = charCodeAt (state .src , pos )
44+ pos += 1
45+
46+ # Check block marker /* % */
47+ if marker != 0x25 :
48+ return False
49+
50+ if silent :
51+ return True
52+
53+ state .line = startLine + 1
54+
55+ token = state .push ("myst_line_comment" , "" , 0 )
56+ token .attrSet ("class" , "myst-line-comment" )
57+ token .content = state .src [pos :maximum ].strip ()
58+ token .map = [startLine , state .line ]
59+ token .markup = chr (marker )
60+
61+ return True
2562
2663
2764def block_break (state : StateBlock , startLine : int , endLine : int , silent : bool ):
@@ -36,7 +73,7 @@ def block_break(state: StateBlock, startLine: int, endLine: int, silent: bool):
3673 marker = charCodeAt (state .src , pos )
3774 pos += 1
3875
39- # Check block marker /* + */:
76+ # Check block marker /* + */
4077 if marker != 0x2B :
4178 return False
4279
@@ -96,4 +133,12 @@ def target(state: StateBlock, startLine: int, endLine: int, silent: bool):
96133
97134def render_myst_target (self , tokens , idx , options , env ):
98135 content = tokens [idx ].content
99- return f'<div class=" admonition myst-target">target = <code>{ content } </code></div>'
136+ return (
137+ '<div class=" admonition myst-target">'
138+ f"target = <code>{ escapeHtml (content )} </code></div>"
139+ )
140+
141+
142+ def render_myst_line_comment (self , tokens , idx , options , env ):
143+ content = tokens [idx ].content
144+ return f"<!--- { escapeHtml (content )} --->"
0 commit comments