diff --git a/src/Flarum/Api/Serializers/PostSerializer.php b/src/Flarum/Api/Serializers/PostSerializer.php index 6a297d23f..16cadfe8c 100644 --- a/src/Flarum/Api/Serializers/PostSerializer.php +++ b/src/Flarum/Api/Serializers/PostSerializer.php @@ -35,12 +35,14 @@ class PostSerializer extends PostBasicSerializer $user = User::current(); unset($attributes['content']); + + $canEdit = $post->can($user, 'edit'); + if ($post->type != 'comment') { $attributes['content'] = $post->content; } else { - // @todo move to a formatter class - $attributes['contentHtml'] = $post->content_html ?: '

'.nl2br(htmlspecialchars(trim($post->content))).'

'; - if ($post->can($user, 'edit')) { + $attributes['contentHtml'] = $post->content_html; + if ($canEdit) { $attributes['content'] = $post->content; } } @@ -50,12 +52,12 @@ class PostSerializer extends PostBasicSerializer } if ($post->delete_time) { + $attributes['isHidden'] = true; $attributes['deleteTime'] = $post->delete_time->toRFC3339String(); } - $attributes += [ - 'canEdit' => $post->can($user, 'edit'), + 'canEdit' => $canEdit, 'canDelete' => $post->can($user, 'delete') ]; diff --git a/src/Flarum/Core/Formatter/BasicFormatter.php b/src/Flarum/Core/Formatter/BasicFormatter.php index abdf20f0b..41316704f 100644 --- a/src/Flarum/Core/Formatter/BasicFormatter.php +++ b/src/Flarum/Core/Formatter/BasicFormatter.php @@ -11,8 +11,15 @@ class BasicFormatter $linkify = new Linkify; $text = $linkify->process($text, ['attr' => ['target' => '_blank']]); + $text = preg_replace_callback('/(?:^ *[-*]\s*([^\n]*)(?:\n|$)){2,}/m', function ($matches) { + return '

'; + }, $text); + $text = '

'.preg_replace(['/[\n]{2,}/', '/\n/'], ['

', '
'], trim($text)).'

'; + $text = preg_replace(array("/

\s*<\/p>/i", "/(?<=

)\s*(?:
)*/i", "/\s*(?:
)*\s*(?=<\/p>)/i"), "", $text); + $text = str_replace("

", "", $text); + return $text; } }