• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >网页编辑器 > CKEditor中加入syntaxhighlighter代码高亮插件

CKEditor中加入syntaxhighlighter代码高亮插件

作者: 字体:[增加 减小] 来源:互联网 时间:2017-06-05

本文主要包含ckeditor 插件,syntaxhighlighter,代码高亮插件,od高亮插件,sublime高亮插件等相关知识,希望在学习及工作中可以帮助到您

从官网 下载ckeditor,我下载的是CKEditor 3.3.1 。CKEditor与原来的FCKeditor有太大的不同了,作为开发人员,在做自己的博客的时候总是需要贴代码的,只好给它先做一个插入代码的插件了。高亮代码用的是"SyntaxHighlighter "。

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

2、增加"images"目录,放入一个"code.jpg"的图片(附件中上传了一个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'], ['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(){ 
  } 
 }; 
}); 
</div>

 我是用"syntaxhighlighter"来做代码高亮的,如果你不喜欢它也可以换成其它的。 

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了,还有最后一步就是在你需要高亮代码的页面引用:

<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/shBrushes.js" ></script> 
</div>

这四个文件在syntaxhighlighter的下载包里都有,最后,还在页面增加这段JS:

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

CKEditor的"插入代码"插件就OK了。

提示个小bug

修改之后insertcode图标的title为空。我把你的代码中的"label: a.lang.insertcode.toolbar"(共2处)改为"label: a.lang.insertcode"。解决!

</div>

您可能想查找下面的文章:

  • CKEDITOR二次开发之插件开发方法
  • ckeditor syntaxhighlighter代码高亮插件配置分享
  • CKEditor中加入syntaxhighlighter代码高亮插件
  • ckeditor插件开发简单实例
  • SyntaxHighlighter配合CKEditor插件轻松打造代码语法着色
  • ckeditor syntaxhighlighter代码高亮插件,完美修复
  • CKEditor 附插入代码的插件

相关文章

  • 2017-06-05CKEditor 取消转义的两种方法
  • 2017-06-05百度编辑器 ueditor 内容编辑自动套P标签,及p标签 替换
  • 2017-06-05UEditor 编辑器跨域上传解决方法
  • 2017-06-0522个国外的Web在线编辑器收集
  • 2017-06-05伪静态下不能使用FCKeditor的解决方法
  • 2017-06-05当使用ckeditor控件时,需要校验输入内容是否为空的一种解决办法(转帖)
  • 2017-06-05页面上存在多个FckEditor编辑器的验证方法
  • 2017-06-05FCKeditor 图片上传进度条不动的解决方法
  • 2017-06-05FCKEditor超级链接默认新窗口打开的修改方法
  • 2017-06-05浏览器执行history.go(-1) FCKeditor编辑框内显示html源代码的解决方法

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • ie9后浏览器fckeditor无法上传图片、弹出浮层内容不显示的解决方法
    • javascript fckeditor编辑器取值与赋值实现代码
    • CKeditor与syntaxhighlight打造joomla代码高亮
    • 关于jsp版ueditor1.2.5的部分问题解决(上传图片失败)
    • FCKeidtor 清除编辑器内容的代码
    • fckeditor常用Js,获取fckeditor内容,统计fckeditor字数,向fckeditor写入指定代码
    • ThinkPHP中FCKeditor编辑器的使用方法
    • 免费开源百度编辑器(UEditor)使用方法
    • FCKeditorAPI 手册 js操作获取等
    • UEditor编辑器自定义上传图片或文件路径的修改方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有