CodeMirror Highlight only with no editor - Javascript CodeMirror

Javascript examples for CodeMirror:Highlight

Description

CodeMirror Highlight only with no editor

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Codemirror 2 - use as syntax highlighter</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js"></script> 
      <script type="text/javascript" src="https://codemirror.net/lib/codemirror.js"></script> 
      <script type="text/javascript" src="https://codemirror.net/mode/javascript/javascript.js"></script> 
      <link rel="stylesheet" type="text/css" href="https://codemirror.net/mode/javascript/javascript.css"> 
      <link rel="stylesheet" type="text/css" href="https://codemirror.net/lib/codemirror.css"> 
      <style id="compiled-css" type="text/css">

.code {//  www.  ja v  a2s . co  m
   margin: 10px 0;
}
.inline {
   display: inline-block;
   margin: 0;
}
.CodeMirror {
   height: auto!important;
}


      </style> 
      <script type="text/javascript">
    $(function(){
$('.code').each(function() {
    var $this = $(this),
        $code = $this.html(),
        $unescaped = $('<div/>').html($code).text();
    $this.empty();
    CodeMirror(this, {
        value: $unescaped,
        mode: 'javascript',
        lineNumbers: !$this.is('.inline'),
        readOnly: true
    });
});
    });

      </script> 
   </head> 
   <body>
       Some code 
      <code class="code inline">function inline() { console.log('inline code') }</code>
       inside a sentence. 
      <div class="code">
         function test() { return false; }
      </div> 
      <div class="code inline">
         &lt;h4&gt;unescape test&lt;/h4&gt;
      </div>  
   </body>
</html>

Related Tutorials