Search code examples
codemirror

How do I format HTML code in Code Mirror when the value is set to it?


I am using the Code Mirror plugin to edit HTML source of a page. The HTML code is fetched from database and set as value in Code Mirror. But, after setting the value it is displayed in the same format in which it was saved in the database. How can I display it in proper format? Thanks in advance.


Solution

  • There is a function called autoFormatRange

    editor.autoFormatRange(range.from, range.to);
    

    This fragment from the CodeMirror Group might be what you need:

    function autoFormat() {
        var totalLines = editor.lineCount();
        var totalChars = editor.getTextArea().value.length;
        editor.autoFormatRange({line:0, ch:0}, {line:totalLines, ch:totalChars});
    }
    

    http://codemirror.net/2/demo/formatting.html