]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/PageEditorData.php
Added core editor switching functionality
[bookstack] / app / Entities / Tools / PageEditorData.php
index a6818839dc27c9ed83491a3bfc3a10724a2f92d7..3e11641750c9a6cfe6c30f6d7e1ed345da27aa35 100644 (file)
@@ -4,19 +4,24 @@ namespace BookStack\Entities\Tools;
 
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Repos\PageRepo;
+use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
+use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
 
 class PageEditorData
 {
     protected Page $page;
     protected PageRepo $pageRepo;
+    protected string $requestedEditor;
 
     protected array $viewData;
     protected array $warnings;
 
-    public function __construct(Page $page, PageRepo $pageRepo)
+    public function __construct(Page $page, PageRepo $pageRepo, string $requestedEditor)
     {
         $this->page = $page;
         $this->pageRepo = $pageRepo;
+        $this->requestedEditor = $requestedEditor;
+
         $this->viewData = $this->build();
     }
 
@@ -53,6 +58,9 @@ class PageEditorData
             $this->warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
         }
 
+        $editorType = $this->getEditorType($page);
+        $this->updateContentForEditor($page, $editorType);
+
         return [
             'page'            => $page,
             'book'            => $page->book,
@@ -60,8 +68,44 @@ class PageEditorData
             'isDraftRevision' => $isDraftRevision,
             'draftsEnabled'   => $draftsEnabled,
             'templates'       => $templates,
-            'editor'          =>  setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown',
+            'editor'          => $editorType,
         ];
     }
 
+    protected function updateContentForEditor(Page $page, string $editorType): void
+    {
+        $isHtml = !empty($page->html) && empty($page->markdown);
+
+        // HTML to markdown-clean conversion
+        if ($editorType === 'markdown' && $isHtml && $this->requestedEditor === 'markdown-clean') {
+            $page->markdown = (new HtmlToMarkdown($page->html))->convert();
+        }
+
+        // Markdown to HTML conversion if we don't have HTML
+        if ($editorType === 'wysiwyg' && !$isHtml) {
+            $page->html = (new MarkdownToHtml($page->markdown))->convert();
+        }
+    }
+
+    /**
+     * Get the type of editor to show for editing the given page.
+     * Defaults based upon the current content of the page otherwise will fall back
+     * to system default but will take a requested type (if provided) if permissions allow.
+     */
+    protected function getEditorType(Page $page): string
+    {
+        $emptyPage = empty($page->html) && empty($page->markdown);
+        $pageType = (!empty($page->html) && empty($page->markdown)) ? 'wysiwyg' : 'markdown';
+        $systemDefault = setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown';
+        $editorType = $emptyPage ? $systemDefault : $pageType;
+
+        // Use requested editor if valid and if we have permission
+        $requestedType = explode('-', $this->requestedEditor)[0];
+        if (($requestedType === 'markdown' || $requestedType === 'wysiwyg') && userCan('editor-change')) {
+            $editorType = $requestedType;
+        }
+
+        return $editorType;
+    }
+
 }
\ No newline at end of file