No comments on pages

This commit is contained in:
Alexander Yakovlev 2022-09-05 13:50:42 +07:00
parent 742f3d39c8
commit c4741f88c1
Signed by: oreolek
GPG key ID: 8D24103F5EE2A6C0
6 changed files with 63 additions and 16 deletions

View file

@ -1,20 +1,49 @@
<?php
namespace App\Helpers;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\MarkdownConverter;
use Illuminate\Support\Str;
class Markdown {
protected $converter;
public function convert(string $text, bool $trim = false): string {
public function convert(string $text, bool $trim = false, bool $escape_html = true): string {
// TODO thumbnails
$text = str_replace('!thumb', '!', $text);
$retval = Str::markdown($text);
$options = [
'smartpunct' => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '',
'single_quote_closer' => '',
],
];
if (!$escape_html) {
$options = [
'html_input' => 'allow',
'allow_unsafe_links' => true,
];
}
$environment = new Environment($options);
$environment->addExtension(new GithubFlavoredMarkdownExtension());
$environment->addExtension(new InlinesOnlyExtension());
$environment->addExtension(new SmartPunctExtension());
$converter = new MarkdownConverter($environment);
$retval = (string) $converter->convert($text);
if ($trim) {
$retval = Str::words($retval, config('app.brief_limit'), '…');
}
if (class_exists('\tidy')) {
$tidy = new \tidy();
$retval = $tidy->repairString($retval);
$retval = $tidy->repairString($retval, [
'show-body-only' => true
]);
}
return $retval;
}

View file

@ -21,7 +21,8 @@ class PageController extends Controller
public function view($id) {
$page = Page::findOrFail($id);
return view('single', [
'item' => $page
'item' => $page,
'comments' => false
]);
}
}

View file

@ -39,7 +39,7 @@ class CommentIndexComposer extends ReadComposer
$output['edit_link'] = route('comments.edit', $item->id);
}
$output['heading'] = $item->name;
$output['content'] = $this->markdown->convert(strip_tags($item->content));
$output['content'] = $this->markdown->convert($item->content);
return $output;
}
}

View file

@ -24,6 +24,10 @@ class ViewComposer extends GenericView
* @var boolean
*/
protected $show_create = FALSE;
/**
* @var boolean
**/
protected $show_comments = TRUE;
protected function current_controller() {
$c = get_class(Route::current()->getController());
@ -53,7 +57,6 @@ class ViewComposer extends GenericView
'content' => '',
'edit_link' => '',
'view_link' => '',
'comments_link' => '',
'speed' => '',
);
if ($this->show_date)
@ -73,19 +76,24 @@ class ViewComposer extends GenericView
'id' => $item->id
));
$output['heading'] = $item->name;
$output['comments_link'] = $this->route(
'view',
$item->id
).'#comments';
if ($this->show_comments) {
$output['comments_link'] = $this->route(
'view',
$item->id
).'#comments';
}
if (isset($item->password) && $item->password !== '') {
$output['content'] = _('Введите пароль для просмотра.');
} else {
if (isset($item->content_html) && !empty($item->content_html)) {
$output['content'] = $item->content_html;
} else {
$output['content'] = $this->markdown->convert($item->content);
$output['content'] = $this->markdown->convert($item->content, true, false);
//$item->content_html = $output['content'];
//$item->save();
}
}
dump($output);
return $output;
}
@ -103,19 +111,25 @@ class ViewComposer extends GenericView
}
$this->show_create = $data['show_create'] ?? false;
$this->show_date = $data['show_date'] ?? true;
if (!empty($this->content)) {
$this->content = $this->markdown->convert($this->content);
$content = '';
if (!empty($data['content'])) {
$content = $this->markdown->convert($data['content'], true, false);
}
$view->with('item', $this->show_item($this->item));
$view->with('content', $data['content'] ?? '');
$view->with('content', $content);
$view->with('show_date', $this->show_date);
$view->with('show_date', $this->show_date);
$view->with('show_create', $this->show_create);
$view->with('is_admin', $this->is_admin);
$view->with('input_password', $this->get_input_password());
$view->with('tags', $this->get_tags());
$view->with('id', $this->item->id);
$view->with('load_comments_action', route('comments.load', $this->item->id));
$view->with('load_comment_form_action', route('comments.post', $this->item->id ));
if (isset($data['comments']) && $data['comments'] === false) {
$this->show_comments = false;
} else {
$view->with('load_comments_action', route('comments.load', $this->item->id));
$view->with('load_comment_form_action', route('comments.post', $this->item->id ));
}
$view->with('navigation', $this->navigation());
}

View file

@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Model;
class Page extends Model
{
use HasFactory;
public $timestamps = false;
public function scopePublished() {
return $this->where('is_draft', 0);

View file

@ -19,6 +19,7 @@
<div class="hyphenate">
<div property="text">{!! $content !!}</div>
<div property="text">{!! $item['content'] !!}</div>
@if (!empty($load_comments_action) && !empty($tags))
<div class="comment_section">
@if (!empty($load_comments_action))
<a name="comments"></a>
@ -29,5 +30,6 @@
<div id="new_comment" data-url="{!! $load_comment_form_action !!}"></div>
@endif
</div>
@endif
</div>
@endsection