Prevent CKEditor from formatting code in source mode

梦想的初衷 提交于 2019-12-03 01:30:18

My solution to this was to use comments in my system, but before feeding the page content to CKEditor, convert them to custom HTML tags. Then, upon save, convert them back to my comment tags.

For your syntax that would be something like this in PHP. Before printing the page content to the textarea:

$content = str_replace(array('<!-- src -->','<!-- end src -->'),array('<protected>','</protected>'),$content);

Before saving the resulting content:

$content = str_replace(array('<protected>','</protected>'),array('<!-- src -->','<!-- end src -->'),$content);

In the CKEditor configuration:

protectedSource:[/<protected>[\s\S]*<\/protected>/g]

Hope that helps!

Todd Kamin

I wanted to preserve newlines in my source, and the protectedSource feature works well for that. I added this to my config.js:

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

config.allowedContent=true; will do the trick

Here is the full HTML code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CKEditor</title>
        <script src="http://cdn.ckeditor.com/4.5.10/standard/ckeditor.js"></script>
    </head>
    <body>
        <textarea name="editor1"></textarea>
        <script>
            CKEDITOR.config.allowedContent=true;
            CKEDITOR.replace( 'editor1' );
        </script>
    </body>
</html>

I solved this problem by simply surrounding the back-end output of edit form page with a conditional on a $_GET variable - when you click on "Expert Mode" it loads a plain textarea instead of the ckeditor system. Your invocation of the ckeditor object will vary depending on your setup. ( I have a custom class that calls/builds the editor object )

                <div id="postdivrich" class="postarea">
<?php
if( isset( $_GET['expert'] ) )
{
    print "<div style=\"text-align:right;\"><a href=\"/admin/ckeditor/edit.php?node={$nNode}\">Editor mode</a></div>\n";
    print "<textarea name=\"content\" style=\"height:400px;width:{$nEwidth}px;\">{$aDoc['content']}</textarea>\n";
}
else
{
    print "<div style=\"text-align:right;\"><a href=\"/admin/ckeditor/edit.php?node={$nNode}&expert=true\">Expert mode</a></div>\n";
    require_once( 'admin/editor.class.php' );
    $aDoc['content'] = str_replace( "\r", '', str_replace( "\n", '', nl2br( $aDoc['content'] ) ) );
    $oEditor = new setEditor( $aDoc['content'], $nEwidth, "400", 'content' );
    $oEditor->ShowEditor();
}
?>
                </div>
Alex J

Does this answer help? Basically you can turn off the options adding a javascript, it looks like.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!