CKEditor Code Snippet plugin does not do syntax hi

2019-06-24 01:27发布

问题:

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'
];

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 ?

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

回答1:

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:

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...