Yii2 Syntax highlighting with support for line numbering for the Web.
176 languages and 79 styles
The plugin is based on highlight.js, added support for line numbering with the plugin highlightjs-line-numbers.js
Auto language definition
<pre><code>...</code></pre>
Explicit language specification
<pre><code class="php">...</code></pre>
<pre><code class="css">...</code></pre>
<pre><code class="json">...</code></pre>
...
Add class hljs, render full block code
<pre><code class="php hljs">...</code></pre>
<pre><code class="css hljs">...</code></pre>
<pre><code class="json hljs">...</code></pre>
...
Watch demo
The preferred way to install this extension is through composer.
Either run
php composer.phar require dominus77/yii2-highlight-plugin "*"
or add
"dominus77/yii2-highlight-plugin": "*"
to the require section of your composer.json
file.
Once the extension is installed, simply use it in your code by View:
<?php
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);
See the following link for supported topics Styles
<?php
\dominus77\highlight\Plugin::$options = [
'theme' => 'paraiso-dark',// Styles
];
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);
By default, line numbers are disabled
<?php
\dominus77\highlight\Plugin::$options = [
//...
'lineNumbers' => true, // Show line numbers
'singleLine' => true, // Show number if one line
];
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);
<?php
\dominus77\highlight\Plugin::$options = [
//...
// Custom init Highlight
'highlightInit' => new \yii\web\JsExpression("
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
"),
// Custom init Highlight Line Numbers
'lineNumbersInit' => new \yii\web\JsExpression("
$('code.hljs').each(function(i, block) {
hljs.lineNumbersBlock(block);
});
"),
];
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);
$ phpunit
Please, check the highlight.js and highlightjs-line-numbers.js
The MIT License (MIT). Please see License File for more information.