Skip to content

Commit 8508bf7

Browse files
authored
✨ NEW: Add myst_fence_as_directive config (#742)
1 parent 1e440e6 commit 8508bf7

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

myst_parser/config/main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ def _test_slug_func(text: str) -> str:
166166
return text[::-1]
167167

168168

169+
def check_fence_as_directive(
170+
inst: "MdParserConfig", field: dc.Field, value: Any
171+
) -> None:
172+
"""Check that the extensions are a sequence of known strings"""
173+
deep_iterable(instance_of(str), instance_of((list, tuple, set)))(inst, field, value)
174+
setattr(inst, field.name, set(value))
175+
176+
169177
@dc.dataclass()
170178
class MdParserConfig:
171179
"""Configuration options for the Markdown Parser.
@@ -250,6 +258,16 @@ def __repr__(self) -> str:
250258
},
251259
)
252260

261+
fence_as_directive: Set[str] = dc.field(
262+
default_factory=set,
263+
metadata={
264+
"validator": check_fence_as_directive,
265+
"help": "Interpret a code fence as a directive, for certain language names. "
266+
"This can be useful for fences like dot and mermaid, "
267+
"and interoperability with other Markdown renderers.",
268+
},
269+
)
270+
253271
number_code_blocks: Sequence[str] = dc.field(
254272
default_factory=list,
255273
metadata={

myst_parser/mdit_to_docutils/base.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,13 @@ def render_fence(self, token: SyntaxTreeNode) -> None:
743743
return self.render_restructuredtext(token)
744744
if name.startswith("{") and name.endswith("}"):
745745
return self.render_directive(token, name[1:-1], arguments)
746+
if name in self.md_config.fence_as_directive:
747+
options = {k: str(v) for k, v in token.attrs.items()}
748+
if "id" in options:
749+
options["name"] = options.pop("id")
750+
return self.render_directive(
751+
token, name, arguments, additional_options=options
752+
)
746753

747754
if not name and self.sphinx_env is not None:
748755
# use the current highlight setting, via the ``highlight`` directive,
@@ -1664,7 +1671,12 @@ def render_restructuredtext(self, token: SyntaxTreeNode) -> None:
16641671
self.current_node.extend(newdoc.children)
16651672

16661673
def render_directive(
1667-
self, token: SyntaxTreeNode, name: str, arguments: str
1674+
self,
1675+
token: SyntaxTreeNode,
1676+
name: str,
1677+
arguments: str,
1678+
*,
1679+
additional_options: dict[str, str] | None = None,
16681680
) -> None:
16691681
"""Render special fenced code blocks as directives.
16701682
@@ -1673,7 +1685,13 @@ def render_directive(
16731685
:param arguments: The remaining text on the same line as the directive name.
16741686
"""
16751687
position = token_line(token)
1676-
nodes_list = self.run_directive(name, arguments, token.content, position)
1688+
nodes_list = self.run_directive(
1689+
name,
1690+
arguments,
1691+
token.content,
1692+
position,
1693+
additional_options=additional_options,
1694+
)
16771695
self.current_node += nodes_list
16781696

16791697
def run_directive(

tests/test_renderers/fixtures/myst-config.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,31 @@ My paragraph
468468
<reference id_link="True" refid="title">
469469
reversed
470470
.
471+
472+
[fence_as_directive] --myst-fence-as-directive=unknown,admonition --myst-enable-extensions=attrs_block
473+
.
474+
```unknown
475+
```
476+
477+
{#myname .class1}
478+
{a=b}
479+
```admonition title
480+
content
481+
```
482+
.
483+
<document source="<string>">
484+
<system_message level="2" line="1" source="<string>" type="WARNING">
485+
<paragraph>
486+
Unknown directive type: 'unknown' [myst.directive_unknown]
487+
<system_message level="2" line="6" source="<string>" type="WARNING">
488+
<paragraph>
489+
'admonition': Unknown option keys: ['a'] (allowed: ['class', 'name']) [myst.directive_parse]
490+
<admonition classes="class1" ids="myname" names="myname">
491+
<title>
492+
title
493+
<paragraph>
494+
content
495+
496+
<string>:1: (WARNING/2) Unknown directive type: 'unknown' [myst.directive_unknown]
497+
<string>:6: (WARNING/2) 'admonition': Unknown option keys: ['a'] (allowed: ['class', 'name']) [myst.directive_parse]
498+
.

0 commit comments

Comments
 (0)