dominus77/yii2-highlight-plugin

Yii2 Syntax highlighting with support for line numbering for the Web.

v1.1.2 2018-01-16 16:18 UTC

This package is auto-updated.

Last update: 2024-04-16 06:17:58 UTC


README

Latest Stable Version License Build Status codecov Scrutinizer Code Quality Total Downloads PayPal donate button SensioLabsInsight

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.

Sensio Labs

SensioLabsInsight