tehwave / laravel-shortcodes
使用Laravel方式实现的简单、优雅的WordPress-like Shortcodes
Requires
- php: ^7.2.5|^8.0|^8.1
- illuminate/console: ^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ~3.8.0|^4.0|^5.0|^6.3|^7.0|^8.0
- phpunit/phpunit: ^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-09-27 20:41:00 UTC
README
Laravel Shortcodes
使用Laravel方式实现简单、优雅的WordPress-like Shortcodes。
要求
该软件包已开发和测试,可与其他最新的PHP和Laravel版本以及以下最低要求一起工作
- Laravel 6
- PHP 7.2
安装
通过Composer安装此软件包。
composer require tehwave/laravel-shortcodes
用法
Laravel Shortcodes
与Wordpress的Shortcode API非常相似。
use tehwave\Shortcodes\Shortcode; $compiledContent = Shortcode::compile('[uppercase]Laravel Shortcodes[/uppercase]'); // LARAVEL SHORTCODES
创建Shortcodes
运行以下命令,将新的Shortcode
类放置到您的app/Shortcodes
目录中。
php artisan make:shortcode ItalicizeText
输出
每个Shortcode
类都包含一个handle
方法,您可以使用它将输出放入编译内容中。
在handle
方法中,您可以访问attributes
和body
属性。
注意:在解析时,
attributes
数组中的所有值都被转换为string
类型。
<?php namespace App\Shortcodes; use tehwave\Shortcodes\Shortcode; class ItalicizeText extends Shortcode { /** * The code to run when the Shortcode is being compiled. * * You may return a string from here, that will then * be inserted into the content being compiled. * * @return string|null */ public function handle(): ?string { if (isset($this->attributes['escape_html']) && $this->attributes['escape_html'] === 'true')) { return sprintf('<i>%s</i>', htmlspecialchars($this->body)); } return sprintf('<i>%s</i>', $this->body); } }
命名
短语的标签是从类名转换成snake_case得到的。
您可以使用tag
属性或通过重写getTag
方法来指定自定义标签。
短语标签必须是字母数字字符,并且可以包含下划线。
<?php namespace App\Shortcodes; use tehwave\Shortcodes\Shortcode; class ItalicizeText extends Shortcode { /** * The tag to match in content. * * @var string */ protected $tag = 'italics'; }
编译Shortcodes
运行一个字符串通过编译器以解析所有短代码。
use tehwave\Shortcodes\Shortcode; $compiledContent = Shortcode::compile('[italics escape_html="true"]<b>Hello World</b>[/italics]'); // <i><b>Hello World</b></i>
您可以指定实例化的Shortcode
类列表以限制解析的短代码。
use tehwave\Shortcodes\Shortcode; $shortcodes = collect([ new ItalicizeText, ]); $compiledContent = Shortcode::compile('[uppercase]Hello World[/uppercase]', $shortcodes); // [uppercase]Hello World[/uppercase]
示例
我为在gm48.net上使用用户提供的内客开发了Laravel Shortcodes
。
内容使用名为Parsedown的Markdown转换器进行解析,由于不能信任用户,因此内容必须被转义。
不幸的是,这会导致双引号中的属性语法被转义,但也可以使用单引号以及省略任何引号。
注意:对于包含空白的任何属性值,都需要引号。
让我们看看以下包含一些基本的Row
、Column
和Image
短语的示例内容。
# Controls:
[row]
[column]
[image align=left src=http://i.imgur.com/6CNoFYx.png alt='Move player character']
[/column]
[column]
[image align=center src=http://i.imgur.com/8nwaVo0.png alt=Jump]
[/column]
[column]
[image align=right src=http://i.imgur.com/QsbkkuZ.png alt='Go down through platforms']
[/column]
[/row]
当运行以下代码时
$parsedDescription = (new Parsedown()) ->setSafeMode(true) ->setUrlsLinked(false) ->text($this->description); $compiledDescription = Shortcode::compile($parsedDescription);
我们可以预期看到以下输出
<h1>Controls:</h1> <p></p> <div class="container-fluid"> <div class="row"> <div class="col"> <img src="http://i.imgur.com/6CNoFYx.png" class="mr-auto" alt="Move player character"> </div> <div class="col"> <img src="http://i.imgur.com/8nwaVo0.png" class="mx-auto" alt="Jump"> </div> <div class="col"> <img src="http://i.imgur.com/QsbkkuZ.png" class="ml-auto" alt="Go down through platforms"> </div> </div> </div>
您仍然应该在短语的handle
中转义任何用户输入。
测试
运行以下命令以测试软件包。
composer test
安全
有关任何安全相关的问题,请发送邮件至peterchrjoergensen+shortcodes@gmail.com,而不是使用问题跟踪器。
变更日志
有关更改的详细信息,请参阅CHANGELOG。
贡献
有关如何贡献的详细信息,请参阅CONTRIBUTING。
致谢
灵感来源于https://github.com/webwizo/laravel-shortcodes和https://github.com/spatie/laravel-blade-x
关于
我在丹麦工作,是一名Web开发者,专注于Laravel和WordPress网站。
在Twitter上关注我@tehwave!