Made some tweaks to related content and other examples while there.
class Tag extends Model
{
protected $fillable = ['name', 'value', 'order'];
- protected $hidden = ['id', 'entity_id', 'entity_type'];
+ protected $hidden = ['id', 'entity_id', 'entity_type', 'created_at', 'updated_at'];
/**
* Get the entity that this tag belongs to
public $searchFactor = 2;
protected $fillable = ['name', 'description'];
- protected $hidden = ['restricted', 'pivot', 'image_id'];
+ protected $hidden = ['restricted', 'pivot', 'image_id', 'deleted_at'];
/**
* Get the url for this book.
protected $fillable = ['name', 'description', 'image_id'];
- protected $hidden = ['restricted', 'image_id'];
+ protected $hidden = ['restricted', 'image_id', 'deleted_at'];
/**
* Get the books in this shelf.
public $searchFactor = 1.3;
protected $fillable = ['name', 'description', 'priority', 'book_id'];
- protected $hidden = ['restricted', 'pivot'];
+ protected $hidden = ['restricted', 'pivot', 'deleted_at'];
/**
* Get the pages that this chapter contains.
<?php namespace BookStack\Entities\Models;
+use BookStack\Entities\Tools\PageContent;
use BookStack\Uploads\Attachment;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
public $textField = 'text';
- protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot'];
+ protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
protected $casts = [
'draft' => 'boolean',
{
return $this->revisions()->first();
}
+
+ /**
+ * Get this page for JSON display.
+ */
+ public function forJsonDisplay(): Page
+ {
+ $refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy']);
+ $refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
+ $refreshed->html = (new PageContent($refreshed))->render();
+ return $refreshed;
+ }
}
}
/**
- * Delete a single book from the system.
+ * Delete a single book.
+ * This will typically send the book to the recycle bin.
* @throws \Exception
*/
public function delete(string $id)
/**
- * Delete a single shelf from the system.
+ * Delete a single shelf.
+ * This will typically send the shelf to the recycle bin.
* @throws Exception
*/
public function delete(string $id)
}
/**
- * Delete a chapter from the system.
+ * Delete a chapter.
+ * This will typically send the chapter to the recycle bin.
*/
public function delete(string $id)
{
protected $rules = [
'create' => [
- 'book_id' => 'required_unless:chapter_id|integer',
- 'chapter_id' => 'required_unless:book_id|integer',
+ 'book_id' => 'required_without:chapter_id|integer',
+ 'chapter_id' => 'required_without:book_id|integer',
'name' => 'required|string|max:255',
'html' => 'required_without:markdown|string',
'markdown' => 'required_without:html|string',
/**
* Create a new page in the system.
+ *
+ * The ID of a parent book or chapter is required to indicate
+ * where this page should be located.
+ *
+ * Any HTML content provided should be kept to a single-block depth of plain HTML
+ * elements to remain compatible with the BookStack front-end and editors.
*/
public function create(Request $request)
{
$draft = $this->pageRepo->getNewDraftPage($parent);
$this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
- return response()->json($draft->load(['tags']));
+ return response()->json($draft->forJsonDisplay());
}
/**
* View the details of a single page.
+ *
+ * Pages will always have HTML content. They may have markdown content
+ * if the markdown editor was used to last update the page.
*/
public function read(string $id)
{
- $page = $this->pageRepo->getById($id, ['tags', 'createdBy', 'updatedBy']);
- return response()->json($page);
+ $page = $this->pageRepo->getById($id, []);
+ return response()->json($page->forJsonDisplay());
}
/**
* Update the details of a single page.
+ *
+ * See the 'create' action for details on the provided HTML/Markdown.
+ * Providing a 'book_id' or 'chapter_id' property will essentially move
+ * the page into that parent element if you have permissions to do so.
*/
public function update(Request $request, string $id)
{
}
$updatedPage = $this->pageRepo->update($page, $request->all());
- return response()->json($updatedPage->load(['tags']));
+ return response()->json($updatedPage->forJsonDisplay());
}
/**
- * Delete a page from the system.
+ * Delete a page.
+ * This will typically send the page to the recycle bin.
*/
public function delete(string $id)
{
--- /dev/null
+{
+ "book_id": 1,
+ "name": "My API Page",
+ "html": "<p>my new API page</p>",
+ "tags": [
+ {"name": "Category", "value": "Not Bad Content"},
+ {"name": "Rating", "value": "Average"}
+ ]
+}
\ No newline at end of file
--- /dev/null
+{
+ "chapter_id": 1,
+ "name": "My updated API Page",
+ "html": "<p>my new API page - Updated</p>",
+ "tags": [
+ {"name": "Category", "value": "API Examples"},
+ {"name": "Rating", "value": "Alright"}
+ ]
+}
\ No newline at end of file
"tags": [
{
"id": 13,
- "entity_id": 16,
- "entity_type": "BookStack\\Book",
"name": "Category",
"value": "Guide",
- "order": 0,
- "created_at": "2020-01-12 14:11:51",
- "updated_at": "2020-01-12 14:11:51"
+ "order": 0
}
],
"cover": {
{
"name": "Category",
"value": "Guide",
- "order": 0,
- "created_at": "2020-05-22 22:51:51",
- "updated_at": "2020-05-22 22:51:51"
+ "order": 0
}
],
"pages": [
"updated_at": "2019-08-26 14:32:59",
"created_by": 1,
"updated_by": 1,
- "draft": 0,
+ "draft": false,
"revision_count": 2,
- "template": 0
+ "template": false
},
{
"id": 7,
"updated_at": "2019-06-06 12:03:04",
"created_by": 3,
"updated_by": 3,
- "draft": 0,
+ "draft": false,
"revision_count": 1,
- "template": 0
+ "template": false
}
]
}
\ No newline at end of file
--- /dev/null
+{
+ "id": 358,
+ "book_id": 1,
+ "chapter_id": 0,
+ "name": "My API Page",
+ "slug": "my-api-page",
+ "html": "<p id=\"bkmrk-my-new-api-page\">my new API page</p>",
+ "priority": 14,
+ "created_at": "2020-11-28 15:01:39",
+ "updated_at": "2020-11-28 15:01:39",
+ "created_by": {
+ "id": 1,
+ "name": "Admin"
+ },
+ "updated_by": {
+ "id": 1,
+ "name": "Admin"
+ },
+ "draft": false,
+ "markdown": "",
+ "revision_count": 1,
+ "template": false,
+ "tags": [
+ {
+ "name": "Category",
+ "value": "Not Bad Content",
+ "order": 0
+ },
+ {
+ "name": "Rating",
+ "value": "Average",
+ "order": 1
+ }
+ ]
+}
\ No newline at end of file
--- /dev/null
+{
+ "data": [
+ {
+ "id": 1,
+ "book_id": 1,
+ "chapter_id": 1,
+ "name": "How to create page content",
+ "slug": "how-to-create-page-content",
+ "priority": 0,
+ "draft": false,
+ "template": false,
+ "created_at": "2019-05-05 21:49:58",
+ "updated_at": "2020-07-04 15:50:58",
+ "created_by": 1,
+ "updated_by": 1
+ },
+ {
+ "id": 2,
+ "book_id": 1,
+ "chapter_id": 1,
+ "name": "How to use images",
+ "slug": "how-to-use-images",
+ "priority": 2,
+ "draft": false,
+ "template": false,
+ "created_at": "2019-05-05 21:53:30",
+ "updated_at": "2019-06-06 12:03:04",
+ "created_by": 1,
+ "updated_by": 1
+ },
+ {
+ "id": 3,
+ "book_id": 1,
+ "chapter_id": 1,
+ "name": "Drawings via draw.io",
+ "slug": "drawings-via-drawio",
+ "priority": 3,
+ "draft": false,
+ "template": false,
+ "created_at": "2019-05-05 21:53:49",
+ "updated_at": "2019-12-18 21:56:52",
+ "created_by": 1,
+ "updated_by": 1
+ }
+ ],
+ "total": 322
+}
\ No newline at end of file
--- /dev/null
+{
+ "id": 306,
+ "book_id": 1,
+ "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>",
+ "priority": 13,
+ "created_at": "2020-02-02 21:40:38",
+ "updated_at": "2020-11-28 14:43:20",
+ "created_by": {
+ "id": 1,
+ "name": "Admin"
+ },
+ "updated_by": {
+ "id": 1,
+ "name": "Admin"
+ },
+ "draft": false,
+ "markdown": "# How this is built\r\n\r\nThis page is written in markdown. BookStack stores the page data in HTML.\r\n\r\nHere's a cute picture of my cat:\r\n\r\n[](http://example.com/uploads/images/gallery/2020-04/yXSrubes.jpg)",
+ "revision_count": 5,
+ "template": false,
+ "tags": [
+ {
+ "name": "Category",
+ "value": "Top Content",
+ "order": 0
+ },
+ {
+ "name": "Animal",
+ "value": "Cat",
+ "order": 1
+ }
+ ]
+}
\ No newline at end of file
--- /dev/null
+{
+ "id": 361,
+ "book_id": 1,
+ "chapter_id": 1,
+ "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>",
+ "priority": 16,
+ "created_at": "2020-11-28 15:10:54",
+ "updated_at": "2020-11-28 15:13:03",
+ "created_by": {
+ "id": 1,
+ "name": "Admin"
+ },
+ "updated_by": {
+ "id": 1,
+ "name": "Admin"
+ },
+ "draft": false,
+ "markdown": "",
+ "revision_count": 5,
+ "template": false,
+ "tags": [
+ {
+ "name": "Category",
+ "value": "API Examples",
+ "order": 0
+ },
+ {
+ "name": "Rating",
+ "value": "Alright",
+ "order": 0
+ }
+ ]
+}
\ No newline at end of file
"tags": [
{
"id": 16,
- "entity_id": 14,
- "entity_type": "BookStack\\Bookshelf",
"name": "Category",
"value": "Guide",
- "order": 0,
- "created_at": "2020-04-10 13:31:04",
- "updated_at": "2020-04-10 13:31:04"
+ "order": 0
}
],
"cover": {