防止Jinja代码在CKEditor 4中包装为一行?

时间:2019-02-09 06:26:48

标签: django ckeditor jinja2

我出于jinja html模板代码的目的使用CKEditor4.x。因此,我尝试在CKEditor的源代码视图中将其配置为支持jinja语言。

出于这种考虑,我希望编辑器不要将我的jinja代码包装为一行。例如,如果我在编辑器的源代码视图中编写以下代码:

{% if name=='hello' %}
    {{ 'hello' }} 
{%else%}
    {{ 'what is your name?' }} 
{% endif %}

然后我将所见即所得视图再次切换回源代码,我希望它与上述代码相同。

但是,目前,它会将上述代码包装为一行,如下所示:

{% if name=='hello' %}{{ 'hello' }} {%else%} {{ 'what is your name?' }} {% endif %}

这是我当前的config.js文件中的文件:

/**
 * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see https://ckeditor.com/legal/ckeditor-oss-license
 */

CKEDITOR.editorConfig = function( config ) {
    // Define changes to default configuration here. For example:

    config.removePlugins = 'easyimage, cloudservices';
    config.height = 800;
    config.toolbarCanCollapse = true;

    /* skin */
    config.skin = 'office2013';

    config.extraPlugins = 'codemirror,placeholder,textindent,floating-tools,textselection,tableresizerowandcolumn,quicktable';
    // 1. Codemirror
       config.codemirror = {
         // Define the language specific mode 'htmlmixed' for html  including (css, xml, javascript), 'application/x-httpd-php' for php mode including html, or 'text/javascript' for using java script only 
        mode: 'django',

    };

    config.toolbar = 'Basic';


};

/* Protected code in editor */
CKEDITOR.config.protectedSource.push(/<\?[\s\S]*?\?>/g); // PHP

CKEDITOR.config.protectedSource.push(/\{\%[\s\S]*?\%\}/g); // Jinja {% %}

// CKEDITOR.config.protectedSource.push(/\{\{[\s\S]*?\}\}/g); // Jinja {{ }}

CKEDITOR.config.protectedSource.push(/\{\#[\s\S]*?\#\}/g); // Jinja {# #}

CKEDITOR.config.autoParagraph = false;

我希望必须有一个配置,我们可以设置为在CKEditor配置设置中存档上述效果。请帮助我将其存档。谢谢。

已添加

我已经用summernote编辑器测试了相同的情况,在这里我可以按需使用它,但是由于某种原因,我更喜欢CKEditor而不是那个summernote。我不清楚在Codemirror或编辑器本身中是否设置了此配置。谢谢。

1 个答案:

答案 0 :(得分:0)

非常感谢,经过几个小时的搜索,我设法自己找到了解决方案。这很简单。为了存档以上结果,我尝试将其添加到我的config.js文件中,并按我的意愿运行:

CKEDITOR.config.protectedSource = [/\r|\n/g];

希望这对像我这样遇到问题并找到解决方案的人有所帮助。谢谢。

相关问题