Skip to content

Instantly share code, notes, and snippets.

@kylefox
Created November 11, 2011 00:02
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kylefox/1356686 to your computer and use it in GitHub Desktop.
Save kylefox/1356686 to your computer and use it in GitHub Desktop.
Liquid syntax highlighting for CodeMirror.
/*
This overlay provides a 'liquid' mode to the excellent CodeMirror editor (http://codemirror.net/).
Add something like this to your CSS:
.cm-liquid-tag {
color: #32273f;
background: #ead9ff;
}
.cm-liquid-variable {
color: #29739b
background: #c2e0f0;
}
*/
CodeMirror.defineMode("liquid", function(config, parserConfig) {
var liquidOverlay = {
token: function(stream, state) {
// Variables.
if (stream.match("{{")) {
while ((ch = stream.next()) != null)
if (ch == "}" && stream.next() == "}") break;
return "liquid-variable";
}
// Tags.
if(stream.match("{%")) {
while ((ch = stream.next()) != null)
if (ch == "%" && stream.next() == "}") break;
return "liquid-tag";
}
while (stream.next() != null && !stream.match("{{", false) && !stream.match("{%", false)) {}
return null;
}
};
return CodeMirror.overlayParser(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), liquidOverlay);
});
@alexkravets
Copy link

Thanks a lot for this! I've updated pull request for Locomotive with CoffeeScript!

@subimage
Copy link

Appreciate this thanks

@noahpryor
Copy link

Nice

@vyorkin
Copy link

vyorkin commented Apr 6, 2016

whats overlayParser? where it comes from?

@bbugh
Copy link

bbugh commented Apr 23, 2020

There's a more complete liquid mode for CodeMirror here: https://github.com/axtro/codemirror_liquid_mode. It supports variables, arguments, operators, etc. It's written for pre-ES6 JavaScript, but it still works for us today.

To make this gist or the mode I linked work for newer CodeMirror (I think 3+), you have to replace CodeMirror.overlayParser with CodeMirror.overlayMode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment