micromark / micromark-extension-frontmatter

micromark extension to support frontmatter (YAML, TOML, etc)
https://unifiedjs.com
MIT License
20 stars 2 forks source link

Allow multiple matters w/ the same first character #2

Closed crystalfp closed 4 years ago

crystalfp commented 4 years ago

YAML front matter normally starts with --- and ends with ---. I and pandoc prefer instead to end the block with ... so I call syntax({type: 'yaml', marker: {open: "-", close: "."}}); Is it possible (or will be possible) to define the end marker as --- or ...? Thanks! mario

wooorm commented 4 years ago

There are a couple of examples in the readme that show that it works!

BTW: I suggest not calling it yaml, but something else. Yaml is typically both open and close three dashes, and that word is aliases as such in this project.

crystalfp commented 4 years ago

Well, not able to find the example in the readme. Could you point me to it? I tried with:

syntax([{type: "custom", marker: {open: "-", close: "."}},
            "yaml"])

And only the --- end is recognized.

BTW, putting type as "custom" and not "yaml" as you suggest: syntax({type: "custom", marker: {open: "-", close: "."}} does not recognize the --- one: two ... form.

wooorm commented 4 years ago
syntax({type: 'customYaml', fence: {open: 'stringUsedAsTheCompleteStart', close: 'stringUsedAsTheCompleteEnd'}})

See the third and fourth example for complete fences.

wooorm commented 4 years ago

don’t do what you provided:

syntax([{type: "custom", marker: {open: "-", close: "."}},
            "yaml"])

That support a custom syntax, and yaml.

This should work:

syntax({type: "custom", marker: {open: "-", close: "."}})

Or:

syntax({type: "custom", fence: {open: "---", close: "..."}})

Make sure to pass the same config to the other stuff too (mdast-util-frontmatter, etc)

crystalfp commented 4 years ago

Exactly. I want to accept my custom syntax, AND yaml. But don't worry, seems it is not possible natively. I will add a postprocessing step to recognize the first "thematicBreak" followed by a "heading" and change them to "yaml". Best mario

wooorm commented 4 years ago

Ahh, I guess the code expects different frontmatters to start with different characters. In your case, the first custom matter hooks into -, but then the second yaml preset overwrites it:

https://github.com/micromark/micromark-extension-frontmatter/blob/8b2df46c7055e6624f4fd5a7ea8c51e085435533/lib/syntax.js#L14

While we could change that, I would suggest not having such weirdness, when at all possible though 😅

crystalfp commented 4 years ago

Yes, you are right. But reality not always conforms to our desires... Anyway, I found an elegant workaround to cover the case when the end fence is also ---. Just FYI, when I'm at the root of the tree:

if(treeRoot.children[0].type === "thematicBreak" &&
   treeRoot.children[1].type === "heading") {
    const frontmatter = treeRoot.children[1].children[0].value;
    // Here I parse and use the front matter
    treeRoot.children.shift();
    treeRoot.children.shift();
}
// Then continue navigating recursively the AST
for(const item of treeRoot.children) visitTree(item);
wooorm commented 4 years ago

ok! Glad you’ve got a version working, and I think it also makes sense in here!

wooorm commented 4 years ago

Released a fix!

crystalfp commented 4 years ago

Fantastic! It works! Thanks a lot.