django - 防止 Jinja 代码在 CKEditor 4 中打包成一行?
问题描述
我将 CKEditor 4.x 用于jinja
html 模板代码。因此,我尝试将其配置为jinja
在 CKEditor 的源代码视图中支持语言。
考虑到这个问题,我希望编辑器不要将我的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 或编辑器本身中设置的这个配置。谢谢。
解决方案
非常感谢,经过几个小时的搜索,我自己找到了解决方案。这很简单。为了存档上述结果,我尝试在我的config.js
文件中添加它,它可以按我的意愿工作:
CKEDITOR.config.protectedSource = [/\r|\n/g];
希望这对像我一样遇到问题并找到解决方案的人有所帮助。谢谢。