且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为CodeMirror创建新模式

更新时间:2023-01-18 18:12:13

在Mustache示例中有特殊处理,因为它需要处理2个字符的分隔符(例如,在'{{''}}'中有两个字符。我以前从未使用过CodeMirror,所以这只是一个猜测,不过请尝试这样的事情:

There is special handling in the Mustache example because it needs to handle 2-character delimiters (e.g. there are two characters in '{{' and '}}'). I've never used CodeMirror before, so this is just a guess, but try something like this:

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);
});




编辑



有效(尽管它也突出显示小写字母的单词)

it works (though it highlights words with lowercase letters too)

应该有效:

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;
}