Nunjucks / *.njk syntax highlighting #5740
-
Is there a way to have syntax highlighting for Nunjucks files? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
It isn't currently a supported language. See https://docs.helix-editor.com/master/guides/adding_languages.html There are some jinja2 tree-sitter grammars that could be used: https://github.com/search?q=tree-sitter-jinja2. (From what I understand, nunjucks is syntactically the same as jinja2.) |
Beta Was this translation helpful? Give feedback.
-
I'm sharing my configuration for nunjucks (it can also be adapted to jinja2):
remember to move highlight.scm from the "source" repository to the Also need to update grammar for html highlighting in templates add injections.scm file to path helix/runtime/queries/njk:
|
Beta Was this translation helpful? Give feedback.
-
Might I ask how I might get the appropriate behavior to expand/shrink/jump to syntax node behavior to include html nodes? I'm using helix 23.05 (7f5940b) The behavior I observe is that jumps to the next nunjucks node that isn't html. Below, the vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
<li class="nav-item"><a href="{{ entry.url }}"{% if entry.url == page.url %} aria-current="page"{% endif %}>{{ entry.title }}</a></li>
^^^^^^^^^^^ Highlighting looks good for those unmatched html nodes (key, operator, quoted-value) are appropriate colors. So I'm not sure if I just don't have the injection query proper Potentially useful output. ~ ❱ command cat ~/.config/helix/runtime/queries/njk/{injections.scm,highlights.scm}
((source_file) @injection.content
(#set! injection.combined)
(#set! injection.include-children)
(#set! injection.language html))
(expression) @string
(statement) @variable.builtin
(keyword) @keyword
(comment) @comment
(identifier) @parameter
(operator) @operator
(string) @string |
Beta Was this translation helpful? Give feedback.
It isn't currently a supported language. See https://docs.helix-editor.com/master/guides/adding_languages.html
There are some jinja2 tree-sitter grammars that could be used: https://github.com/search?q=tree-sitter-jinja2. (From what I understand, nunjucks is syntactically the same as jinja2.)