Friday, May 10, 2024
149
rated 0 times [  150] [ 1]  / answers: 1 / hits: 21576  / 13 Years ago, sat, april 2, 2011, 12:00:00

Can CodeMirror 2 be used to highlight code from a DIV or PRE tag (without the editor)?



Like CodeMirror 1 used to be able to do with the hightlightText() function?
For example here: http://codemirror.net/1/highlight.html, after you press run highlight (the highlighted text below)



Also can it highlight code from a inline element, like <code>, and keep the results inline, like Google's Prettify does?


More From » syntax-highlighting

 Answers
15

A much nicer and easier solution is to just set the readOnly property of the CodeMirror instance to true, like this:



$('.code').each(function() {

var $this = $(this),
$code = $this.html();

$this.empty();

var myCodeMirror = CodeMirror(this, {
value: $code,
mode: 'javascript',
lineNumbers: !$this.is('.inline'),
readOnly: true
});

});


Just add the class .code to the tag containing the code and it will be syntax highlighted. I've also added support for inline code, by using the class .inline.



Example on jsfiddle


[#92948] Thursday, March 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zachariaho

Total Points: 34
Total Questions: 87
Total Answers: 100

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;