dominus77/yii2-highlight-plugin

支持行号显示的Web用Yii2语法高亮插件。

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

This package is auto-updated.

Last update: 2024-09-16 07:08:18 UTC


README

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

支持行号显示的Web用Yii2语法高亮插件。

176种语言和79种风格

此插件基于 highlight.js,通过插件 highlightjs-line-numbers.js 添加了行号支持

自动语言定义

<pre><code>...</code></pre>

显式语言指定

<pre><code class="php">...</code></pre>
<pre><code class="css">...</code></pre>
<pre><code class="json">...</code></pre>
...

添加类 hljs,渲染完整的代码块

<pre><code class="php hljs">...</code></pre>
<pre><code class="css hljs">...</code></pre>
<pre><code class="json hljs">...</code></pre>
...

观看 demo

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

php composer.phar require dominus77/yii2-highlight-plugin "*"

或添加以下内容到您的 composer.json 文件的 require 部分。

"dominus77/yii2-highlight-plugin": "*"

使用方法

扩展安装后,只需通过 View 在代码中使用即可

<?php
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);

配置插件

主题

有关支持主题的链接,请参阅以下链接 Styles

<?php
\dominus77\highlight\Plugin::$options = [
    'theme' => 'paraiso-dark',// Styles       
];
/** $this \yii\web\View */
\dominus77\highlight\Plugin::register($this);

行号

默认情况下,行号是禁用的

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

更多信息

请查看 highlight.jshighlightjs-line-numbers.js

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件

Sensio Labs

SensioLabsInsight