Auto-Indent html code

Hello,

I would like to find a way to auto indent some html code view in a codemirror editor.
I Found these demos:

I didn’t test these solutions yet, I wonder if there is another more recent script that achieve this.
I use the last (5) version of CodeMirror.

Thanks in advance

PS : I would like to find a script that autoindent an html code each time I load the html code. I store thiscode in .json files so, no newline or tab characters must be stored, the indentation must be done in the editor by the fly…

Well, I did it on server side (not very skilled in JS to make it alone…)
Here’s the PHP function used (only took in account the tags I need on my site, table blockquote and other block tags have been omitted…)

function html_indent($content) {
    $pattern = array(
        "@<h[1-6]{1}[^>]*>@",
        "@</h[1-6]{1}>@",
        "@<(p|div)[^>]*>@",
        "@<(dl|ul|ol)[^>]*>@",
        "@</(p|div|dl|ul|ol)>@",
        "@<(li|dt|dd)[^>]*>@"
    );
    $replace = array(
        "\n\n$0",
        "$0\n",
        "\n$0\n",
        "\n$0",
        "\n$0\n",
        "\n\t$0"
    );
    $indented_content = preg_replace($pattern, $replace, $content);
    return $indented_content;
}