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

您的位置: 首页 > 文章列表 > 编程开发 > SyntaxHighlighter配合CKEditor插件轻松打造代码语法着色

SyntaxHighlighter配合CKEditor插件轻松打造代码语法着色

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

扫一扫,手机访问

在网上搜索相关教程,发现不少文章大同小异。很多开发者选择使用SyntaxHighlighter配合CKEditor插件来实现代码语法着色。不过由于版本差异,实际操作中可能会遇到一些问题。本文基于3.0.83版SyntaxHighlighter和3.5.3版CKEditor,整理了一套可行的集成方案,供参考。

SyntaxHighlighter配合CKEditor插件轻松打造代码语法着色

一、SyntaxHighlighter简介

SyntaxHighlighter(原名:dp.SyntaxHighlighter)是一套在浏览器上对各种代码进行语法着色的Ja vaScript库。官方下载地址:http://alexgorbatchev.com/SyntaxHighlighter/。本文使用的版本是3.0.83版,下载后只需要syntaxhighlighter文件夹下的“scripts”和“styles”文件夹内的文件即可。

在代码语法着色高亮显示的页面中,需要引用“styles/shCore.css”样式文件和“scripts/shCore.js”JS文件。由于每一种代码语言都要引用各自的JS文件,为了降低HTTP请求,可以把所有代码语言的JS文件内容合并到一个“scripts/shBrushSeaYee.js”文件里,并优化成一行。例如:

复制代码

代码如下:

二、CKEditor简介

CKEditor是一个专门使用在网页上的开源所见即所得文字编辑器。它追求轻量化,安装步骤简单,可和PHP、Ja vaScript、ASP、ASP.NET、ColdFusion、Ja va、ABAP等不同编程语言结合。它原名FCKEditor,2009年发布3.0版本后改名为CKEditor。原来叫FCK是因为最初的开发者叫Frederico Calderia Knabben;现在叫CK,意指“Content and Knowledge”。官方下载地址:http://ckeditor.com/。本文使用的版本是3.5.3版,安装配置都比较简单,不再赘述。

三、CKEditor代码语法着色高亮显示的插件开发

1、在“ckeditor\plugins\”目录下新建“insertcode”目录

在“insertcode”目录下新建一个“plugin.js”,输入以下代码:

复制代码

代码如下:

CKEDITOR.plugins.add('insertcode',

{

init: function(editor)

{

//plugin code goes here

var pluginName = 'Insertcode';

CKEDITOR.dialog.add(pluginName, this.path + 'insertcode.js');

editor.config.flv_path = editor.config.flv_path || (this.path);

editor.addCommand(pluginName, new CKEDITOR.dialogCommand(pluginName));

editor.ui.addButton('Insertcode',

{

label: '代码',

command: pluginName,

icon: this.path + 'insertcode.gif'

});

}

});

注意:第1行代码中的“insertcode”必须与文件夹名称相同且区分大小写字母,因为在Linux系统的Web服务器中,路径区分大小写。

2、放入图标文件

在“insertcode”目录下放入一个“insertcode.gif”16×16的图片,可以自己制作或从网上查找。

3、新建“insertcode.js”文件

在“insertcode”目录下新建一个“insertcode.js”,输入如下代码:

复制代码

代码如下:

CKEDITOR.dialog.add('Insertcode', function(editor){

var escape = function(value){return value;};

return{

title: '代码',

resizable: CKEDITOR.DIALOG_RESIZE_BOTH,

minWidth: 720,

minHeight: 520,

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'], ['ColdFusion', 'cf'], ['C#', 'csharp'], ['C++', 'cpp'], ['CSS', 'css'], ['Delphi', 'delphi'], ['Diff', 'diff'], ['Groovy', 'groovy'], ['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:718px;height:450px',

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(){}

};

});

4、配置CKEditor的config.js

在“ckeditor\”目录下找到“config.js”文件,添加如下代码:

复制代码

代码如下:

config.extraPlugins = 'insertcode';

注意:代码中的“insertcode”也必须与文件夹名称相同,区分大小写字母。要在CKEditor工具栏添加按钮,就在此配置文件中添加上“,Insertcode”即可,同样注意大小写。至此,插件配置完成。

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

热门关注