ckeditor---插入代码功能并高亮显示,结合syntaxhighlighter_2.1

给自己做了个小博客网站:www.luchg.com

我希望能和csdn的博客编辑一样,能够添加高亮显示代码的功能,看了网上很多文章,大部分都是抄袭的,一模一样,而且按照他们的方法有的也不能运行出来,所以我就自己摸索出来了一个方法:

当然我也是按照别人的方法做的,只不过我亲自做了出来,和大家分享一下,如有问题大家一起讨论


1、在"ckeditor/plugins/"目录下新建一个"insertcode"目录,然后在"insertcode"目录下新建一个"plugin.js",输入以下代码:

CKEDITOR.plugins.add('insertcode', {  
    requires: ['dialog'],  
    init: function(a){  
        var b = a.addCommand('insertcode', new CKEDITOR.dialogCommand('insertcode'));  
        a.ui.addButton('insertcode', {  
            label: a.lang.insertcode,  
            command: 'insertcode',  
            icon: this.path + 'images/code.jpg'  
        });  
        CKEDITOR.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');  
    }  
});  

2、在"insertcode"目录下增加"images"目录,放入一个"code.jpg"的图片,随便一个都可以(16*16)

3、在"insertcode"目录下增加"dialogs"目录,新建一个"insertcode.js",输入如下代码:

CKEDITOR.dialog.add('insertcode', function(editor){  
    var escape = function(value){  
        return value;  
    }; 
    
    return {  
        title: 'Insert Code Dialog',  
        resizable: CKEDITOR.DIALOG_RESIZE_BOTH,  
        minWidth: 720,  
        minHeight: 480,  
        contents: [{  
            id: 'cb',  
            name: 'cb',  
            label: 'cb',  
            title: 'cb',  
            elements: [{  
                type: 'select',  
                label: 'Language',  
                id: 'lang',  
                required: true,  
                'default': 'csharp',  
                items: [['ActionScript3', 'as3'], ['Bash/shell', 'bash'], ['C#', 'csharp'], ['C++', 'cpp'], ['CSS', 'css'], ['Delphi', 'delphi'], ['Diff', 'diff'], ['Groovy', 'groovy'], ['Html', 'xhtml'], ['JavaScript', 'js'], ['Java', 'java'], ['JavaFX', 'jfx'], ['Perl', 'perl'], ['PHP', 'php'], ['Plain Text', 'plain'], ['PowerShell', 'ps'], ['Python', 'py'], ['Ruby', 'rails'], ['Scala', 'scala'], ['SQL', 'sql'], ['Visual Basic', 'vb'], ['XML', 'xml']]  
            }, {  
                type: 'textarea',  
                style: 'width:700px;height:420px',  
                label: 'Code',  
                id: 'code',  
                rows: 31,  
                'default': ''  
            }]  
        }],  
        onOk: function(){  
            code = this.getValueOf('cb', 'code');  
            lang = this.getValueOf('cb', 'lang');  
            html = '' + escape(code) + '';  
          	//注意这里的代码,网上很多教程都是因为这里组合的时候出错而不能运行
            editor.insertHtml("<pre class='brush:"+ lang +"' >"+ html +"</pre>");
        },  
        onLoad: function(){  
        }  
    };  
});  

4、接下来就是把插件加入到CKEditor里了,我是直接修改CKEditor插件的核心文件的,因为我是把“插入代码”功能做为一个编辑器必要的功能来使用的。

     找到ckeditor目录下的"ckeditor.js",这里的代码是经过压缩的,我们用CKEditor原来的about插件做参考。查找"about",找到"fullPage:false,height:200,plugins:'about,basicstyles ",我们在"about"后面增加",insertcode",这里就变成"plugins:'about,insertcode ,basicstyles"。

    继续查找"about",找到"j.add('about',{init:function(l){var m=l.addCommand('about',new a.dialogCommand('about'));m.modes={wysiwyg:1,source:1};m.canUndo=false;l.ui.addButton('About',{label:l.lang.about.title,command:'about'});a.dialog.add('about',this.path+'dialogs/about.js');}}); ",我们在这个分号后面增加"j.add('insertcode', {requires: ['dialog'],init: function(l){l.addCommand('insertcode', new a.dialogCommand('insertcode'));l.ui.addButton('insertcode', {label: l.lang.insertcode.toolbar,command: 'insertcode',icon: this.path + 'images/code.jpg'});a.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');}}); "。

    接下来查找"i.toolbar_Basic=",这就是CKEditor默认的工具栏了,我们在这里加上",insertcode ",你可以加在你想要的位置。比如我的"['Maximize','ShowBlocks','-','insertcode'] "。


 5、进入"ckeditor/lang",分别在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code' ",",insertcode:'插入代碼' ",",insertcode:'插入代码' "。


 6、对CKEditor的修改已经OK了,还有最后一步就是在你需要高亮代码的页面引用:(注意,需要什么Brush就导如什么,或者直接全部导入)

这些文件在syntaxhighlighter里面的都有

<link type="text/css" rel="stylesheet" href="js/syntaxhighlighter/styles/shCore.css" />   
<link type="text/css" rel="stylesheet" href="js/syntaxhighlighter/styles/shThemeDefault.css" />   
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shCore.js" > </script>   
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushJava.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushCSharp.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushCss.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushJScript.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushPhp.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushXml.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushSql.js" ></script>
<script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushVB.js" ></script>

最后还要引入这个

这个文件在syntaxhighlighter3之前的版本中才有,我不知道现在的3里面用们代替它,有知道的可以告诉我

<script type= "text/javascript" >  
SyntaxHighlighter.config.clipboardSwf= 'js/syntaxhighlighter/scripts/clipboard.swf' ;  
SyntaxHighlighter.all();  
</script>  



好了,这样就可以了,可以到我的博客中看看效果的: http://lc448986375.gicp.net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要添加配置到CKEditor 5 Decoupled Document Build中,您需要执行以下步骤: 1. 打开您的`webpack.config.js`文件,这是您的CKEditor 5应用程序的配置文件。 2. 找到`module.exports`对象中的`module`属性,并在`rules`数组中添加以下代码: ```javascript { test: /\.css$/, use: ['style-loader', 'css-loader'], exclude: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/ }, { test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/, use: ['raw-loader'] }, { test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/, use: ['style-loader', 'css-loader'], }, { test: /ckeditor5-[^/\\]+[/\\]theme[/\\]loader\.js$/, use: [ { loader: 'style-loader', options: { injectType: 'singletonStyleTag', attributes: { 'data-cke': true } } }, { loader: 'postcss-loader', options: styles.getPostCssConfig({ themeImporter: { themePath: require.resolve('@ckeditor/ckeditor5-theme-lark') }, minify: true }) } ] } ``` 这将允许您在Decoupled Document Build中使用CSS样式。 3. 找到`module.exports`对象中的`plugins`属性,并在其中添加以下代码: ```javascript new CKEditorWebpackPlugin({ language: 'en', translationsOutputFile: /app[\\/]translations\.json$/, additionalLanguages: 'all' }) ``` 这将启用CKEditor 5的语言支持,并生成翻译文件。 4. 找到您的`src`文件夹,然后在其中创建一个名为`ckeditor.js`的文件。在该文件中,添加以下代码: ```javascript import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials'; import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; ClassicEditor.create(document.querySelector('#editor'), { plugins: [Essentials, Bold, Italic, Paragraph], toolbar: ['bold', 'italic'] }) .then(editor => { console.log(editor); }) .catch(error => { console.error(error); }); ``` 这将初始化ClassicEditor,并创建一个具有加粗和斜体工具栏按钮的编辑器。 5. 在您的应用程序中添加一个`<div>`元素,并为其指定一个`id`属性,例如: ```html <div id="editor"></div> ``` 这将使编辑器显示在您的应用程序中。 现在,您已经成功地将CKEditor 5 Decoupled Document Build集成到您的应用程序中,并且已经添加了一些自定义配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值