# Pimcore CKEditor Syntax Highlight; Code Snippet

**URL:** https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65
**Category:** WEB Design
**Created:** [2017 年 1 月 6 日午後 1:06 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65 "2017-01-06T13:06:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2017 年 1 月 6 日午後 1:06 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/1 "2017-01-06T13:06:32Z")

</div>

PimcoreのデフォルトエディターであるCKEeditorのプラグイン、Code Snippetにより各種プログラムコードを強調表示させる方法について。  
Pimcoreに限らずブログ用CMSでCKEditorが標準装備されている場合の参考にして下さい。  
[Syantaxhighlighter](http://alexgorbatchev.com/SyntaxHighlighter/)などで強調表示させる方法もあるようですが、標準実装されているCKEditorのプラグインに同等の機能を有するものが幾つかあるのでこれを導入します。

今回導入するプラグインは、

**Code Snippet**

> **[Code Snippet](https://ckeditor.com/cke4/addon/codesnippet)**
>
> This plugin lets you insert rich code snippets with syntax highlighting into the editor.

とそれに関連した次のプラグインです。

**Widget**

> **[Widget](https://ckeditor.com/cke4/addon/widget)**
>
> This plugin introduces the Widget API. It is required by all plugins that create widgets. Widgets let you standardize, manage and control the layout and style of specific sections of your WYSIWYG area, all this at the press of a button.

**Line Utilities**

> **[Line Utilities](https://ckeditor.com/cke4/addon/lineutils)**
>
> A set of utilities to find and create horizontal spaces in edited contents.

CKEditorのバージョンに合致するものをダウンロードして解凍します。

CKEditorのpluginsフォルダに解凍したフォルダをコピーします。

CKEditorの設定ファイルである **config.js** にプラグインの設定を記述します。

`config.extraPlugins = 'codesnippet,widget,lineutils';`

を追加します。

```
CKEDITOR.editorConfig = function( config ) {
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	// config.uiColor = '#AADC6E';
	config.extraPlugins = 'codesnippet,widget,lineutils';
};

```

CKEditorを読み込むページの`<head>...</head>`の部分に以下を追加します。

```
<head>
    ...
    <link href="ckeditor/plugins/codesnippet/lib/highlight/styles/default.css" rel="stylesheet">
    <script src="ckeditor/plugins/codesnippet/lib/highlight/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
</head>

```

htmlホームディレクトリからのパスを指定して下さい。  
ブラウザの検証機能でパスの設定などのエラーが検出されないか確認して下さい。  
強調表示の色配分などは指定するcssを変更することで可能です。詳細はプラグインのページで確認して下さい。

 ![](https://forum.ficusonline.com/uploads/default/original/1X/f77a1189f1fab8cfc97e4a9744b3859c4afb8f80.png)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2017 年 1 月 10 日午後 1:29 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/2 "2017-01-10T13:29:21Z")

</div>

デフォルトのCSSでは半角スペース、タブ、改行をそのまま表示しないため **white-space: pre; overflow-x: auto;** を **/web/bundles/pimcoreadmin/js/lib/ckeditor/plugins/codesnippet/lib/highlight/styles** 内で使用するcssファイルに追加します( **rainbow.css** )。

```
.hljs {
  display: block;
  padding: 0.5em;
  background: #002b36;
  color: #839496;
  white-space: pre !important; --->スライダー表示
  overflow-x: auto; --->これは任意（領域内にスライダー表示）
}

```

各cssの配色は下記リンク参照。

[https://highlightjs.org/static/demo/](https://highlightjs.org/static/demo/)

以下ファイル内で **CKEditor** のプラグイン **codesnippet** を指定。  
`/app/Resources/views/layout.html.php`

```auto
.....
.....
<head>
.....
.....
<link href="/bundles/pimcoreadmin/js/lib/ckeditor/plugins/codesnippet/lib/highlight/styles/rainbow.css" rel="stylesheet">
    <script src="/bundles/pimcoreadmin/js/lib/ckeditor/plugins/codesnippet/lib/highlight/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
.....
.....
</head>
.....
.....

```

**スライダー右移動時のコンテンツ右側余白0**

**`display: block ---> display: inline-block`** へ変更

`bundles/pimcoreadmin/js/lib/ckeditor/plugins/codesnippet/lib/highlight/styles/rainbow.css`

```auto
.hljs {
  display: inline-block; padding: 0.5rem;
  background: #474949; color: #D1D9E1;
  white-space: pre !important;
}

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2018 年 7 月 22 日午後 9:34 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/3 "2018-07-22T21:34:59Z")

</div>

**CKEditor4.9.2 full にバージョンアップ**

**ダウンロード**  
[https://download.cksource.com/CKEditor/CKEditor/](https://download.cksource.com/CKEditor/CKEditor/)

Code Snippetの依存プラグインは、

**Widget**

> **[Widget](https://ckeditor.com/cke4/addon/widget)**
>
> This plugin introduces the Widget API. It is required by all plugins that create widgets. Widgets let you standardize, manage and control the layout and style of specific sections of your WYSIWYG area, all this at the press of a button.

**Dialog**

> **[Dialog](https://ckeditor.com/cke4/addon/dialog)**
>
> This plugin provides the dialog API for other plugins to build an editor dialog from a definition object.

**Lineutils**

> **[Line Utilities](https://ckeditor.com/cke4/addon/lineutils)**
>
> A set of utilities to find and create horizontal spaces in edited contents.

**Widgedselection**

> **[Widget Selection](https://ckeditor.com/cke4/addon/widgetselection)**
>
> This plugin improves selection handling for Widget elements. Due to some browser bugs additional handling is required to allow the widget selection.

上記プラグインは、一部中身のないホルダーのみ格納されているため、ダウンロード、インストールが必要です。

CKEditorの設定ファイルである **config.js** には **Code Snippet** のプラグインのみを記述します。

```
CKEDITOR.editorConfig = function( config ) {
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	// config.uiColor = '#AADC6E';
	config.extraPlugins = 'codesnippet';
};

```

 ![Screenshot%20from%202018-10-03%2022-34-04](https://forum.ficusonline.com/uploads/default/original/1X/a7f37c9a658551642b9a045ca918f038600f891c.png)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 2 月 18 日午後 12:13 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/4 "2019-02-18T12:13:39Z")

</div>

CKEditorビルダーページによる各プラグインオプションの追加とスキンを指定してダウンロード

> **[CKEditor 4 online builder](https://ckeditor.com/cke4/builder)**

 ![ckeditor_builder](https://forum.ficusonline.com/uploads/default/original/1X/2fe71f1c29d22efabcfdc35e6b6f6b12f5c771d2.png)

1. Fullを選択
2. Reset presetを押してcode snippet, source dialog を選択
3. Moonoスキンを選択
4. Optimizedをチェックしてダウンロード

**config.js** を編集します。

```auto
CKEDITOR.editorConfig = function( config ) {
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	// config.uiColor = '#AADC6E';
	config.extraPlugins = 'codesnippet,widget,lineutils';
};

```

**/var/www/test-site/web/bundles/pimcoreadmin/js/lib** 内の **ckeditor** を削除後コピーします。

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 10 月 2 日午前 7:59 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/5 "2019-10-02T07:59:22Z")

</div>

## CKEditor4.12.1へアップグレード

Code Snippetプラグイン追加のため関連プラグインもインストール。

以下Code Snippetの該当バージョンをダンロードする際に表示される依存プラグインを全てインストール

### 新たにインストールしたプラグイン

 ![1_plugins](https://forum.ficusonline.com/uploads/default/original/1X/2074ccda535dfefe89c3fa737943213467c5906b.png)

### CKEdior4.12.1プラグイン一覧

 ![ckeditor_plugins](https://forum.ficusonline.com/uploads/default/original/1X/975149713a7f3c209f2f9bc0767c94097a928ed7.png)

**config.js** を編集します。（編集する必要はありません）

```auto
CKEDITOR.editorConfig = function( config ) {
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	// config.uiColor = '#AADC6E';
	config.extraPlugins = 'codesnippet,widget,lineutils';
};

```

**ckeditorフォルダ** をサーバの該当箇所

```auto
vendor/pimcore/pimcore/bundles/AdminBundle/Resources/public/js/lib

```

にコピーしてアクセス権を変更します。ブラウザのキャッシュを削除してから読み込みます。

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 7 月 5 日午前 8:05 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/6 "2020-07-05T08:05:03Z")

</div>

## CKEditor4.16.1へアップグレード

> **[CKEditor 4 online builder](https://ckeditor.com/cke4/builder)**

**1. Fullを選択**

**2. Reset presetを押してcode snippet, source dialog を選択**

**Available plugins** から **code snippet, source dialog** を選択し、左側矢印ボタンをクリックして **Selected plugins** にリストアップします。これら依存プラグイン確認のため **Website** をクリック、各プラグインの **Website** から直接ダウンロードすると依存プラグインが表示されますが、依存プラグインはセットに含まれています。

 ![ckeditor4-14-1](https://forum.ficusonline.com/uploads/default/original/1X/4b63d2fd28ca4120d67cdcda5e4968f1d98044e9.png)

**code snippet, source dialogの依存プラグイン**

 ![ckeditor4-14-1plugins](https://forum.ficusonline.com/uploads/default/original/1X/61ed57dabc6ac733d8d08e3c0fa3751cf3cd5ce8.png)

**3. Moono-Lisaスキン、LanguageにEnglishを選択**

**4. Optimizedをチェックしてダウンロード**

**5. CKEditorのアップロード**  
ダウンロードした **ckeditor4.16.1** のzipファイルを展開後、展開したフォルダを  
`/var/www/test-site/web/bundles/pimcoreadmin/js/lib`にコピーします。  
（ **cofig.js** は編集する必要はありません）。

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2021 年 11 月 26 日午前 5:19 UTC](https://forum.ficusonline.com/t/pimcore-ckeditor-syntax-highlight-code-snippet/65/7 "2021-11-26T05:19:09Z")

</div>

**[CKEDITOR] Error code: exportpdf-no-token-url.**

メモリ負荷が大きいためとフリーではないため、以下のプラグイン **exportpdf** を無効化。

[https://ckeditor.com/cke4/addon/exportpdf](https://ckeditor.com/cke4/addon/exportpdf)

`/public/bundles/pimcoreadmin/js/lib/ckeditor/config.js`

```auto
/**
 * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see https://ckeditor.com/legal/ckeditor-oss-license
 */

CKEDITOR.editorConfig = function( config ) {
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	// config.uiColor = '#AADC6E';
   config.removePlugins = 'exportpdf';
};

```
