Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed
declare(strict_types=1);

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities;

Yassine Doghri
committed
use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;

Yassine Doghri
committed
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Autolink\AutolinkExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\MarkdownConverter;

Yassine Doghri
committed
/**
* @property int $id
* @property string $title
* @property string $link
* @property string $slug
* @property string $content_markdown
* @property string $content_html
* @property Time $created_at
* @property Time $updated_at
* @property Time|null $delete_at
*/

Yassine Doghri
committed
class Page extends Entity
{
protected string $link;
protected string $content_html;

Yassine Doghri
committed

Yassine Doghri
committed
/**
* @var array<string, string>
*/

Yassine Doghri
committed
protected $casts = [
'id' => 'integer',
'title' => 'string',
'slug' => 'string',
'content_markdown' => 'string',
'content_html' => 'string',

Yassine Doghri
committed
];
public function getLink(): string

Yassine Doghri
committed
{

Yassine Doghri
committed
return url_to('page', $this->attributes['slug']);

Yassine Doghri
committed
}
public function setContentMarkdown(string $contentMarkdown): static

Yassine Doghri
committed
{

Yassine Doghri
committed
$config = [

Yassine Doghri
committed
'allow_unsafe_links' => false,

Yassine Doghri
committed
];
$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new AutolinkExtension());
$environment->addExtension(new SmartPunctExtension());
$environment->addExtension(new DisallowedRawHtmlExtension());
$converter = new MarkdownConverter($environment);

Yassine Doghri
committed
$this->attributes['content_markdown'] = $contentMarkdown;

Yassine Doghri
committed
$this->attributes['content_html'] = $converter->convert($contentMarkdown);
return $this;