我只想突出显示如下所示的关键字:{KEYWORD}
(基本上大写单词包裹在单个{}
括号)
我通过复制代码来尝试这个小胡子覆盖演示 http://codemirror.net/demo/mustache.html,并将双括号替换为单括号:
CodeMirror.defineMode('mymode', function(config, parserConfig) {
var mymodeOverlay = {
token: function(stream, state) {
if (stream.match("{")) {
while ((ch = stream.next()) != null)
if (ch == "}" && stream.next() == "}") break;
return 'mymode';
}
while (stream.next() != null && !stream.match("{", false)) {}
return null;
}
};
return CodeMirror.overlayParser(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), mymodeOverlay);
});
但效果不是很好:)
有任何想法吗?
Mustache 示例中有特殊处理,因为它需要处理 2 个字符的分隔符(例如,中有两个字符'{{'
and '}}'
)。我以前从未使用过 CodeMirror,所以这只是一个猜测,但请尝试这样的操作:
CodeMirror.defineMode("mymode", function(config, parserConfig) {
var mymodeOverlay = {
token: function(stream, state) {
if (stream.match("{")) {
while ((ch = stream.next()) != null)
if (ch == "}") break;
return "mymode";
}
while (stream.next() != null && !stream.match("{", false)) {}
return null;
}
};
return CodeMirror.overlayParser(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), mymodeOverlay);
});
Edit
它有效(尽管它也突出显示带有小写字母的单词)
This should work:
token: function(stream, state) {
if (stream.match("{")) {
while ((ch = stream.next()) != null && ch === ch.toUpperCase())
if (ch == "}") break;
return "mymode";
}
while (stream.next() != null && !stream.match("{", false)) {}
return null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)