我想定义扩展其中一种可用模式(例如javascript)的自定义模式?我试过使用下面的代码,但没有效果。
CodeMirror.defineMode("foo", function (config, parserConfig) {
var fooOverlay = {
token: function (stream, state) {
var ch;
if (stream.match("{{")) {
while ((ch = stream.next()) != null)
if (ch == "}" && stream.next() == "}") {
stream.eat("}");
return "foo";
}
}
while (stream.next() != null && !stream.match("{{", false)) { }
return null;
}
};
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "javascript"), fooOverlay);
});
this.config = {
lineNumbers: true,
mode: 'foo'
};
更新
CodeMirror.defineSimpleMode("foo", {
start = [
{ regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string" },
{ regex: /'(?:[^\\]|\\.)*?(?:'|$)/, token: "string" },
{
regex: /(?:if|foreach|in|while)\b/,
token: "keyword"
},
{ regex: /true|false|null|undefined/, token: "atom" },
];
});