CMIContent Marketing InstituteContent directory

News · General · Archive

CodeMirror: JavaScript mode

Space Home News

.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}

CodeMirror

Home Manual Code

Language modes JavaScript

JavaScript mode

// Demo code (the actual new parser character stream implementation)

function StringStream(string) { this.pos = 0; this.string = string; }

StringStream.prototype = { done: function() {return this.pos >= this.string.length;}, peek: function() {return this.string.charAt(this.pos);}, next: function() { if (this.pos < this.string.length) return this.string.charAt(this.pos++); }, eat: function(match) { var ch = this.string.charAt(this.pos); if (typeof match == "string") var ok = ch == match; else var ok = ch && match.test ? match.test(ch) : match(ch); if (ok) {this.pos++; return ch;} }, eatWhile: function(match) { var start = this.pos; while (this.eat(match)); if (this.pos > start) return this.string.slice(start, this.pos); }, backUp: function(n) {this.pos -= n;}, column: function() {return this.pos;}, eatSpace: function() { var start = this.pos; while (/\s/.test(this.string.charAt(this.pos))) this.pos++; return this.pos - start; }, match: function(pattern, consume, caseInsensitive) { if (typeof pattern == "string") { function cased(str) {return caseInsensitive ? str.toLowerCase() : str;} if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) { if (consume !== false) this.pos += str.length; return true; } } else { var match = this.string.slice(this.pos).match(pattern); if (match && consume !== false) this.pos += match[0].length; return match; } } };

var editor = CodeMirror.fromTextArea(document.getElementById("code"), { lineNumbers: true, matchBrackets: true, continueComments: "Enter", extraKeys: {"Ctrl-Q": "toggleComment"} });

JavaScript mode supports several configuration options

json which will set the mode to expect JSON data rather than a JavaScript program. jsonld which will set the mode to expect JSON-LD linked data rather than a JavaScript program ( demo ). typescript which will activate additional syntax highlighting and some other things for TypeScript code ( demo ). statementIndent which (given a number) will determine the amount of indentation to use for statements continued on a new line. wordCharacters , a regexp that indicates which characters should be considered part of an identifier. Defaults to /[\w$]/ , which does not handle non-ASCII identifiers. Can be set to something more elaborate to improve Unicode support.

MIME types defined: text/javascript , application/json , application/ld+json , text/typescript , application/typescript .

More in Archive · More in General · More in News