Skip to content

Commit 2bdbe4e

Browse files
authored
Merge pull request #1 from contentstack/feature/supercharge-rte
Feature/supercharge rte
2 parents 68a89bd + a1e11fb commit 2bdbe4e

11 files changed

Lines changed: 1008 additions & 19 deletions

File tree

.talismanrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fileignoreconfig:
2+
- filename: README.md
3+
allowed_patterns: [API_KEY, DELIVERY_TOKEN, ENVIRONMENT, Contentstack::]

README.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,33 @@ use Contentstack\Utils\Resource\RenderableInterface;
3636
use Contentstack\Utils\Resource\EmbeddedObject;
3737
use Contentstack\Utils\Model\Option;
3838
use Contentstack\Utils\Model\Metadata;
39-
use Contentstack\Utils\Enum\StyleType;
40-
41-
class CustomOption extends Option {
39+
use Contentstack\Utils\Enum\StyleType;
40+
use Contentstack\Utils\Enum\NodeType;
41+
use Contentstack\Utils\Enum\MarkType;
42+
43+
class CustomOption extends Option {
44+
function renderMark(MarkType $markType, string $text): string
45+
{
46+
switch ($markType)
47+
{
48+
case MarkType::get(MarkType::BOLD):
49+
return "<b>".$text."</b>";
50+
default:
51+
return parent::renderMark($markType, $text);
52+
}
53+
}
54+
function renderNode(string $nodeType, object $node, string $innerHtml): string
55+
{
56+
switch ($nodeType)
57+
{
58+
case "p":
59+
return "<p class='class-id'>".$innerHtml."</p>";
60+
case "h1":
61+
return "<h1 class='class-id'>".$innerHtml."</h1>";
62+
default:
63+
return parent::renderNode($nodeType, $node, $innerHtml);
64+
}
65+
}
4266
function renderOptions(array $embeddedObject, Metadata $metadata): string
4367
{
4468
switch ($metadata->getStyleType()) {
@@ -61,7 +85,7 @@ class CustomOption extends Option {
6185
return "<a href=".$metadata->getAttribute("href")->value
6286
.">".$metadata->getText()."</a>"
6387
}
64-
return $resultString;
88+
return parent::renderOptions($embeddedObject, $metadata);
6589
}
6690
}
6791
```
@@ -70,8 +94,9 @@ class CustomOption extends Option {
7094
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
7195
7296
### Fetch Embedded Item(s) from a Single Entry:
97+
#### Render HTML RTE Embedded object
7398
74-
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:
99+
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:
75100
76101
```php
77102
use Contentstack\Contentstack;
@@ -84,9 +109,27 @@ $render_rich_text = Contentstack::renderContent($entry['rte_field_uid'], new Opt
84109

85110
If you want to render embedded items using the CustomOption function, you can refer to the code below:
86111
```php
87-
$rendered_rich_text = Contentstack.render_content($entry['rte_field_uid'], new CustomOption($entry));
112+
$rendered_rich_text = Contentstack.renderContent($entry['rte_field_uid'], new CustomOption($entry));
113+
```
114+
#### Render Supercharged RTE contents
115+
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:
116+
117+
118+
```php
119+
use Contentstack\Contentstack;
120+
use Contentstack\Utils\Model\Option;
121+
122+
$stack = Contentstack::Stack('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>');
123+
$entry = $stack->ContentType('<CONTENT_TYPE_UID>')->Entry('<ENTRY_UID>')->includeEmbeddedItems()->toJSON()->fetch();
124+
$render_rich_text = Contentstack::jsonToHtml($entry['rte_field_uid'], new Option($entry));
125+
```
126+
127+
If you want to render embedded items using the CustomOption function, you can refer to the code below:
128+
```php
129+
$rendered_rich_text = Contentstack.jsonToHtml($entry['rte_field_uid'], new CustomOption($entry));
88130
```
89131
### Fetch Embedded Item(s) from Multiple Entries
132+
#### Render HTML RTE Embedded object
90133

91134
To get embedded items from multiple entries, you need to provide the stack API key, environment name, delivery token, and content type’s UID.
92135
```php
@@ -100,3 +143,18 @@ for($i = 0; $i < count($result[0]); $i++) {
100143
$render_rich_text = Contentstack::renderContent($entry['rich_text_content'], new Option($entry));
101144
}
102145
```
146+
147+
#### Render Supercharged RTE contents
148+
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:
149+
150+
```php
151+
use Contentstack\Contentstack;
152+
use Contentstack\Utils\Model\Option;
153+
154+
$stack = Contentstack::Stack('<API_KEY>', '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', '<ENVIRONMENT>');
155+
$result = $stack->ContentType('<CONTENT_TYPE_UID>')->Query()->toJSON()->includeEmbeddedItems()->find()
156+
for($i = 0; $i < count($result[0]); $i++) {
157+
$entry = $result[0][$i];
158+
$render_rich_text = Contentstack::jsonToHtml($entry['rich_text_content'], new Option($entry));
159+
}
160+
```

src/Enum/MarkType.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contentstack\Utils\Enum;
6+
7+
use MabeEnum\Enum;
8+
9+
class MarkType extends Enum
10+
{
11+
const BOLD = 'bold';
12+
const ITALIC = 'italic';
13+
const UNDERLINE = 'underline';
14+
15+
const STRIKE_THROUGH = 'strikethrough';
16+
const INLINE_CODE = 'inlineCode';
17+
18+
const SUBSCRIPT = 'subscript';
19+
const SUPERSCRIPT = 'superscript';
20+
}

src/Enum/NodeType.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contentstack\Utils\Enum;
6+
7+
use MabeEnum\Enum;
8+
9+
class NodeType extends Enum
10+
{
11+
const DOCUMENT = 'doc';
12+
const PARAGRAPH = 'p';
13+
14+
const LINK = 'a';
15+
const IMAGE = 'img';
16+
const EMBED = 'embed';
17+
18+
const HEADING_1 = 'h1';
19+
const HEADING_2 = 'h2';
20+
const HEADING_3 = 'h3';
21+
const HEADING_4 = 'h4';
22+
const HEADING_5 = 'h5';
23+
const HEADING_6 = 'h6';
24+
25+
const ORDER_LIST = 'ol';
26+
const UNORDER_LIST = 'ul';
27+
const LIST_ITEM = 'li';
28+
29+
const HR = 'hr';
30+
31+
const TABLE = 'table';
32+
const TABLE_HEADER = 'thead';
33+
const TABLE_BODY = 'tbody';
34+
const TABLE_FOOTER = 'tfoot';
35+
const TABLE_ROW = 'tr';
36+
const TABLE_HEAD = 'th';
37+
const TABLE_DATA = 'td';
38+
39+
const BLOCK_QUOTE = 'blockquote';
40+
const CODE = 'code';
41+
42+
const TEXT = 'text';
43+
const REFERENCE = 'reference';
44+
}

src/Model/Metadata.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@
99

1010
class Metadata {
1111

12-
public function __construct(\DOMElement $element)
12+
public function __construct(object $element)
1313
{
14-
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
15-
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
16-
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
17-
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
18-
$this->text = $element->textContent;
19-
$this->attributes = $element->attributes;
20-
$this->element = $element;
14+
if ($element instanceof \DOMElement) {
15+
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
16+
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
17+
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
18+
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
19+
$this->text = $element->textContent;
20+
$this->attributes = $element->attributes;
21+
$this->element = $element;
22+
} else {
23+
$this->attributes = get_object_vars($element->attrs);
24+
$this->itemType = !empty($this->attributes->type)? EmbedItemType::byValue($this->attributes->type) : EmbedItemType::get(EmbedItemType::ENTRY);
25+
$this->styleType = !empty($this->attributes['display-type']) ? StyleType::byValue($this->attributes['display-type']) : StyleType::get(StyleType::BLOCK);
26+
$this->itemUid = !empty($this->attributes['entry-uid']) ? $this->attributes['entry-uid'] : $this->attributes['asset-uid'];
27+
$this->contentTypeUid = $this->attributes['content-type-uid'];
28+
$this->text = ($element->children && count($element->children) > 0) ? $element->children[0]->text : '';
29+
$this->element = $element;
30+
}
2131
}
2232

2333
/* Properties */
@@ -77,13 +87,18 @@ public function getContentTypeUid(): string
7787
return $this->contentTypeUid;
7888
}
7989

80-
public function getAttributes(): \DOMNamedNodeMap
90+
public function getAttributes(): object
8191
{
8292
return $this->attributes;
8393
}
8494

85-
public function getAttribute(string $name): string {
86-
return $this->element->getAttribute($name);
95+
public function getAttribute(string $name): ?string {
96+
if ($this->element instanceof \DOMElement) {
97+
return $this->element->getAttribute($name);
98+
} else if (isset($this->attributes[$name])) {
99+
return $this->attributes[$name];
100+
}
101+
return null;
87102
}
88103

89104
public function getOuterHTML(): string

src/Model/Option.php

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Contentstack\Utils\Resource\RenderableInterface;
99
use Contentstack\Utils\Resource\EmbeddedObject;
1010
use Contentstack\Utils\Enum\StyleType;
11+
use Contentstack\Utils\Enum\MarkType;
12+
use Contentstack\Utils\Enum\NodeType;
1113

1214
class Option implements RenderableInterface {
1315

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

1820
public $entry;
1921

20-
public function __construct(array $entry)
22+
public function __construct(array $entry = null)
2123
{
2224
$this->entry = $entry;
2325
}
26+
27+
function renderMark(MarkType $markType, string $text): string
28+
{
29+
$resultString = "";
30+
31+
switch ($markType) {
32+
case MarkType::get(MarkType::BOLD):
33+
$resultString = "<strong>".$text."</strong>";
34+
break;
35+
case MarkType::get(MarkType::ITALIC):
36+
$resultString = "<em>".$text."</em>";
37+
break;
38+
case MarkType::get(MarkType::UNDERLINE):
39+
$resultString = "<u>".$text."</u>";
40+
break;
41+
case MarkType::get(MarkType::STRIKE_THROUGH):
42+
$resultString = "<strike>".$text."</strike>";
43+
break;
44+
case MarkType::get(MarkType::INLINE_CODE):
45+
$resultString = "<span>".$text."</span>";
46+
break;
47+
case MarkType::get(MarkType::SUBSCRIPT):
48+
$resultString = "<sub>".$text."</sub>";
49+
break;
50+
case MarkType::get(MarkType::SUPERSCRIPT):
51+
$resultString = "<sup>".$text."</sup>";
52+
break;
53+
}
54+
return $resultString;
55+
}
56+
57+
function renderNode(string $nodeType, object $node, string $innerHtml): string
58+
{
59+
$resultString = "";
60+
$attrs = get_object_vars($node->attrs);
61+
switch ($nodeType)
62+
{
63+
case NodeType::get(NodeType::PARAGRAPH)->getValue():
64+
$resultString = "<p>".$innerHtml."</p>";
65+
break;
66+
case NodeType::get(NodeType::LINK)->getValue():
67+
$resultString = "<a href=\"".($attrs["href"] ?? "")."\">".$innerHtml."</a>";
68+
break;
69+
case NodeType::get(NodeType::IMAGE)->getValue():
70+
$resultString = "<img src=\"".($attrs["src"] ?? "")."\" />".$innerHtml;
71+
break;
72+
case NodeType::get(NodeType::EMBED)->getValue():
73+
$resultString = "<iframe src=\"".($attrs["src"] ?? "")."\">".$innerHtml."</iframe>";
74+
break;
75+
case NodeType::get(NodeType::HEADING_1)->getValue():
76+
$resultString = "<h1>".$innerHtml."</h1>";
77+
break;
78+
case NodeType::get(NodeType::HEADING_2)->getValue():
79+
$resultString = "<h2>".$innerHtml."</h2>";
80+
break;
81+
case NodeType::get(NodeType::HEADING_3)->getValue():
82+
$resultString = "<h3>".$innerHtml."</h3>";
83+
break;
84+
case NodeType::get(NodeType::HEADING_4)->getValue():
85+
$resultString = "<h4>".$innerHtml."</h4>";
86+
break;
87+
case NodeType::get(NodeType::HEADING_5)->getValue():
88+
$resultString = "<h5>".$innerHtml."</h5>";
89+
break;
90+
case NodeType::get(NodeType::HEADING_6)->getValue():
91+
$resultString = "<h6>".$innerHtml."</h6>";
92+
break;
93+
case NodeType::get(NodeType::ORDER_LIST)->getValue():
94+
$resultString = "<ol>".$innerHtml."</ol>";
95+
break;
96+
case NodeType::get(NodeType::UNORDER_LIST)->getValue():
97+
$resultString = "<ul>".$innerHtml."</ul>";
98+
break;
99+
case NodeType::get(NodeType::LIST_ITEM)->getValue():
100+
$resultString = "<li>".$innerHtml."</li>";
101+
break;
102+
case NodeType::get(NodeType::HR)->getValue():
103+
$resultString = "<hr>";
104+
break;
105+
case NodeType::get(NodeType::TABLE)->getValue():
106+
$resultString = "<table>".$innerHtml."</table>";
107+
break;
108+
case NodeType::get(NodeType::TABLE_HEADER)->getValue():
109+
$resultString = "<thead>".$innerHtml."</thead>";
110+
break;
111+
case NodeType::get(NodeType::TABLE_BODY)->getValue():
112+
$resultString = "<tbody>".$innerHtml."</tbody>";
113+
break;
114+
case NodeType::get(NodeType::TABLE_FOOTER)->getValue():
115+
$resultString = "<tfoot>".$innerHtml."</tfoot>";
116+
break;
117+
case NodeType::get(NodeType::TABLE_ROW)->getValue():
118+
$resultString = "<tr>".$innerHtml."</tr>";
119+
break;
120+
case NodeType::get(NodeType::TABLE_HEAD)->getValue():
121+
$resultString = "<th>".$innerHtml."</th>";
122+
break;
123+
case NodeType::get(NodeType::TABLE_DATA)->getValue():
124+
$resultString = "<td>".$innerHtml."</td>";
125+
break;
126+
case NodeType::get(NodeType::BLOCK_QUOTE)->getValue():
127+
$resultString = "<blockquote>".$innerHtml."</blockquote>";
128+
break;
129+
case NodeType::get(NodeType::CODE)->getValue():
130+
$resultString = "<code>".$innerHtml."</code>";
131+
break;
132+
default:
133+
$resultString = $innerHtml;
134+
break;
135+
}
136+
return $resultString;
137+
}
24138

25139
function renderOptions(array $embeddedObject, Metadata $metadata): string
26140
{

src/Resource/RenderableInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use Contentstack\Utils\Enum\EmbedItemType;
77
use Contentstack\Utils\Model\Metadata;
8+
use Contentstack\Utils\Enum\MarkType;
9+
use Contentstack\Utils\Enum\NodeType;
810

911
interface RenderableInterface
1012
{
@@ -13,4 +15,17 @@ interface RenderableInterface
1315
* @param $metadata - Tag details and attributes
1416
*/
1517
function renderOptions(array $embeddedObject, Metadata $metadata): string;
18+
19+
/**
20+
* @param $markType - MarkType for the text content
21+
* @param $text - Text content for rendering
22+
*/
23+
function renderMark(MarkType $markType, string $text): string;
24+
25+
/**
26+
* @param $nodeType - NodeType for the text content
27+
* @param $node - Json node content for rendering
28+
* @param $innerHtml - Child Html content for the node
29+
*/
30+
function renderNode(string $nodeType, object $node, string $innerHtml): string;
1631
}

0 commit comments

Comments
 (0)