Skip to content

Commit 8b935e7

Browse files
committed
Pages API: Made raw_html available on page responses
To provide a way to see the original un-pre-processed database HTML content. For #4310
1 parent 41c3ed1 commit 8b935e7

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

app/Entities/Controllers/PageApiController.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
class PageApiController extends ApiController
1515
{
16-
protected PageRepo $pageRepo;
17-
1816
protected $rules = [
1917
'create' => [
2018
'book_id' => ['required_without:chapter_id', 'integer'],
@@ -34,9 +32,9 @@ class PageApiController extends ApiController
3432
],
3533
];
3634

37-
public function __construct(PageRepo $pageRepo)
38-
{
39-
$this->pageRepo = $pageRepo;
35+
public function __construct(
36+
protected PageRepo $pageRepo
37+
) {
4038
}
4139

4240
/**

app/Entities/Models/Page.php

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function forJsonDisplay(): self
139139
{
140140
$refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
141141
$refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
142+
$refreshed->setAttribute('raw_html', $refreshed->html);
142143
$refreshed->html = (new PageContent($refreshed))->render();
143144

144145
return $refreshed;

dev/api/responses/pages-create.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"name": "My API Page",
66
"slug": "my-api-page",
77
"html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
8+
"raw_html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
89
"priority": 14,
910
"created_at": "2020-11-28T15:01:39.000000Z",
1011
"updated_at": "2020-11-28T15:01:39.000000Z",

dev/api/responses/pages-read.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"chapter_id": 0,
55
"name": "A page written in markdown",
66
"slug": "a-page-written-in-markdown",
7-
"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>",
7+
"html": "<h1 id=\"bkmrk-this-is-my-cool-page\">This is my cool page! With some included text</h1>",
8+
"raw_html": "<h1 id=\"bkmrk-this-is-my-cool-page\">This is my cool page! {{@1#bkmrk-a}}</h1>",
89
"priority": 13,
910
"created_at": "2020-02-02T21:40:38.000000Z",
1011
"updated_at": "2020-11-28T14:43:20.000000Z",

dev/api/responses/pages-update.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"name": "My updated API Page",
66
"slug": "my-updated-api-page",
77
"html": "<p id=\"bkmrk-my-new-api-page---up\">my new API page - Updated</p>",
8+
"raw_html": "<p id=\"bkmrk-my-new-api-page---up\">my new API page - Updated</p>",
89
"priority": 16,
910
"created_at": "2020-11-28T15:10:54.000000Z",
1011
"updated_at": "2020-11-28T15:13:03.000000Z",

tests/Api/PagesApiTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ public function test_read_endpoint_provides_rendered_html()
159159
$this->assertStringContainsString('testing', $html);
160160
}
161161

162+
public function test_read_endpoint_provides_raw_html()
163+
{
164+
$html = "<p>testing</p><script>alert('danger')</script><h1>Hello</h1>";
165+
166+
$this->actingAsApiEditor();
167+
$page = $this->entities->page();
168+
$page->html = $html;
169+
$page->save();
170+
171+
$resp = $this->getJson($this->baseEndpoint . "/{$page->id}");
172+
$this->assertEquals($html, $resp->json('raw_html'));
173+
$this->assertNotEquals($html, $resp->json('html'));
174+
}
175+
162176
public function test_read_endpoint_returns_not_found()
163177
{
164178
$this->actingAsApiEditor();

0 commit comments

Comments
 (0)