MichaelHatherly / CommonMark.jl

A CommonMark-compliant Markdown parser for Julia.
Other
84 stars 11 forks source link

Add method `frontmatter` to easily extract frontmatter from a parsed document #21

Closed haberdashPI closed 3 years ago

haberdashPI commented 3 years ago

This is handy if you want to do other things with the front matter once having parsed the Markdown. I didn't see an obvious way to do it from the docs.

MichaelHatherly commented 3 years ago

Thanks @haberdashPI.

This is handy if you want to do other things with the front matter once having parsed the Markdown. I didn't see an obvious way to do it from the docs.

Definitely handy. I've been meaning to look into creating a "proper" API for the AST manipulation/querying. I'm still not 100% sure what it should actually look like though.

My one reservation about adding specialised getters like frontmatter is that I'm unsure where the line would be drawn, do we also want a codeblocks function that returns all the codeblocks in the AST (just as a silly example). Might be worth instead defining a tree manipulation API, maybe just leveraging AbstractTrees.jl for most of it, such that some hypothetical query(FrontMatter, ast) would do the trick, and could extend to more interesting things like map!(uppercase, query(Strong, ast)).

That said, I am fine with just going with the simple frontmatter for now (deprecations are easy enough if something changes in the future). This will need some docs, and tests in that case.

MichaelHatherly commented 3 years ago

CI looks to be failing possibly because there's a frontmatter(::Node) method already defined in src/writers.jl. It does the same thing pretty much as what you've added, so for now you should be able to just use that unexported one.

haberdashPI commented 3 years ago

Hah! Funny that I didn't find that... Well, now it's more discoverable!

And I agree, long-term it does probably make sense to generate some more general-purpose methods for access components of the document programmatically.

MichaelHatherly commented 3 years ago

Even more funny that I completely forgot I had written that function. Thanks for the contribution @haberdashPI.