0

我目前正在将 django-ckeditor 用于网站,效果很好,但我需要一些文章内广告(google adsense)。保存寄存器后,代码被剥离,我不知道在哪里或如何设置 protectedSource 以允许标记。

我已经成功地允许脚本内容,但不是谷歌 Adsense 使用的 ins 标签。

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-XXXXXXXXXXXX"
     data-ad-slot="XXXXXXXXXXX"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

我当前在 settings.py 中的 CKEDITOR_CONFIGS 定义是这样的:

CKEDITOR_CONFIGS = {
    'custom': {
        'toolbar': 'Custom',
        'extraAllowedContent':'script ins',
        'removePlugins': 'stylesheetparser',
        #'protectedSource': ['/<ins class=\"adsbygoogle\"\>.*?<\/ins\>/g'],
        'toolbar_Custom': [
            {
                'items': [
                    'RemoveFormat', 'PasteFromWord',
                ]
            },
            {
                'items': [
                    'Styles', 'Format', 'Bold', 'Italic', 'Underline',
                ]
            },
            {
                'items': [
                    'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'
                ]
            },
            {'items': ['Blockquote', 'Outdent', 'Indent']},
            {'items': ['NumberedList', 'BulletedList']},
            {
                'items': [
                    'JustifyLeft', 'JustifyCenter',
                    'JustifyRight', 'JustifyBlock'
                ]
            },
            '/',
            {'items': ['Font', 'FontSize']},
            {'items': ['Source']},
            {'items': ['Link', 'Unlink']},
            {'items': ['Image', 'Flash', 'Table', 'HorizontalRule', 'Embed']},
            {'items': ['Smiley', 'SpecialChar', 'PageBreak', 'Iframe']}

        ],
        'extraPlugins': ','.join([
            'div',
            'autoembed',
            'embedsemantic',
        ]),
        'embed_provider': '//ckeditor.iframe.ly/api/oembed?url={url}&callback={callback}&api_key=abc123',
    },
    'default': {
        'toolbar': 'full',
        'removePlugins': 'stylesheetparser',
        'allowedContent': True,
    }
}

4

1 回答 1

0

我终于解决了这个问题。我添加了以下代码行:

config.protectedSource.push( /<ins[\s|\S]+?<\/ins>/g );
config.protectedSource.push( /<ins class=\"adsbygoogle\"\>.*?<\/ins\>/g );

在位于文件夹中的 config.js 文件中static/ckeditor/ckeditor/ 它不必对 CKEDITOR_CONFIGS 定义做任何事情,而是对 ckeditor 内部配置做任何事情。最终结果如下所示:

在此处输入图像描述

于 2020-04-02T05:54:48.807 回答