Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nunjucks in md files #11

Closed
probins opened this issue Dec 13, 2020 · 11 comments
Closed

Nunjucks in md files #11

probins opened this issue Dec 13, 2020 · 11 comments
Labels
enhancement New feature or request

Comments

@probins
Copy link
Contributor

probins commented Dec 13, 2020

I'm converting an old Jekyll site to Lume, and one issue I'm running into is that the md files contain {{ ... }}, but it looks like these are not being parsed. https://lumeland.github.io/plugins/nunjucks/ says "just place the .njk or .html file outside the _includes folder", but in my case these are .md files, not .html. I tried a simple test with a layout which includes an md file with {{ content | safe }}: if I put {{ myData }} in the layout file, it is parsed correctly, but if I put it in the md file, it is included unparsed in the output html. Is this intentional, or a bug of some sort? Or do I have to define something else to get the Nunjucks variables in md files parsed?

@oscarotero
Copy link
Member

At this moment, there's no way to mix nunjucks and markdown. Maybe I could create a new file extension, let's say *.md.njk, so the file execute nunkjucks first and then render the result as markdown.

Anyway, one way to do this now is by changing the extensions of these files. For example, instead index.md, change to index.njk. So, even if the content is markdown code, it's transformed by nunjuks. Then, use the wrapper layout of the page to render markdown, for example:

index.njk:

---
title: Hello world
layout: main.njk
---
# {{ title }}

main.njk:

<html>
    <body>
         {{ content | md | safe }}
    </body>
</html>

@probins
Copy link
Contributor Author

probins commented Dec 14, 2020

yes, that works, thanks.

Rather than creating a new extension, I'd suggest making the whole process more configurable. Static sites can be built from any number of sources, for example, from cloud-based databases. They aren't useful for data that changes frequently, but they're perfect for publishing other data at little or no cost. Templates can be used in any type of file, for example, in js or css. So, it would be good to have the option to define in the config file which part of the build is run in which order, for example, run this script to fetch data from database, then run Nunjucks on the md files in dirA, then convert md files in dirB to html, and so on. There's currently the ability to run scripts from the command line, but it would also be useful to be able to just run Nunjucks or md conversion on a directory or even a single file, without having the recreate the whole site every time.

No hurry, of course, I can use what's there atm. Just something to think about for future development. 😄

@oscarotero
Copy link
Member

1tty has a nice option templateEngineOverride to do that: https://www.11ty.dev/docs/languages/#overriding-the-template-language

Having something like that, you could configure how a specific file is rendered or, if you save the config value it in a _data.* file, it would affect to all files in that directory.
What do you think?

@probins
Copy link
Contributor Author

probins commented Dec 15, 2020

yes, having the ability to specify md only or md+template language both globally and at a directory level would solve this particular problem. I found a further problem here yesterday: I set up a couple of njk files as you suggested, which worked, but I then added some md files in the same directory, and it stopped working. I had to put the md files in a separate subdir for it to work again.

I can put other suggested improvements to the flow in separate issues.

@oscarotero oscarotero added the enhancement New feature or request label Dec 16, 2020
@oscarotero
Copy link
Member

Ok, I've added support for the templateEngine variable.
Starting from v0.10.0 (not released yet) you can do things like this: https://github.com/lumeland/test/blob/master/custom-template.md (run njk first and then md) and the result is https://github.com/lumeland/test/blob/master/_expected/custom-template.html

@probins
Copy link
Contributor Author

probins commented Dec 16, 2020

yeah, that's much more flexible, thanks. Are you planning on releasing 0.10 soon? If so, I'll wait. Otherwise, I could download the current master and try with that.

@oscarotero
Copy link
Member

Probably today

@oscarotero
Copy link
Member

v0.10.0 released.

@probins
Copy link
Contributor Author

probins commented Dec 17, 2020

I can't get this to work. To simplify, if mydata consists of a list of title and no, in my page file page.html, I have:

---
layout: layout.html
templateEngine: njk,md
---
{% for title, no in mydata %}
* {{ title }}: [{{ no }}](/items/{{ no }}.html){% endfor %}

and I'm ending up with lines of:

* title: [no](/items/no.html)

where title and no are the correct values for each line.

So, Nunjucks is substituting the data correctly, but the result is not being converted from md into html.

Am I missing something?

@oscarotero
Copy link
Member

I just tested your example and it works fine to me:
https://github.com/lumeland/test/commit/4550c5b2c430930004b3d95627636f96a3618c91

Maybe it is something related with cache (again). Try to clean deno cache and reinstall lume again.

@probins
Copy link
Contributor Author

probins commented Dec 17, 2020

yes, now works; it was indeed the cache. Looks like cache reload does not work properly. ☹️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants