商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > CKEditor中加入syntaxhighlighter代码高亮插件

CKEditor中加入syntaxhighlighter代码高亮插件

  发布于2026-07-22 阅读(0)

扫一扫,手机访问

从官网下载CKEditor(我用的版本是3.3.1)之后,你会发现新版和原来的FCKeditor差别还挺大的。做个人博客时,贴代码是刚需,所以顺手给它写了个插入代码的插件。高亮部分用的是SyntaxHighlighter,下面直接上步骤。

1. 插件目录与核心文件

ckeditor/plugins/下新建一个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.toolbar,
            command: 'insertcode',
            icon: this.path + 'images/code.jpg'
        });
        CKEDITOR.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');
    }
});

2. 图标文件

insertcode目录下新建images文件夹,放入一个code.jpg图片(附件中已提供,可直接使用)。

3. 对话框配置

再新建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'],
                    ['Ja vaScript', 'js'], ['Ja va', 'ja va'], ['Ja vaFX', '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("
" + html + "
"); }, onLoad: function(){} }; });

这里用的是SyntaxHighlighter做高亮,如果你喜欢其他工具,替换掉相应部分即可。

4. 将插件集成到CKEditor核心

接下来的操作比较直接,因为我是把“插入代码”当作编辑器标配功能来用的,所以直接修改核心文件ckeditor.js。注意,这个文件是压缩过的,我们以原有的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.jszh.jszh-cn.js。在合适的位置添加:

  • 英文版:

    ,insertcode:'Insert Code'

  • 繁体中文:

    ,insertcode:'插入代碼'

  • 简体中文:

    ,insertcode:'插入代码'

6. 前端页面引用

最后,在需要高亮代码的页面中引入SyntaxHighlighter的样式和脚本:




这四个文件都在SyntaxHighlighter的下载包里。另外,在页面底部添加初始化代码:

至此,CKEditor的“插入代码”插件就完工了。

提示个小bug

修改之后,insertcode图标的title会显示为空。解决办法很简单:把代码中两处label: a.lang.insertcode.toolbar改为label: a.lang.insertcode即可。

本文转载于:https://www.jb51.net/article/58407.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注