Loops? #1094
-
Hello! I'm evaluating evaluating remark for a project where I need to render templates to markdown. Currently I am using jinja, but this means that I have two passes - python and javascript - and there is brittleness around encoding issues. Finally, adding the remark ecosystem to my project means that I can take advantage of various remark features. However, one thing I am noticing about remark is that there is not a builtin or plugin capability for loops. In my jinja templates, I commonly have something like the following: # Items
{% for item in list %}
* `{% item %}`
{% endfor %} In remark, it appears I can write a custom component (or perhaps a directive?) to achieve this functionality generically. However, the lack of an existing plugin makes me wonder if I'm thinking about remark incorrectly, and/or trying to make it do something that it is not intended to do. Is this the case? If not, what is the best way to achieve the above example? Thanks! EDIT: note that there is a bug in the above example that I did not immediately notice, which is that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
CommonMark and GitHub Flavor Markdown (GFM) are both document languages, they have neither a concept nor an implementation of anything Jinja-template-like. To give a better answer there are a few pieces of context which would be needed:
Without those answers I can only offer some sweeping generalizations of possible solutions, which may or may not work for your specific use case. 1. Create a plugin to edit the Abstract Syntax TreeWorking directly with text can inherently be a bit brittle, that is what remark provides an Abstract Syntax Tree (AST) called 2. Use remark-directive and create a plugin to replace directives
# items
:::list-from-json{file-path='./list.json'} 3. Use MDX and use JS or Components to add contentMDX adds JSX to Markdown https://mdxjs.com/ An example of a MDX file with a loop # item
# items
<ul>
{props.items.map((item) => (
<li>{item}</li>
))}
</ul> An example of a MDX file using a component to generate content based off a JSON file: # items
<ListFromJson file="./items.json" /> |
Beta Was this translation helpful? Give feedback.
CommonMark and GitHub Flavor Markdown (GFM) are both document languages, they have neither a concept nor an implementation of anything Jinja-template-like.
Their goal is simplicity for static content, templates are not particularly simple or static 😅
To give a better answer there are a few pieces of context which would be needed:
Without those answers I can only offer some sweeping generalizations of possible solutions, which may or may not work for y…