Skip to content

Access Markdown frontmatter with env.metadata #947

@holmboe

Description

@holmboe

What version of myst-parser are you using?

3.0.1

What version dependencies are you using?

docutils                      0.20.1
markdown-it-py                3.0.0
mdit-py-plugins               0.4.0
Sphinx                        7.4.5

What operating system are you using?

Linux

Describe the Bug

Accessing frontmatter to use as content in the current file fails because the metadata of the file itself is not present in the env.metadata dict.

Expected Behavior

It should be possible to access env.metadata for current file as env.metadata[env.docname]["key"], just as it is possible to access env.metadata["another-file"]["key"].

sphinx-doc/sphinx#2043 suggests that the "file wide metadata" should be accessible in the env.metadata dict, and it is for all other files, but just not the metadata that is in the file at hand.

To Reproduce

A working set up to showcase the behavior can be found in this Gist: https://gist.github.com/holmboe/51dd5231301e0e282fc86922a17362ca

However, for the sake of completeness, here is a short example Markdown file:

---
last_review_date: 1970-09-08
---
The last review date of _this_ file was on {{ env.metadata[env.docname]["last_review_date"] }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions