SyntaxHighlighter.js 1.3 KB

123456789
  1. export default class SyntaxHighlighter{constructor(mimeType,stripExtraWhitespace){this._mimeType=mimeType;this._stripExtraWhitespace=stripExtraWhitespace;}
  2. createSpan(content,className){const span=createElement('span');span.className=className.replace(/\S+/g,'cm-$&');if(this._stripExtraWhitespace&&className!=='whitespace'){content=content.replace(/^[\n\r]*/,'').replace(/\s*$/,'');}
  3. span.createTextChild(content);return span;}
  4. syntaxHighlightNode(node){const lines=node.textContent.split('\n');let plainTextStart;let line;return self.runtime.extension(TextUtils.TokenizerFactory).instance().then(processTokens.bind(this));function processTokens(tokenizerFactory){node.removeChildren();const tokenize=tokenizerFactory.createTokenizer(this._mimeType);for(let i=0;i<lines.length;++i){line=lines[i];plainTextStart=0;tokenize(line,processToken.bind(this));if(plainTextStart<line.length){const plainText=line.substring(plainTextStart,line.length);node.createTextChild(plainText);}
  5. if(i<lines.length-1){node.createTextChild('\n');}}}
  6. function processToken(token,tokenType,column,newColumn){if(!tokenType){return;}
  7. if(column>plainTextStart){const plainText=line.substring(plainTextStart,column);node.createTextChild(plainText);}
  8. node.appendChild(this.createSpan(token,tokenType));plainTextStart=newColumn;}}}
  9. self.UI=self.UI||{};UI=UI||{};UI.SyntaxHighlighter=SyntaxHighlighter;