mirror of
https://github.com/flarum/framework.git
synced 2025-04-28 07:34:03 +08:00
Remove BasicFormatter; add LinkifyFormatter
This commit is contained in:
parent
4fd61e2466
commit
d3845ed1c0
@ -33,6 +33,8 @@ class CoreServiceProvider extends ServiceProvider
|
|||||||
$this->registerGambits();
|
$this->registerGambits();
|
||||||
$this->setupModels();
|
$this->setupModels();
|
||||||
|
|
||||||
|
$this->app['flarum.formatter']->add('linkify', 'Flarum\Core\Formatter\LinkifyFormatter');
|
||||||
|
|
||||||
$bus->mapUsing(function ($command) {
|
$bus->mapUsing(function ($command) {
|
||||||
return Bus::simpleMapping(
|
return Bus::simpleMapping(
|
||||||
$command, 'Flarum\Core\Commands', 'Flarum\Core\Handlers\Commands'
|
$command, 'Flarum\Core\Commands', 'Flarum\Core\Handlers\Commands'
|
||||||
@ -54,11 +56,7 @@ class CoreServiceProvider extends ServiceProvider
|
|||||||
// forum, registering, and starting discussions.)
|
// forum, registering, and starting discussions.)
|
||||||
$this->app->singleton('flarum.forum', 'Flarum\Core\Models\Forum');
|
$this->app->singleton('flarum.forum', 'Flarum\Core\Models\Forum');
|
||||||
|
|
||||||
$this->app->singleton('flarum.formatter', function () {
|
$this->app->singleton('flarum.formatter', 'Flarum\Core\Formatter\FormatterManager');
|
||||||
$formatter = new FormatterManager($this->app);
|
|
||||||
$formatter->add('basic', 'Flarum\Core\Formatter\BasicFormatter');
|
|
||||||
return $formatter;
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->app->bind(
|
$this->app->bind(
|
||||||
'Flarum\Core\Repositories\DiscussionRepositoryInterface',
|
'Flarum\Core\Repositories\DiscussionRepositoryInterface',
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
<?php namespace Flarum\Core\Formatter;
|
|
||||||
|
|
||||||
use Misd\Linkify\Linkify;
|
|
||||||
|
|
||||||
class BasicFormatter
|
|
||||||
{
|
|
||||||
public function format($text)
|
|
||||||
{
|
|
||||||
$text = htmlspecialchars($text);
|
|
||||||
|
|
||||||
$linkify = new Linkify;
|
|
||||||
$text = $linkify->process($text, ['attr' => ['target' => '_blank']]);
|
|
||||||
|
|
||||||
$text = preg_replace_callback('/(?:^ *[-*]\s*([^\n]*)(?:\n|$)){2,}/m', function ($matches) {
|
|
||||||
return '</p><ul>'.preg_replace('/^ *[-*]\s*([^\n]*)(?:\n|$)/m', '<li>$1</li>', trim($matches[0])).'</ul><p>';
|
|
||||||
}, $text);
|
|
||||||
|
|
||||||
$text = '<p>'.preg_replace(['/[\n]{2,}/', '/\n/'], ['</p><p>', '<br>'], trim($text)).'</p>';
|
|
||||||
|
|
||||||
$text = preg_replace(array("/<p>\s*<\/p>/i", "/(?<=<p>)\s*(?:<br>)*/i", "/\s*(?:<br>)*\s*(?=<\/p>)/i"), "", $text);
|
|
||||||
$text = str_replace("<p></p>", "", $text);
|
|
||||||
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
}
|
|
@ -29,9 +29,8 @@ class FormatterManager
|
|||||||
$this->remove($name);
|
$this->remove($name);
|
||||||
|
|
||||||
if (is_string($formatter)) {
|
if (is_string($formatter)) {
|
||||||
$container = $this->container;
|
$formatter = function () use ($formatter) {
|
||||||
$formatter = function () use ($container, $formatter) {
|
$callable = array($this->container->make($formatter), 'format');
|
||||||
$callable = array($container->make($formatter), 'format');
|
|
||||||
$data = func_get_args();
|
$data = func_get_args();
|
||||||
return call_user_func_array($callable, $data);
|
return call_user_func_array($callable, $data);
|
||||||
};
|
};
|
||||||
|
18
framework/core/src/Core/Formatter/LinkifyFormatter.php
Normal file
18
framework/core/src/Core/Formatter/LinkifyFormatter.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php namespace Flarum\Core\Formatter;
|
||||||
|
|
||||||
|
use Misd\Linkify\Linkify;
|
||||||
|
|
||||||
|
class LinkifyFormatter
|
||||||
|
{
|
||||||
|
protected $linkify;
|
||||||
|
|
||||||
|
public function __construct(Linkify $linkify)
|
||||||
|
{
|
||||||
|
$this->linkify = $linkify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function format($text)
|
||||||
|
{
|
||||||
|
return $this->linkify->process($text, ['attr' => ['target' => '_blank']]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user