3 namespace BookStack\Entities\Tools;
5 use BookStack\Activity\Tools\CommentTree;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Queries\EntityQueries;
8 use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
9 use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
13 protected array $viewData;
14 protected array $warnings;
16 public function __construct(
18 protected EntityQueries $queries,
19 protected string $requestedEditor
21 $this->viewData = $this->build();
24 public function getViewData(): array
26 return $this->viewData;
29 public function getWarnings(): array
31 return $this->warnings;
34 protected function build(): array
36 $page = clone $this->page;
37 $isDraft = boolval($this->page->draft);
38 $templates = $this->queries->pages->visibleTemplates()
39 ->orderBy('name', 'asc')
43 $draftsEnabled = auth()->check();
45 $isDraftRevision = false;
47 $editActivity = new PageEditActivity($page);
49 if ($editActivity->hasActiveEditing()) {
50 $this->warnings[] = $editActivity->activeEditingMessage();
53 // Check for a current draft version for this user
54 $userDraft = $this->queries->revisions->findLatestCurrentUserDraftsForPageId($page->id)->first();
55 if (!is_null($userDraft)) {
56 $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
57 $isDraftRevision = true;
58 $this->warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
61 $editorType = $this->getEditorType($page);
62 $this->updateContentForEditor($page, $editorType);
66 'book' => $page->book,
67 'isDraft' => $isDraft,
68 'isDraftRevision' => $isDraftRevision,
69 'draftsEnabled' => $draftsEnabled,
70 'templates' => $templates,
71 'editor' => $editorType,
72 'comments' => new CommentTree($page),
76 protected function updateContentForEditor(Page $page, string $editorType): void
78 $isHtml = !empty($page->html) && empty($page->markdown);
80 // HTML to markdown-clean conversion
81 if ($editorType === 'markdown' && $isHtml && $this->requestedEditor === 'markdown-clean') {
82 $page->markdown = (new HtmlToMarkdown($page->html))->convert();
85 // Markdown to HTML conversion if we don't have HTML
86 if ($editorType === 'wysiwyg' && !$isHtml) {
87 $page->html = (new MarkdownToHtml($page->markdown))->convert();
92 * Get the type of editor to show for editing the given page.
93 * Defaults based upon the current content of the page otherwise will fall back
94 * to system default but will take a requested type (if provided) if permissions allow.
96 protected function getEditorType(Page $page): string
98 $editorType = $page->editor ?: self::getSystemDefaultEditor();
100 // Use requested editor if valid and if we have permission
101 $requestedType = explode('-', $this->requestedEditor)[0];
102 if (($requestedType === 'markdown' || $requestedType === 'wysiwyg') && userCan('editor-change')) {
103 $editorType = $requestedType;
110 * Get the configured system default editor.
112 public static function getSystemDefaultEditor(): string
114 return setting('app-editor') === 'markdown' ? 'markdown' : 'wysiwyg';