]> BookStack Code Mirror - bookstack/commitdiff
Pages API: Made raw_html available on page responses
authorDan Brown <redacted>
Tue, 20 Jun 2023 16:07:46 +0000 (17:07 +0100)
committerDan Brown <redacted>
Tue, 20 Jun 2023 16:07:46 +0000 (17:07 +0100)
To provide a way to see the original un-pre-processed database HTML
content.

For #4310

app/Entities/Controllers/PageApiController.php
app/Entities/Models/Page.php
dev/api/responses/pages-create.json
dev/api/responses/pages-read.json
dev/api/responses/pages-update.json
tests/Api/PagesApiTest.php

index 28dd36f973e0d487f74bc6be8b0af30e3bf986c8..655eeeec9ec4c2ce9bc4713a75bd7e9b5dec3962 100644 (file)
@@ -13,8 +13,6 @@ use Illuminate\Http\Request;
 
 class PageApiController extends ApiController
 {
 
 class PageApiController extends ApiController
 {
-    protected PageRepo $pageRepo;
-
     protected $rules = [
         'create' => [
             'book_id'    => ['required_without:chapter_id', 'integer'],
     protected $rules = [
         'create' => [
             'book_id'    => ['required_without:chapter_id', 'integer'],
@@ -34,9 +32,9 @@ class PageApiController extends ApiController
         ],
     ];
 
         ],
     ];
 
-    public function __construct(PageRepo $pageRepo)
-    {
-        $this->pageRepo = $pageRepo;
+    public function __construct(
+        protected PageRepo $pageRepo
+    ) {
     }
 
     /**
     }
 
     /**
index 40acb9a354b259e648dfb5461fb1fadd06b37528..7e2c12c2048d94f93745f803a8927942b79ad120 100644 (file)
@@ -139,6 +139,7 @@ class Page extends BookChild
     {
         $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
         $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
     {
         $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
         $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
+        $refreshed->setAttribute('raw_html', $refreshed->html);
         $refreshed->html = (new PageContent($refreshed))->render();
 
         return $refreshed;
         $refreshed->html = (new PageContent($refreshed))->render();
 
         return $refreshed;
index eeaa5303af6d5eea824c59c733945b209d46add0..5c3d8021504566f5eae13380c8476ddec75aeb7f 100644 (file)
@@ -5,6 +5,7 @@
        "name": "My API Page",
        "slug": "my-api-page",
        "html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
        "name": "My API Page",
        "slug": "my-api-page",
        "html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
+       "raw_html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
        "priority": 14,
        "created_at": "2020-11-28T15:01:39.000000Z",
        "updated_at": "2020-11-28T15:01:39.000000Z",
        "priority": 14,
        "created_at": "2020-11-28T15:01:39.000000Z",
        "updated_at": "2020-11-28T15:01:39.000000Z",
index 9a21cd44cad93a2fc13846d7c54e687faece45da..a47990cc67532bfe7bfd5c191eb34ee6fb88ad6c 100644 (file)
@@ -4,7 +4,8 @@
        "chapter_id": 0,
        "name": "A page written in markdown",
        "slug": "a-page-written-in-markdown",
        "chapter_id": 0,
        "name": "A page written in markdown",
        "slug": "a-page-written-in-markdown",
-       "html": "<h1 id=\"bkmrk-how-this-is-built\">How this is built</h1>\r\n<p id=\"bkmrk-this-page-is-written\">This page is written in markdown. BookStack stores the page data in HTML.</p>\r\n<p id=\"bkmrk-here%27s-a-cute-pictur\">Here's a cute picture of my cat:</p>\r\n<p id=\"bkmrk-\"><a href=\"http://example.com/uploads/images/gallery/2020-04/yXSrubes.jpg\"><img src=\"http://example.com/uploads/images/gallery/2020-04/scaled-1680-/yXSrubes.jpg\" alt=\"yXSrubes.jpg\"></a></p>",
+       "html": "<h1 id=\"bkmrk-this-is-my-cool-page\">This is my cool page! With some included text</h1>",
+       "raw_html": "<h1 id=\"bkmrk-this-is-my-cool-page\">This is my cool page! {{@1#bkmrk-a}}</h1>",
        "priority": 13,
        "created_at": "2020-02-02T21:40:38.000000Z",
        "updated_at": "2020-11-28T14:43:20.000000Z",
        "priority": 13,
        "created_at": "2020-02-02T21:40:38.000000Z",
        "updated_at": "2020-11-28T14:43:20.000000Z",
index 0b8b2374c180fe66c4497b2f56095b5fc5cb4675..e91b74661d8951fa35cb798ba368694b7cc6ca39 100644 (file)
@@ -5,6 +5,7 @@
        "name": "My updated API Page",
        "slug": "my-updated-api-page",
        "html": "<p id=\"bkmrk-my-new-api-page---up\">my new API page - Updated</p>",
        "name": "My updated API Page",
        "slug": "my-updated-api-page",
        "html": "<p id=\"bkmrk-my-new-api-page---up\">my new API page - Updated</p>",
+       "raw_html": "<p id=\"bkmrk-my-new-api-page---up\">my new API page - Updated</p>",
        "priority": 16,
        "created_at": "2020-11-28T15:10:54.000000Z",
        "updated_at": "2020-11-28T15:13:03.000000Z",
        "priority": 16,
        "created_at": "2020-11-28T15:10:54.000000Z",
        "updated_at": "2020-11-28T15:13:03.000000Z",
index 75cc2807fa58fc8c5d5978474541d8c458eb4b60..4a81f738bbdb092f12ce922331a7011600331b71 100644 (file)
@@ -159,6 +159,20 @@ class PagesApiTest extends TestCase
         $this->assertStringContainsString('testing', $html);
     }
 
         $this->assertStringContainsString('testing', $html);
     }
 
+    public function test_read_endpoint_provides_raw_html()
+    {
+        $html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
+
+        $this->actingAsApiEditor();
+        $page = $this->entities->page();
+        $page->html = $html;
+        $page->save();
+
+        $resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
+        $this->assertEquals($html, $resp->json('raw_html'));
+        $this->assertNotEquals($html, $resp->json('html'));
+    }
+
     public function test_read_endpoint_returns_not_found()
     {
         $this->actingAsApiEditor();
     public function test_read_endpoint_returns_not_found()
     {
         $this->actingAsApiEditor();