tree-sitter-grammars / tree-sitter-markdown

Markdown grammar for tree-sitter
MIT License
414 stars 52 forks source link

Feature request: add front matter support #32

Closed ikalnytskyi closed 2 years ago

ikalnytskyi commented 2 years ago

Rationale

Markdown is a quite popular choice for writing content for static sites or documentation. Majority of existing tooling supports so called "front matter" that allows to attach some metadata to the document.

See example https://gohugo.io/content-management/front-matter/

Suggestion

Front matter is neither a part of the standard nor standardize among existing tooling. Nevertheless, it seems community pretty much settled on two front matter formats:

It would be nice to support both YAML and TOML injections for this 2 most popular choices. I'm sure it would cover 99% of cases.

MDeiml commented 2 years ago

In nvim-treesitter this is done by querying content between two consecutive thematic breaks, but this is of course not very accurate and stable. So this seems like a good idea.

ikalnytskyi commented 2 years ago

Last time I tried nvim-treesitter, front-matter wasn't supported. 🤔 Are you saying that I can have a query that is not tied to AST elements? I.e. just simply check for --- at the beginning and make an injection for this?

MDeiml commented 2 years ago

Yes kind of. In realtiy it checks for thematic breaks, so it doesn't work to great. You can see the query in nvim-treesitter here. To use this you may need to update nvim-treesitter of course.

mhanberg commented 2 years ago

I have noticed that the neovim treesitter query works if there is an extra space at the end of the frontmatter. here is an example

https://user-images.githubusercontent.com/5523984/156380005-4f38dbb9-9a34-4ec3-98ae-d8b81af8f72e.mov

MDeiml commented 2 years ago

This is implemented in the newest version. (Update the nvim-treesitter plugin and run :TSUpdate to get the newest version)

MDeiml commented 2 years ago

Currently both pluses and minuses cause the content to be parsed as YAML as that is the behavior. I'm not sure why, probably because I made a mistake writing the query.

It's easy to fix with :TSEditQueryUserAfter injections markdown though and someone motivated could open a PR to nvim-treesitter