function render_preview() {
    document.getElementById('preview').innerHTML = 
        new commonmark.HtmlRenderer().render(new commonmark.Parser().parse(editor.getValue()));
}

var editor_type = document.getElementById("editor_script").getAttribute("data-type");
var editor_mode = null;

switch (editor_type) {
    case 'article':   editor_mode = 'markdown'; break;
    case 'file':      editor_mode = 'turtle'; break;
    case 'query':     editor_mode = 'application/sparql-query'; break;
    case 'template':  editor_mode = {name: 'jinja2', htmlMode: true}; break;
    case 'topic':     editor_mode = 'turtle'; break;
}

var editor = CodeMirror.fromTextArea(
    document.getElementById('content'),
    {
        mode: editor_mode,
        theme: 'default',
        lineNumbers: true,
        lineWrapping: true,
        scrollbarStyle: null,
        undoDepth: 1000,
        autofocus: true,
        spellcheck: false,
        autocapitalize: false
    });

editor.on('change', (cm, change) => { render_preview(); });

// Render initial document
render_preview();
    
