Skip to content

Commit f5a3bd5

Browse files
committed
Stop using YAML frontmatter-based directive options
Instead, we move to the more modern recommendation (per the MyST docs) of using the "colon" syntax for directive options.
1 parent 4b3ff3a commit f5a3bd5

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

mdformat_myst/_directives.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,9 @@ def format_directive_content(raw_content: str, context) -> str:
101101
except ruamel.yaml.YAMLError:
102102
LOGGER.warning("Invalid YAML in MyST directive options.")
103103
return raw_content
104-
formatted_yaml = dump_stream.getvalue()
105-
106-
# Remove the YAML closing tag if added by `ruamel.yaml`
107-
if formatted_yaml.endswith("\n...\n"):
108-
formatted_yaml = formatted_yaml[:-4]
109-
110-
# Convert empty YAML to most concise form
111-
if formatted_yaml == "null\n":
112-
formatted_yaml = ""
113-
114-
formatted += "---\n" + formatted_yaml + "---\n"
115-
if content:
104+
if parsed:
105+
formatted += "\n".join([f":{k}: {v}" for k, v in parsed.items()]) + "\n\n"
106+
if content.strip():
116107
# Get currently active plugin modules
117108
active_plugins = context.options.get("parser_extension", [])
118109

@@ -126,9 +117,17 @@ def format_directive_content(raw_content: str, context) -> str:
126117
formatted += mdformat.text(
127118
content, options=context.options, extensions=extension_names
128119
)
129-
formatted = formatted.rstrip("\n") + "\n"
130-
if formatted.endswith(":::\n"):
131-
formatted += "\n"
120+
if not formatted:
121+
return ""
122+
# In both the content-containing case (in which case we might have many terminal
123+
# newlines in the content) and the options-only case (in which case, we have
124+
# inserted two newlines above to separate the options from the non-existent content)
125+
# we want to ensure we end in _exactly_ one newline.
126+
formatted = formatted.rstrip("\n") + "\n"
127+
# Unless the last thing in the content is a colon-fence, which for consistency we
128+
# always add padding to.
129+
if formatted.endswith(":::\n"):
130+
formatted += "\n"
132131
return formatted
133132

134133

tests/data/fixtures.md

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -435,34 +435,30 @@ Content
435435
```
436436
.
437437
```{some-directive} args
438-
---
439-
option1: 1
440-
option2: hello
441-
---
438+
:option1: 1
439+
:option2: hello
440+
442441
Content
443442
```
444443
445444
```{some-directive} args
446-
---
447-
option1: 1
448-
option2: hello
449-
---
445+
:option1: 1
446+
:option2: hello
447+
450448
Content
451449
```
452450
453451
```{some-directive} args
454-
---
455-
option1: 1
456-
option2: hello
457-
---
452+
:option1: 1
453+
:option2: hello
454+
458455
Content
459456
```
460457
461458
```{some-directive} args
462-
---
463-
option1: 1
464-
option2: hello
465-
---
459+
:option1: 1
460+
:option2: hello
461+
466462
Content
467463
```
468464
.
@@ -476,8 +472,6 @@ Content
476472
```
477473
.
478474
```{some-directive} args
479-
---
480-
---
481475
Content
482476
```
483477
.
@@ -490,9 +484,7 @@ MyST directive no content
490484
```
491485
.
492486
```{some-directive} args
493-
---
494-
letter: a
495-
---
487+
:letter: a
496488
```
497489
.
498490
@@ -527,10 +519,9 @@ incididunt ut labore et dolore magna aliqua.
527519
:::
528520
.
529521
:::{admonition} MyST colon fenced directive with metadata
530-
---
531-
class: foo
532-
truc: bla
533-
---
522+
:class: foo
523+
:truc: bla
524+
534525
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
535526
incididunt ut labore et dolore magna aliqua.
536527
:::
@@ -540,18 +531,18 @@ MyST colon fenced directive with nested directive
540531
.
541532
::::{admonition} Parent directive
542533
:::{image} foo.png
543-
:class: foo
544-
:alt: An image
534+
---
535+
class: foo
536+
alt: An image
537+
---
545538
:::
546539
::::
547540
.
548541
::::{admonition} Parent directive
549542
550543
:::{image} foo.png
551-
---
552-
class: foo
553-
alt: An image
554-
---
544+
:class: foo
545+
:alt: An image
555546
:::
556547
557548
::::

0 commit comments

Comments
 (0)