3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Util\HtmlContentFilter;
10 class EntityHtmlDescription
12 protected string $html = '';
13 protected string $plain = '';
15 public function __construct(
16 protected Book|Chapter|Bookshelf $entity,
18 $this->html = $this->entity->description_html ?? '';
19 $this->plain = $this->entity->description ?? '';
23 * Update the description from HTML code.
24 * Optionally takes plaintext to use for the model also.
26 public function set(string $html, string|null $plaintext = null): void
29 $this->entity->description_html = $this->html;
31 if ($plaintext !== null) {
32 $this->plain = $plaintext;
33 $this->entity->description = $this->plain;
36 if (empty($html) && !empty($plaintext)) {
37 $this->html = $this->getHtml();
38 $this->entity->description_html = $this->html;
43 * Get the description as HTML.
44 * Optionally returns the raw HTML if requested.
46 public function getHtml(bool $raw = false): string
48 $html = $this->html ?: '<p>' . nl2br(e($this->plain)) . '</p>';
53 return HtmlContentFilter::removeScriptsFromHtmlString($html);
56 public function getPlain(): string