Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fileignoreconfig:
- filename: README.md
allowed_patterns: [API_KEY, DELIVERY_TOKEN, ENVIRONMENT, Contentstack::]
70 changes: 64 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,33 @@ use Contentstack\Utils\Resource\RenderableInterface;
use Contentstack\Utils\Resource\EmbeddedObject;
use Contentstack\Utils\Model\Option;
use Contentstack\Utils\Model\Metadata;
use Contentstack\Utils\Enum\StyleType;

class CustomOption extends Option {
use Contentstack\Utils\Enum\StyleType;
use Contentstack\Utils\Enum\NodeType;
use Contentstack\Utils\Enum\MarkType;

class CustomOption extends Option {
function renderMark(MarkType $markType, string $text): string
{
switch ($markType)
{
case MarkType::get(MarkType::BOLD):
return "<b>".$text."</b>";
default:
return parent::renderMark($markType, $text);
}
}
function renderNode(string $nodeType, object $node, string $innerHtml): string
{
switch ($nodeType)
{
case "p":
return "<p class='class-id'>".$innerHtml."</p>";
case "h1":
return "<h1 class='class-id'>".$innerHtml."</h1>";
default:
return parent::renderNode($nodeType, $node, $innerHtml);
}
}
function renderOptions(array $embeddedObject, Metadata $metadata): string
{
switch ($metadata->getStyleType()) {
Expand All @@ -61,7 +85,7 @@ class CustomOption extends Option {
return "<a href=".$metadata->getAttribute("href")->value
.">".$metadata->getText()."</a>"
}
return $resultString;
return parent::renderOptions($embeddedObject, $metadata);
}
}
```
Expand All @@ -70,8 +94,9 @@ class CustomOption extends Option {
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.

### Fetch Embedded Item(s) from a Single Entry:
#### Render HTML RTE Embedded object

To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type’s UID, and entry’s UID. Then, use the includeEmbeddedItems function as shown below:
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type’s UID, and entry’s UID. Then, use the `Contentstack::renderContent` function as shown below:

```php
use Contentstack\Contentstack;
Expand All @@ -84,9 +109,27 @@ $render_rich_text = Contentstack::renderContent($entry['rte_field_uid'], new Opt

If you want to render embedded items using the CustomOption function, you can refer to the code below:
```php
$rendered_rich_text = Contentstack.render_content($entry['rte_field_uid'], new CustomOption($entry));
$rendered_rich_text = Contentstack.renderContent($entry['rte_field_uid'], new CustomOption($entry));
```
#### Render Supercharged RTE contents
To get a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use `Contentstack::jsonToHtml` function as shown below:


```php
use Contentstack\Contentstack;
use Contentstack\Utils\Model\Option;

$stack = Contentstack::Stack('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>');
$entry = $stack->ContentType('<CONTENT_TYPE_UID>')->Entry('<ENTRY_UID>')->includeEmbeddedItems()->toJSON()->fetch();
$render_rich_text = Contentstack::jsonToHtml($entry['rte_field_uid'], new Option($entry));
```

If you want to render embedded items using the CustomOption function, you can refer to the code below:
```php
$rendered_rich_text = Contentstack.jsonToHtml($entry['rte_field_uid'], new CustomOption($entry));
```
### Fetch Embedded Item(s) from Multiple Entries
#### Render HTML RTE Embedded object

To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type’s UID.
```php
Expand All @@ -100,3 +143,18 @@ for($i = 0; $i < count($result[0]); $i++) {
$render_rich_text = Contentstack::renderContent($entry['rich_text_content'], new Option($entry));
}
```

#### Render Supercharged RTE contents
To get a single entry, you need to provide the stack API key, environment name, delivery token, content type UID. Then, use `Contentstack::jsonToHtml` function as shown below:

```php
use Contentstack\Contentstack;
use Contentstack\Utils\Model\Option;

$stack = Contentstack::Stack('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>');
$result = $stack->ContentType('<CONTENT_TYPE_UID>')->Query()->toJSON()->includeEmbeddedItems()->find()
for($i = 0; $i < count($result[0]); $i++) {
$entry = $result[0][$i];
$render_rich_text = Contentstack::jsonToHtml($entry['rich_text_content'], new Option($entry));
}
```
20 changes: 20 additions & 0 deletions src/Enum/MarkType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Contentstack\Utils\Enum;

use MabeEnum\Enum;

class MarkType extends Enum
{
const BOLD = 'bold';
const ITALIC = 'italic';
const UNDERLINE = 'underline';

const STRIKE_THROUGH = 'strikethrough';
const INLINE_CODE = 'inlineCode';

const SUBSCRIPT = 'subscript';
const SUPERSCRIPT = 'superscript';
}
44 changes: 44 additions & 0 deletions src/Enum/NodeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Contentstack\Utils\Enum;

use MabeEnum\Enum;

class NodeType extends Enum
{
const DOCUMENT = 'doc';
const PARAGRAPH = 'p';

const LINK = 'a';
const IMAGE = 'img';
const EMBED = 'embed';

const HEADING_1 = 'h1';
const HEADING_2 = 'h2';
const HEADING_3 = 'h3';
const HEADING_4 = 'h4';
const HEADING_5 = 'h5';
const HEADING_6 = 'h6';

const ORDER_LIST = 'ol';
const UNORDER_LIST = 'ul';
const LIST_ITEM = 'li';

const HR = 'hr';

const TABLE = 'table';
const TABLE_HEADER = 'thead';
const TABLE_BODY = 'tbody';
const TABLE_FOOTER = 'tfoot';
const TABLE_ROW = 'tr';
const TABLE_HEAD = 'th';
const TABLE_DATA = 'td';

const BLOCK_QUOTE = 'blockquote';
const CODE = 'code';

const TEXT = 'text';
const REFERENCE = 'reference';
}
37 changes: 26 additions & 11 deletions src/Model/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@

class Metadata {

public function __construct(\DOMElement $element)
public function __construct(object $element)
{
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
$this->text = $element->textContent;
$this->attributes = $element->attributes;
$this->element = $element;
if ($element instanceof \DOMElement) {
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
$this->text = $element->textContent;
$this->attributes = $element->attributes;
$this->element = $element;
} else {
$this->attributes = get_object_vars($element->attrs);
$this->itemType = !empty($this->attributes->type)? EmbedItemType::byValue($this->attributes->type) : EmbedItemType::get(EmbedItemType::ENTRY);
$this->styleType = !empty($this->attributes['display-type']) ? StyleType::byValue($this->attributes['display-type']) : StyleType::get(StyleType::BLOCK);
$this->itemUid = !empty($this->attributes['entry-uid']) ? $this->attributes['entry-uid'] : $this->attributes['asset-uid'];
$this->contentTypeUid = $this->attributes['content-type-uid'];
$this->text = ($element->children && count($element->children) > 0) ? $element->children[0]->text : '';
$this->element = $element;
}
}

/* Properties */
Expand Down Expand Up @@ -77,13 +87,18 @@ public function getContentTypeUid(): string
return $this->contentTypeUid;
}

public function getAttributes(): \DOMNamedNodeMap
public function getAttributes(): object
{
return $this->attributes;
}

public function getAttribute(string $name): string {
return $this->element->getAttribute($name);
public function getAttribute(string $name): ?string {
if ($this->element instanceof \DOMElement) {
return $this->element->getAttribute($name);
} else if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}

public function getOuterHTML(): string
Expand Down
116 changes: 115 additions & 1 deletion src/Model/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Contentstack\Utils\Resource\RenderableInterface;
use Contentstack\Utils\Resource\EmbeddedObject;
use Contentstack\Utils\Enum\StyleType;
use Contentstack\Utils\Enum\MarkType;
use Contentstack\Utils\Enum\NodeType;

class Option implements RenderableInterface {

Expand All @@ -17,10 +19,122 @@ class Option implements RenderableInterface {

public $entry;

public function __construct(array $entry)
public function __construct(array $entry = null)
{
$this->entry = $entry;
}

function renderMark(MarkType $markType, string $text): string
{
$resultString = "";

switch ($markType) {
case MarkType::get(MarkType::BOLD):
$resultString = "<strong>".$text."</strong>";
break;
case MarkType::get(MarkType::ITALIC):
$resultString = "<em>".$text."</em>";
break;
case MarkType::get(MarkType::UNDERLINE):
$resultString = "<u>".$text."</u>";
break;
case MarkType::get(MarkType::STRIKE_THROUGH):
$resultString = "<strike>".$text."</strike>";
break;
case MarkType::get(MarkType::INLINE_CODE):
$resultString = "<span>".$text."</span>";
break;
case MarkType::get(MarkType::SUBSCRIPT):
$resultString = "<sub>".$text."</sub>";
break;
case MarkType::get(MarkType::SUPERSCRIPT):
$resultString = "<sup>".$text."</sup>";
break;
}
return $resultString;
}

function renderNode(string $nodeType, object $node, string $innerHtml): string
{
$resultString = "";
$attrs = get_object_vars($node->attrs);
switch ($nodeType)
{
case NodeType::get(NodeType::PARAGRAPH)->getValue():
$resultString = "<p>".$innerHtml."</p>";
break;
case NodeType::get(NodeType::LINK)->getValue():
$resultString = "<a href=\"".($attrs["href"] ?? "")."\">".$innerHtml."</a>";
break;
case NodeType::get(NodeType::IMAGE)->getValue():
$resultString = "<img src=\"".($attrs["src"] ?? "")."\" />".$innerHtml;
break;
case NodeType::get(NodeType::EMBED)->getValue():
$resultString = "<iframe src=\"".($attrs["src"] ?? "")."\">".$innerHtml."</iframe>";
break;
case NodeType::get(NodeType::HEADING_1)->getValue():
$resultString = "<h1>".$innerHtml."</h1>";
break;
case NodeType::get(NodeType::HEADING_2)->getValue():
$resultString = "<h2>".$innerHtml."</h2>";
break;
case NodeType::get(NodeType::HEADING_3)->getValue():
$resultString = "<h3>".$innerHtml."</h3>";
break;
case NodeType::get(NodeType::HEADING_4)->getValue():
$resultString = "<h4>".$innerHtml."</h4>";
break;
case NodeType::get(NodeType::HEADING_5)->getValue():
$resultString = "<h5>".$innerHtml."</h5>";
break;
case NodeType::get(NodeType::HEADING_6)->getValue():
$resultString = "<h6>".$innerHtml."</h6>";
break;
case NodeType::get(NodeType::ORDER_LIST)->getValue():
$resultString = "<ol>".$innerHtml."</ol>";
break;
case NodeType::get(NodeType::UNORDER_LIST)->getValue():
$resultString = "<ul>".$innerHtml."</ul>";
break;
case NodeType::get(NodeType::LIST_ITEM)->getValue():
$resultString = "<li>".$innerHtml."</li>";
break;
case NodeType::get(NodeType::HR)->getValue():
$resultString = "<hr>";
break;
case NodeType::get(NodeType::TABLE)->getValue():
$resultString = "<table>".$innerHtml."</table>";
break;
case NodeType::get(NodeType::TABLE_HEADER)->getValue():
$resultString = "<thead>".$innerHtml."</thead>";
break;
case NodeType::get(NodeType::TABLE_BODY)->getValue():
$resultString = "<tbody>".$innerHtml."</tbody>";
break;
case NodeType::get(NodeType::TABLE_FOOTER)->getValue():
$resultString = "<tfoot>".$innerHtml."</tfoot>";
break;
case NodeType::get(NodeType::TABLE_ROW)->getValue():
$resultString = "<tr>".$innerHtml."</tr>";
break;
case NodeType::get(NodeType::TABLE_HEAD)->getValue():
$resultString = "<th>".$innerHtml."</th>";
break;
case NodeType::get(NodeType::TABLE_DATA)->getValue():
$resultString = "<td>".$innerHtml."</td>";
break;
case NodeType::get(NodeType::BLOCK_QUOTE)->getValue():
$resultString = "<blockquote>".$innerHtml."</blockquote>";
break;
case NodeType::get(NodeType::CODE)->getValue():
$resultString = "<code>".$innerHtml."</code>";
break;
default:
$resultString = $innerHtml;
break;
}
return $resultString;
}

function renderOptions(array $embeddedObject, Metadata $metadata): string
{
Expand Down
15 changes: 15 additions & 0 deletions src/Resource/RenderableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Contentstack\Utils\Enum\EmbedItemType;
use Contentstack\Utils\Model\Metadata;
use Contentstack\Utils\Enum\MarkType;
use Contentstack\Utils\Enum\NodeType;

interface RenderableInterface
{
Expand All @@ -13,4 +15,17 @@ interface RenderableInterface
* @param $metadata - Tag details and attributes
*/
function renderOptions(array $embeddedObject, Metadata $metadata): string;

/**
* @param $markType - MarkType for the text content
* @param $text - Text content for rendering
*/
function renderMark(MarkType $markType, string $text): string;

/**
* @param $nodeType - NodeType for the text content
* @param $node - Json node content for rendering
* @param $innerHtml - Child Html content for the node
*/
function renderNode(string $nodeType, object $node, string $innerHtml): string;
}
Loading