Added [more] tag to news

This commit is contained in:
Igor Scheller 2021-12-10 22:24:18 +01:00 committed by msquare
parent 90ec8d8d43
commit f1531ad987
6 changed files with 57 additions and 3 deletions

View File

@ -2883,6 +2883,9 @@ msgstr "+"
msgid "news.is_meeting"
msgstr "[Treffen]"
msgid "news.read_more"
msgstr "Weiterlesen »"
msgid "news.updated"
msgstr "Aktualisiert"
@ -2913,6 +2916,9 @@ msgstr "Oben anpinnen"
msgid "news.edit.message"
msgstr "Nachricht"
msgid "news.edit.hint"
msgstr "Du kannst Markdown und den [more] Tag benutzen"
msgid "form.search"
msgstr "Suchen"

View File

@ -153,6 +153,9 @@ msgstr "+"
msgid "news.is_meeting"
msgstr "[Meeting]"
msgid "news.read_more"
msgstr "Continue reading »"
msgid "news.updated"
msgstr "Updated"
@ -183,6 +186,9 @@ msgstr "Pin to top"
msgid "news.edit.message"
msgstr "Message"
msgid "news.edit.hint"
msgstr "You can use Markdown and the [more] tag"
msgid "form.search"
msgstr "Search"

View File

@ -50,6 +50,8 @@
<div class="col-md-12">
{{ f.textarea('text', __('news.edit.message'), {'required': true, 'rows': 10, 'value': news ? news.text : ''}) }}
<p>{{ m.info(__('news.edit.hint')) }}</p>
{{ f.submit() }}
{{ f.submit(m.icon('eye'), {'name': 'preview', 'btn_type': 'info', 'title': __('form.preview')}) }}

View File

@ -60,7 +60,10 @@
{% endif %}
<div class="card-body bg-body">
{{ news.text|raw|markdown(false) }}
{{ news.text(not is_overview)|raw|markdown(false) }}
{% if is_overview and news.text != news.text(false) %}
{{ m.button(__('news.read_more'), url('news/' ~ news.id), null, 'sm') }}
{% endif %}
</div>
<div class="card-footer {% if theme.type =='light' %}bg-light{% else %}bg-dark{% endif %} text-muted">

View File

@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Str;
/**
* @property int $id
@ -62,4 +63,20 @@ class News extends BaseModel
return $this->hasMany(NewsComment::class)
->orderBy('created_at');
}
/**
* @param bool $showMore
* @return string
*/
public function text(bool $showMore = true): string
{
if ($showMore || !Str::contains($this->text, 'more')) {
// Remove more tag
return preg_replace('/(.*)\[\s*more\s*\](.*)/is', '$1$2', $this->text);
}
// Only show text before more tag
$text = preg_replace('/(.*)(\s*\[\s*more\s*\].*)/is', '$1', $this->text);
return rtrim($text);
}
}

View File

@ -64,6 +64,26 @@ class NewsTest extends ModelTest
$this->assertEquals($comment->toArray(), $news->comments->first()->toArray());
}
/**
* Tests that text more tags work
*
* @covers \Engelsystem\Models\News::text
*/
public function testTextMore(): void
{
$news = new News($this->newsData);
$news->text = "Foo\n\n\nBar";
$this->assertEquals("Foo\n\n\nBar", $news->text);
$this->assertEquals("Foo\n\n\nBar", $news->text());
$this->assertEquals("Foo\n\n\nBar", $news->text(false));
$news->text = "Foo\n[more]\nBar";
$this->assertEquals("Foo\n[more]\nBar", $news->text);
$this->assertEquals("Foo\n\nBar", $news->text());
$this->assertEquals('Foo', $news->text(false));
}
/**
* Tests that creating a News item with all fill values works.
*