dominus77 / yii2-highlight-plugin
Yii2 Syntax highlighting with support for line numbering for the Web.
Installs: 456
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 1
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0
- yiisoft/yii2-bootstrap: ~2.0.0
Requires (Dev)
- phpunit/phpunit: 6.*
README
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
Installation
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.
Usage
Once the extension is installed, simply use it in your code by View:
<?php
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);
Configure plugin
Theme
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);
Line Numbers
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);
Custom init
<?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);
Testing
$ phpunit
More Information
Please, check the highlight.js and highlightjs-line-numbers.js
License
The MIT License (MIT). Please see License File for more information.