From 8e5cbcd1965bd4e8131a9775134edef40a765f3f Mon Sep 17 00:00:00 2001
From: Toby Zerner
Date: Tue, 10 Feb 2015 17:52:13 +1030
Subject: [PATCH] Add unordered list formatting
---
src/Flarum/Api/Serializers/PostSerializer.php | 12 +++++++-----
src/Flarum/Core/Formatter/BasicFormatter.php | 7 +++++++
2 files changed, 14 insertions(+), 5 deletions(-)
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 '
'.preg_replace('/^ *[-*]\s*([^\n]*)(?:\n|$)/m', '- $1
', trim($matches[0])).'
';
+ }, $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;
}
}