Conditional language for filtering markdown content in lua based on metadata #10547
Replies: 2 comments 3 replies
-
A thought (not sure it's best): [conditional content]{ onlyif="global.meta.variable=someVal"} You need to pass two arguments to your conditional, a meta key (string) and its value (string). If meta keys are guaranteed not to contain '=' would do. If not you could provide a second syntax: [conditional content]{ onlyif="global.meta.variable=someVal"} It's not too difficult to extract the meta key with Lua patterns. One advantage is that a filter can easily target just those contents with conditional statements. A potential downside: not straightforwardly generalizable to other types of conditions (e.g. greater than for numbers), in case that turns out to be needed in the feature. A more markdown-heavy approach would be to have two attributes: [conditional content]{ ifmetavalue="global.meta.variable" equals="someVal"} Advantages: easy to code (no pattern matching);l allows filters to pick up just the conditionals; straightforwardly extensible to other type of conditionals. Downsides: verbose; you may not like the 'equals' attribute. |
Beta Was this translation helpful? Give feedback.
-
Not to dissuade you, but just in case you don't know this would typically be a job for a templating system that pre-processes the content. My favorite such system is tera¹ but there are many others in this conceptual space such as handlebars², Jinja, etc. These tools allow for meta data to be passed into a template and generate output with conditional blocks, looping over data, multiple outputs, etc. For your application you would use one of these as a pre-processor to take your Markdown template and parse the conditional blocks, placeholders, etc. That would generate the full final content to then pass to Pandoc for further manipulation using Lua for transformation as necessary and finally conversion to other formats. ¹ There are several different CLI tools implementing this system as well as being baked into other applications such as the Zola static site generator |
Beta Was this translation helpful? Give feedback.
-
I'm sure other people have solved this problem; I'm curious how they've done it though.
We use pandoc (and extensive lua filtering) to convert markdown to various outputs.
We want to add conditional statements into the markdown body that allow us to check metadata values for whether to include that content in the output document. We pass metadata into the lua filters (via pandoc) and essentially 'touch' all the content in lua.
The obvious place I think makes sense is in the bracketed span and fenced div attribute sections. Something like:
[conditional content]{ global.metadata.variable="someVal" }
where the content is included if the attribute
global.metadata.variable
value equalssomeVal
. If the attribute doesn't, the block is replaced with an empty set. The trick is formatting this condition such that it is HTML / XML compliant, but still easily human readable.I have some ideas as to how to do it - stuff like:
[conditional content]{ if.global.metadata.variable="someVal" }
[conditional content]{ global.metadata.variable="?someVal?" }
[conditional content]{ global.metadata.variable?="someVal" }
(I'm pretty sure this is not HTML / XML compliant)[conditional content]{ ?global.metadata.variable="someVal" }
(same for this)But, there are probably better ways?
Beta Was this translation helpful? Give feedback.
All reactions