本文介绍了CKEditor Code Snippet插件不在yii2框架中进行语法高亮显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在yii2 php框架应用程序中使用CKEditor及其。当我使用CKEditor编写一些代码时,我想获得语法高亮,但无论我做什么,我都无法使其工作。我已经尝试了从指南中的所有内容,包括头部中的css和highlight.js但我什么都没得到...声明资产包也没有帮助,yii2将加载文件,但突出显示仍然无效。

I am using CKEditor and its Code Snippet plugin in yii2 php framework application. I would like to get syntax highlighting when I write some code using CKEditor, but whatever I do I can not make it work. I have tried everything from the guide like including css and highlight.js in the head but I get nothing... Declaring asset bundle also do not help, yii2 will load files, but highlighting still do not work.

public $css = [
    'css/site.css',
    'highlight/styles/default.css'
];

public $js = [
    'js/site.js',
    'highlight/highlight.pack.js'
];

我使用这个小部件在我的yii2网站上安装CKEditor:

I am using this widget to install CKEditor in my yii2 site: https://github.com/2amigos/yii2-ckeditor-widget

有人可以帮我解决这个问题吗?我需要做些什么才能使语法高亮显示工作?

Can someone help me out with this ? What do I have to do to make syntax highlighting work ?

编辑:突出显示在编辑器内部工作,但我需要在我的页面上发布代码。

highlighting is working inside the editor, but I need it for my page where code is posted.

推荐答案

我已经解决了我的问题,我必须包含 hljs.initHighlightingOnLoad(); 在我的页面上加载highlight.pack.js之后。有几种方法可以这样做,我这样做:

I have solved my problem, I had to include hljs.initHighlightingOnLoad(); after highlight.pack.js is loaded on my page. There are few ways to do so, I did it like this:

我已经包含 hljs.initHighlightingOnLoad(); 作为我的site.js脚本的一部分,我已经 highlight.pack.js 之后加载了site.js文件。所以我不得不像这样更新我的资产包:

I have included hljs.initHighlightingOnLoad(); as a part of my site.js script, and I have loaded site.js file after the highlight.pack.js. So I had to update my Asset bundle like this:

public $js = [
    'highlight/highlight.pack.js',
    'js/site.js'
];

我有几个小时的麻烦,并没有意识到解决方案就这么简单。必须是大脑冻结......

I had troubles for few hours and didn't realized that solution is that simple. Must be a brain freeze...

这篇关于CKEditor Code Snippet插件不在yii2框架中进行语法高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:20