Skip to content

Instantly share code, notes, and snippets.

@kylefox
Created November 11, 2011 00:02
Show Gist options
  • 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);
});
@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