70 lines (62 sloc) 2.05 KB
<!doctype html>
<title>CodeMirror: Overlay Parser Demo</title>
<meta charset="utf-8"/>
<link rel=stylesheet href="../doc/docs.css">
<link rel="stylesheet" href="../lib/codemirror.css">
<script src="../lib/codemirror.js"></script>
<script src="../addon/mode/overlay.js"></script>
<script src="../mode/xml/xml.js"></script>
<style type="text/css">
.CodeMirror {border: 1px solid black;}
.cm-mustache {color: #0ca;}
<div id=nav>
<a href=""><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
<li><a href="../index.html">Home</a>
<li><a href="../doc/manual.html">Manual</a>
<li><a href="">Code</a>
<li><a class=active href="#">Overlay Parser</a>
<h2>Overlay Parser Demo</h2>
<form><textarea id="code" name="code">
<p>These are links to {{things}}:</p>
<li><a href="{{url}}">{{text}}</a></li>
CodeMirror.defineMode("mustache", function(config, parserConfig) {
var mustacheOverlay = {
token: function(stream, state) {
var ch;
if (stream.match("{{")) {
while ((ch = != null)
if (ch == "}" && == "}") {"}");
return "mustache";
while ( != null && !stream.match("{{", false)) {}
return null;
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), mustacheOverlay);
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {mode: "mustache"});
<p>Demonstration of a mode that parses HTML, highlighting
the <a href="">Mustache</a> templating
directives inside of it by using the code
in <a href="../addon/mode/overlay.js"><code>overlay.js</code></a>. View
source to see the 15 lines of code needed to accomplish this.</p>