Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Markdown frontmatter with env.metadata #947

Open
holmboe opened this issue Jul 17, 2024 · 4 comments
Open

Access Markdown frontmatter with env.metadata #947

holmboe opened this issue Jul 17, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@holmboe
Copy link

holmboe commented Jul 17, 2024

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"] }}
@holmboe holmboe added the bug Something isn't working label Jul 17, 2024
@holmboe
Copy link
Author

holmboe commented Jul 17, 2024

It could be that #860 is also experiencing the same behavior/bug as I am.

@holmboe
Copy link
Author

holmboe commented Jul 19, 2024

I am seeing some weird behaviour with accessing the metadata in general (outside the described issue above).

  1. After a make clean and a single build the metadata is not present
  2. It takes a second build to have the metadata show up

So in my case I am doing make clean; make livehtml and then making a small insignificant change in a file only to trigger a second build. After the second build the data is shown.

This could potentially be reported as a second bug somewhere (probably not in myst-parser) however that will have to be handled separately.

@holmboe
Copy link
Author

holmboe commented Jul 19, 2024

I have tested with updated dependencies, same results:

docutils                      0.21.2
markdown-it-py                3.0.0
mdit-py-plugins               0.4.1
Sphinx                        7.4.6

@chrisjsewell
Copy link
Member

chrisjsewell commented Aug 5, 2024

Heya, metadata is collected in a transform, after the full document is parsed: https://github.com/sphinx-doc/sphinx/blob/05cc39d9b2a64b09504c06bfc2299aad96c85ccd/sphinx/environment/collectors/metadata.py#L28

so no you should not be relying on env.metadata during parsing (this would be the same for e.g. directives), and this is not strictly the same as the markdown front-matter.

you can already do this though with:

---
myst:
  substitutions:
    last_review_date: 1970-09-08
---
The last review date of _this_ file was on {{ last_review_date }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants