Skip to content

👌 IMPROVE: Stop using YAML frontmatter-based directive options #49

Open
brunobeltran wants to merge 3 commits into
executablebooks:masterfrom
brunobeltran:bruno/use-modern-option-syntax
Open

👌 IMPROVE: Stop using YAML frontmatter-based directive options #49
brunobeltran wants to merge 3 commits into
executablebooks:masterfrom
brunobeltran:bruno/use-modern-option-syntax

Conversation

@brunobeltran

@brunobeltran brunobeltran commented Jan 4, 2026

Copy link
Copy Markdown

Instead, move to the more modern recommendation (per the MyST docs)
of using the "colon" syntax for directive options.

I understand this PR might be much more controversial than #48 but I put it "on top" to be more convenient for what I believe to be the "typical" mdformat_myst user---someone who wants to use large the latest and greatest MyST conventions and just wants a formatter that works out-of-the-box.

Happy to squash/split, or otherwise modify commits as necessary! For example, if just this change might go in independentlyof #48, I can easily split off f5a3bd5

Fixes #47

Instead, we move to the more modern recommendation (per the MyST docs)
of using the "colon" syntax for directive options.

@KyleKing KyleKing left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOGGER.warning("Invalid YAML in MyST directive options.")
return raw_content
if parsed:
formatted += "\n".join([f":{k}: {v}" for k, v in parsed.items()]) + "\n\n"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a way for people to opt-out of the newer formatting with a configuration option or do you think most people would prefer these changes?

@KyleKing
KyleKing self-requested a review January 6, 2026 03:26
@KyleKing

Copy link
Copy Markdown
Collaborator

Just following up, I would be happy to approve and mere, but I don't have the context on myst usage to know how much of a breaking change this would be and if we would want to support both (and if so, should it be opt-out of the newer formatting?)

@marcosmolla

Copy link
Copy Markdown

It would be great to have the option to opt for the colon syntax. Maybe it can be an opt-in option in the .mdformat.toml?
My issue with the --- is that an option like hidden, looks okay with the colon syntax (:hidden:) but like it is missing a value in the dashed syntax (hidden: )

LOGGER.warning("Invalid YAML in MyST directive options.")
return raw_content
if parsed:
formatted += "\n".join([f":{k}: {v}" for k, v in parsed.items()]) + "\n\n"

@marcosmolla marcosmolla Jul 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the problem here, that yaml.load from above will parse valueless options as {"hidden": None}, and then the emitted f-string would become: :hidden: None? That would sort of defeat the purpose.

for name, plugin in mdformat.plugins.PARSER_EXTENSIONS.items()
if plugin in active_plugins
]
formatted += mdformat.text(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directive bodies are not always CommonMark. toctree entries use title <target> syntax; I think, running them through mdformat.text escapes the < to \<. This would rewrite every entry in a toctree, and probably break them.

def format_directive_content(raw_content: str, context) -> str:
unformatted_yaml, content = parse_opts_and_content(raw_content)
formatted = ""
if unformatted_yaml is not None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't parse_opts_and_content return None if the directive has no options but does have content, i.e. line 156? In that case, directives without options but with a body would lose their entire body.

LOGGER.warning("Invalid YAML in MyST directive options.")
return raw_content
if parsed:
formatted += "\n".join([f":{k}: {v}" for k, v in parsed.items()]) + "\n\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this line has another problem. If you have a code-cell

```{code-cell} python
  ---
  tags: [hide-input, remove-output]
  label: my-cell
  ---
  print("hello")

the formatting works fine for scalars, but if there is a list it yields "['hide-input', 'remove-output']" but what it should look like is "('hide-input', 'remove-output')".

So this line needs to differentiate between:

  1. flags (just return bare :key:)
  2. scalars
  3. lists (comma separated lists in round brackets)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why is YAML synxtax preffered over Colon syntax for directive blocks?

3 participants