ikatyang / tree-sitter-markdown

Markdown grammar for tree-sitter
https://ikatyang.github.io/tree-sitter-markdown
MIT License
180 stars 29 forks source link

support for front-matter #21

Closed kkharji closed 3 years ago

kkharji commented 3 years ago

Hey @ikatyang,

Is it possible to support identification of front-matter block. I'd like to inject yaml to it like

(fenced_code_block
  (info_string) @language
  (code_fence_content) @content)

Currently I'm getting for

---
title: GitHub Flavored Markdown
author: GitHub
---

=

thematic_break [0, 0] - [0, 3]
setext_heading [1, 0] - [3, 3]
  heading_content [1, 0] - [2, 14]
    text [1, 0] - [1, 31]
    soft_line_break [1, 31] - [2, 0]
    text [2, 0] - [2, 14]
  setext_heading_underline [3, 0] - [3, 3]

Thanks

ikatyang commented 3 years ago

I'm not sure what did you mean by injecting yaml, but according to Multi-language Documents, there should be a front-matter language to be used for extracting the yaml part and the markdown part, and those extracted parts should be passed to the corresponding language to get the final tree then.

kkharji commented 3 years ago

Hey @ikatyang I meant more as a simple identification because the syntax tree of ---\frontmatter--/ is wrong.

I'll look into multiple languages, i'm getting started with treesitter, feel free to close the issue if it doesn't make sense to identify frontmatter block like code_block

ikatyang commented 3 years ago

the syntax tree of ---\frontmatter--/ is wrong.

That's the expected syntax tree for parsers that follow CommonMark/GFM spec strictly, front matter is not part of the spec but an common extension to the Markdown language, and as far as I can tell there is no spec for how to parse it, its grammar is all up to the implementer.

feel free to close the issue if it doesn't make sense to identify frontmatter block like code_block

Since front matter is not part of the spec and it could also be used in other languages such as HTML, I think it'd be better to use a front-matter language to achieve this goal. I'm going to close this issue but feel free to comment if you have further questions.