taylornetwork / linkify
一个将文本链接化的包
Requires
- php: >=7.0
- illuminate/support: >=5.5
Requires (Dev)
- mockery/mockery: dev-master
- orchestra/testbench: ^3.7
This package is auto-updated.
Last update: 2024-09-04 11:50:18 UTC
README
一个将字符串中的所有链接转换为 Markdown、HTML 或自定义格式的包。
轻松转换
'This text has a link https://github.com/taylornetwork/linkify and also another one https://google.com'
为
'This text has a link [github.com](https://github.com/taylornetwork/linkify) and also another one [google.com](https://google.com)' // OR 'This text has a link <a href="https://github.com/taylornetwork/linkify">github.com</a> and also another one <a href="https://google.com">google.com</a>'
安装
通过 Composer
$ composer require taylornetwork/linkify
使用方法
查看 配置选项
Linkify 类
基本用法
use TaylorNetwork\Linkify\Linkify; $text = 'This has a link. https://google.com'; $linkify = new Linkify; $linkify->parse($text);
返回
'This has a link. [google.com](https://google.com)'
基本用法 - 静态调用
use TaylorNetwork\Linkify\Linkify; $text = 'This has a link. https://google.com'; Linkify::instance()->parse($text);
返回
'This has a link. [google.com](https://google.com)'
覆盖配置
use TaylorNetwork\Linkify\Linkify; $text = 'This has a link. https://google.com'; $linkify = new Linkify; $linkify->setConfig('convertTo', Linkify::ConvertHTML); $linkify->parse($text);
返回
'This has a link. <a href="https://google.com">google.com</a>'
覆盖配置 - 静态,一行代码
use TaylorNetwork\Linkify\Linkify; $text = 'This has a link. https://google.com'; Linkify::instance()->setConfig('convertTo', Linkify::ConvertHTML)->parse($text);
返回
'This has a link. <a href="https://google.com">google.com</a>'
创建链接特性
MakesLinks
特性将允许您通过类上的 linkify($text)
方法访问解析器。
基本用法
use TaylorNetwork\Linkify\MakesLinks; class DummyClass { use MakesLinks; protected $text = 'This has a link. https://google.com'; public function getParsedText() { return $this->linkify($this->text); } }
getParsedText()
返回
'This has a link. [google.com](https://google.com)'
覆盖配置
use TaylorNetwork\Linkify\MakesLinks; use TaylorNetwork\Linkify\Linkify; class DummyClass { use MakesLinks; protected $text = 'This has a link. https://google.com'; public function getParsedText() { return $this->linkify($this->text); } public function linkifyConfig(&$linkify) { $linkify->setConfig('convertTo', Linkify::ConvertHTML); $linkify->setConfig('linkAttributes', [ 'class' => 'btn-link' ]); } }
getParsedText()
返回
'This has a link. <a class="btn-link" href="https://google.com">google.com</a>'
自定义格式
use TaylorNetwork\Linkify\MakesLinks; use TaylorNetwork\Linkify\Linkify; class DummyClass { use MakesLinks; protected $text = 'This has a link. https://google.com'; public function getParsedText() { return $this->linkify($this->text); } public function linkifyConfig(&$linkify) { $linkify->setConfig('convertTo', Linkify::ConvertCustom); } public function linkifyCustomParse(string $caption, string $url) { return '=>' . $caption . '<=#' . $url . '#'; } }
getParsedText()
返回
'This has a link. =>google.com<=#https://google.com#'
配置
使用 artisan 命令发布配置。
$ php artisan vendor:publish --provider="TaylorNetwork\Linkify\LinkifyServiceProvider"
将配置文件发布到 config/linkify.php
链接格式
您可以通过更改 config/linkify.php
中的 convertTo
设置来更改默认链接格式。
'convertTo' => Linkify::ConvertMarkdown, // Converts to markdown links // OR 'convertTo' => Linkify::ConvertHTML, // Converts to <a> links // OR 'convertTo' => Linkify::ConvertCustom, // Your own custom callback
链接属性(仅限 HTML)
您可以在生成链接时将任何您想要包含在 <a>
标签中的 HTML 链接属性添加到 linkAttributes
数组中(不要使用 href
)。
将键和值添加到 linkAttributes
数组中。
'linkAttributes' => [ 'target' => '_blank', 'class' => 'btn-link', ],
这将生成链接
<a target="_blank" class="btn-link" href="$url">$caption</a>
其中 $caption
和 $url
将自动替换。
注意:属性按数组顺序添加,并且仅在 convertTo
中使用 Linkify::ConvertHTML
设置时运行。
仅格式化未格式化的链接
如果只想转换没有现有格式的链接(默认),则应将 checkForExistingFormatting
设置为 true
。
例如
// Link is already formatted, by user, or another package, etc. $text = 'Link: [link](https://google.com)'; Linkify::instance()->parse($text); // Returns // If 'checkForExistingFormatting' is true 'Link: [link](https://google.com)' // If 'checkForExistingFormatting' is false 'Link: [link]([google.com](https://google.com))'
自定义格式化
您可以在配置文件中定义自定义格式化程序,但更喜欢您使用 MakeLinks
特性。
'customCallback' => function ($caption, $url) { return '{' . $caption . '}#' . $url . '#'; },
如果 'convertTo' => Linkify::ConvertCustom
,则 $caption
和 $url
将传递给回调。
这将返回: '{google.com}#https://google.com#'
许可
MIT