I’m trying to write a syntax for Pandoc Markdown files. My articles in Pandoc Markdown always contain YAML blocks fenced by a pair of triple hyphens ---. Sublime Text syntax-highlights YAML keys and list hyphens nicely within the metadata block.
In the Drafts syntax file though, I haven’t been able to figure out a regex to match YAML keys and list hyphens only within metadata blocks. What I’m looking for is a regex like:
((\w+)(: )(.*))|((\w+)(:)(\s\- )(.*))
But what this regex does is to match any instance of key: value or key: \n- value anywhere in the document. I want it only to match instances within the YAML header. Is this even possible?
Please share this when you have it. YAML metadata blocks are also supported in GitLab-flavored Markdown and Multimarkdown. I regularly include metadata and it would be very useful to have it highlighted.
Thanks for this idea! When I plugged the first regex into https://regex101.com/, it threw a “catastrophic backtracking error” at me, so I made one of the quantifiers possessive. Specifically, I had to make
into (?s)(\\w+: )([^\\n]++)(?=.*^---)
for it to work. That seemed to do the trick. Except that I then noticed that if I manipulated the string and then re-formed it, the syntax highlighting got messed up. A screen recording to demonstrate:
Any idea what could be going on? Here are my patterns:
Well, no replies to this, so I suppose that it must be an issue with Drafts itself. My workaround will be to put the YAML header into a form that’s stably highlightable by Drafts. I’ll have to write a script to normalise the syntax before saving it for processing by Pandoc.