Kotsu
Kotsu copied to clipboard
Remove Gray Matter from templates before rendering
Currently Gray Matter stays in Nunjucks file during rendering. It's never printed out to final html, though, because it's placed before block declaration.
However, it gets rendered anyway. In most cases there seems to be no harm, but since Gray Matter can contain Nunjucks templates inside, and have its own escaping rules, this might result in sudden errors.
Consider this:
---
prop: '{{ gettext(''Some value'') }}'
Here we need to escape quotes, because they are already inside other quotes. That's totally fine for Gray Matter. However, Nunjucks will try to render that Gray Matter on this page (do not confuse it with render
module, which renders received Gray Matter and removes all unneeded escapings). And it will result in error, because Nunjucks does not allow escaping of quotes in those positions.
This results in some very quirky and hard to catch errors.
Obviously, we need to strip Gray Matter before giving files to Nunjucks render. And that's easy to do with Gray Matter — it already can return stripped versions of files.
But, unfortunately, so far there is no easy way to preprocess files before it is going to Nunjucks render.
Here are possible solutions:
-
Write temporary templates with removed Gray Matter. Very bad solution, because there will be a lot of disk writes and reads, loading of additional tasks and total mess with large number of templates.
-
Hope that https://github.com/mozilla/nunjucks/issues/808 will be someday implemented. Judging from Nunjucks pulse, it won't be ever implemented.
-
Rewrite again grunt-nunjucks-2-html to support preprocessing and postprocessing.
To do so, we will need to revert used Nunjucks method inside
grunt-nunjucks-2-html
fromnunjucks.render
(which renders from file) back tonunjucks.renderString
(which can render from string, file have to be loaded by Grunt). Can't tell how it will impact performance. -
I dunno — Gulp?