Skip to content

Commit 393c5ac

Browse files
authored
Merge pull request #2 from contentstack/feature/supercharge-rte
Feature/supercharge rte
2 parents 2bdbe4e + 3eb4917 commit 393c5ac

9 files changed

Lines changed: 519 additions & 97 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Setup PHP with Xdebug 2.x
1818
uses: shivammathur/setup-php@v2
1919
with:
20-
php-version: '7.4'
20+
php-version: '8.0'
2121
coverage: xdebug2
2222

2323
- name: Validate composer.json and composer.lock

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## [1.1.0](https://github.com/contentstack/contentstack-utils-php/tree/v1.1.0) (2021-07-16)
3+
- JSON RTE to html content functionality for GQL API added
4+
## [1.0.1](https://github.com/contentstack/contentstack-utils-php/tree/v1.0.1) (2021-07-16)
5+
- JSON RTE to html content functionality added
26

37
## [1.0.0](https://github.com/contentstack/contentstack-utils-php/tree/v1.0.0) (2021-04-05)
48
- Initial release for Contentstack Utils SDK

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,15 @@ for($i = 0; $i < count($result[0]); $i++) {
158158
$render_rich_text = Contentstack::jsonToHtml($entry['rich_text_content'], new Option($entry));
159159
}
160160
```
161+
#### GQL
162+
163+
To Convert JSON RTE content to HTML from GQL API. Use `GQL::jsonToHtml` function as shown below:
164+
165+
```php
166+
use Contentstack\Utils\GQL;
167+
use Contentstack\Utils\Model\Option;
168+
// GQL fetch API
169+
...
170+
$render_html_text = GQL::jsonToHtml($entry->rich_text_content,, new Option());
171+
...
172+
```

src/BaseParser.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contentstack\Utils;
6+
7+
use Contentstack\Utils\Model\Option;
8+
use Contentstack\Utils\Model\Metadata;
9+
use Contentstack\Utils\Enum\NodeType;
10+
use Contentstack\Utils\Enum\MarkType;
11+
12+
class BaseParser
13+
{
14+
15+
protected static function nodeChildrenToHtml(array $nodes, Option $option, $renderEmbed): string {
16+
return \implode('', \array_map(function (object $node) use ($option, $renderEmbed): string {
17+
return BaseParser::nodeToHtml($node, $option, $renderEmbed);
18+
}, $nodes));
19+
}
20+
21+
protected static function nodeToHtml(object $node, Option $option, $renderEmbed): string {
22+
$resultHtml = '';
23+
if (isset($node->type)) {
24+
switch ($node->type) {
25+
case NodeType::get(NodeType::REFERENCE)->getValue():
26+
$resultHtml = BaseParser::referenceToHtml($node, $renderEmbed);
27+
break;
28+
default:
29+
$innerHtml = "";
30+
if (isset($node->children))
31+
{
32+
$innerHtml = BaseParser::nodeChildrenToHtml($node->children, $option, $renderEmbed);
33+
}
34+
$resultHtml = $option->renderNode(
35+
$node->type,
36+
$node,
37+
$innerHtml
38+
);
39+
break;
40+
}
41+
} else {
42+
$resultHtml = BaseParser::textToHtml($node, $option);
43+
}
44+
return $resultHtml;
45+
}
46+
47+
protected static function textToHtml(object $node, Option $option)
48+
{
49+
$text = $node->text;
50+
if (isset($node->superscript) && $node->superscript) {
51+
$text = $option->renderMark(MarkType::get(MarkType::SUPERSCRIPT), $text);
52+
}
53+
if (isset($node->subscript) && $node->subscript) {
54+
$text = $option->renderMark(MarkType::get(MarkType::SUBSCRIPT), $text);
55+
}
56+
if (isset($node->inlineCode) && $node->inlineCode) {
57+
$text = $option->renderMark(MarkType::get(MarkType::INLINE_CODE), $text);
58+
}
59+
if (isset($node->strikethrough) && $node->strikethrough) {
60+
$text = $option->renderMark(MarkType::get(MarkType::STRIKE_THROUGH), $text);
61+
}
62+
if (isset($node->underline) && $node->underline) {
63+
$text = $option->renderMark(MarkType::get(MarkType::UNDERLINE), $text);
64+
}
65+
if (isset($node->italic) && $node->italic) {
66+
$text = $option->renderMark(MarkType::get(MarkType::ITALIC), $text);
67+
}
68+
if (isset($node->bold) && $node->bold) {
69+
$text = $option->renderMark(MarkType::get(MarkType::BOLD), $text);
70+
}
71+
return $text;
72+
}
73+
74+
protected static function referenceToHtml(object $node, $renderEmbed)
75+
{
76+
$metadata = new Metadata($node);
77+
return $renderEmbed($metadata);
78+
}
79+
80+
protected static function findEmbeddedObject(\DOMDocument $doc): array {
81+
$xpath = new \DOMXPath($doc);
82+
$elements = $xpath->query('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]');
83+
$metadataArray = array();
84+
foreach ($elements as $node) {
85+
$metadataArray[] = new Metadata($node);
86+
}
87+
return $metadataArray;
88+
}
89+
90+
static function innerHTML(\DOMElement $element)
91+
{
92+
$doc = $element->ownerDocument;
93+
$html = '';
94+
foreach ($element->childNodes as $node) {
95+
$html .= $doc->saveHTML($node);
96+
}
97+
return $html;
98+
}
99+
}

src/GQL.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contentstack\Utils;
6+
7+
use Contentstack\Utils\Model\Option;
8+
use Contentstack\Utils\Model\Metadata;
9+
use Contentstack\Utils\Enum\NodeType;
10+
use Contentstack\Utils\Enum\MarkType;
11+
12+
class GQL extends BaseParser
13+
{
14+
public static function jsonToHtml(object $content, Option $option): array|string {
15+
$result = array();
16+
$embeddedItems = $content->embedded_itemsConnection != null ? $content->embedded_itemsConnection->edges : [];
17+
18+
if (isset($content->json) && isset($content->json->children)) {
19+
return GQL::nodeChildrenToHtml($content->json->children, $option, function (Metadata $metadata) use ($option, $embeddedItems): string {
20+
$resultHtml = '';
21+
$object = GQL::findObject($metadata, $embeddedItems);
22+
if (count($object) > 0) {
23+
$resultHtml = $option->renderOptions($object, $metadata);
24+
}
25+
return $resultHtml;
26+
});
27+
} else if (is_array($content->json)) {
28+
foreach ($content->json as $node) {
29+
$result[] = GQL::nodeChildrenToHtml($node->children, $option, function (Metadata $metadata) use ($option, $embeddedItems): string {
30+
$resultHtml = '';
31+
$object = GQL::findObject($metadata, $embeddedItems);
32+
if (count($object) > 0) {
33+
$resultHtml = $option->renderOptions($object, $metadata);
34+
}
35+
return $resultHtml;
36+
});
37+
}
38+
}
39+
return $result;
40+
}
41+
protected static function findObject(Metadata $metadata, array $embeddedItems): array
42+
{
43+
44+
foreach ($embeddedItems as $entry)
45+
{
46+
if ($entry->node)
47+
{
48+
$item = $entry->node;
49+
if ($item->system && $item->system->uid && $item->system->uid == $metadata->getItemUid())
50+
{
51+
return json_decode(json_encode($item), true);
52+
}
53+
}
54+
}
55+
return [];
56+
}
57+
}

src/Model/Option.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ function renderNode(string $nodeType, object $node, string $innerHtml): string
136136
return $resultString;
137137
}
138138

139-
function renderOptions(array $embeddedObject, Metadata $metadata): string
139+
function renderOptions(array $embeddedObject, Metadata $metadata): string
140140
{
141141
$resultString = "";
142142
switch ($metadata->getStyleType()) {
143143
case StyleType::get(StyleType::BLOCK):
144-
$resultString = "<div><p>" . ($embeddedObject["title"] ?? $embeddedObject["uid"]) . "</p><p>Content type: <span>". $embeddedObject["_content_type_uid"] ."</span></p></div>";
144+
$resultString = "<div><p>" . ($embeddedObject["title"] ?? $embeddedObject["uid"]) . "</p><p>Content type: <span>". ($embeddedObject["_content_type_uid"] ?? $embeddedObject["system"]["content_type_uid"]) ."</span></p></div>";
145145
break;
146146
case StyleType::get(StyleType::INLINE):
147147
$resultString = "<span>".($embeddedObject["title"] ?? $embeddedObject["uid"])."</span>";

src/Utils.php

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Contentstack\Utils\Enum\NodeType;
1010
use Contentstack\Utils\Enum\MarkType;
1111

12-
class Utils
12+
class Utils extends BaseParser
1313
{
1414
/**
1515
*
@@ -70,94 +70,21 @@ public static function jsonArrayToHtml(array $contents, Option $option): array {
7070
public static function jsonToHtml(object $content, Option $option): string {
7171
$resultHtml = '';
7272
if (isset($content->children)) {
73-
$resultHtml = Utils::nodeChildrenToHtml($content->children, $option);
74-
}
75-
return $resultHtml;
76-
}
77-
78-
private static function nodeChildrenToHtml(array $nodes, Option $option): string {
79-
return \implode('', \array_map(function (object $node) use ($option): string {
80-
return Utils::nodeToHtml($node, $option);
81-
}, $nodes));
82-
}
83-
84-
private static function nodeToHtml(object $node, Option $option): string {
85-
$resultHtml = '';
86-
if (isset($node->type)) {
87-
switch ($node->type) {
88-
case NodeType::get(NodeType::REFERENCE)->getValue():
89-
$resultHtml = Utils::referenceToHtml($node, $option);
90-
break;
91-
default:
92-
$innerHtml = "";
93-
if (isset($node->children))
94-
{
95-
$innerHtml = Utils::nodeChildrenToHtml($node->children, $option);
73+
$resultHtml = Utils::nodeChildrenToHtml($content->children, $option, function (Metadata $metadata) use ($option): string {
74+
$resultHtml = '';
75+
if ($option->entry) {
76+
$object = Utils::findObject($metadata, $option->entry);
77+
if (count($object) > 0) {
78+
$resultHtml = $option->renderOptions($object, $metadata);
9679
}
97-
$resultHtml = $option->renderNode(
98-
$node->type,
99-
$node,
100-
$innerHtml
101-
);
102-
break;
103-
}
104-
} else {
105-
$resultHtml = Utils::textToHtml($node, $option);
106-
}
107-
return $resultHtml;
108-
}
109-
110-
private static function textToHtml(object $node, Option $option)
111-
{
112-
$text = $node->text;
113-
if (isset($node->superscript) && $node->superscript) {
114-
$text = $option->renderMark(MarkType::get(MarkType::SUPERSCRIPT), $text);
115-
}
116-
if (isset($node->subscript) && $node->subscript) {
117-
$text = $option->renderMark(MarkType::get(MarkType::SUBSCRIPT), $text);
118-
}
119-
if (isset($node->inlineCode) && $node->inlineCode) {
120-
$text = $option->renderMark(MarkType::get(MarkType::INLINE_CODE), $text);
121-
}
122-
if (isset($node->strikethrough) && $node->strikethrough) {
123-
$text = $option->renderMark(MarkType::get(MarkType::STRIKE_THROUGH), $text);
124-
}
125-
if (isset($node->underline) && $node->underline) {
126-
$text = $option->renderMark(MarkType::get(MarkType::UNDERLINE), $text);
127-
}
128-
if (isset($node->italic) && $node->italic) {
129-
$text = $option->renderMark(MarkType::get(MarkType::ITALIC), $text);
130-
}
131-
if (isset($node->bold) && $node->bold) {
132-
$text = $option->renderMark(MarkType::get(MarkType::BOLD), $text);
133-
}
134-
return $text;
135-
}
136-
137-
private static function referenceToHtml(object $node, Option $option)
138-
{
139-
$resultHtml = '';
140-
if ($option->entry) {
141-
$metadata = new Metadata($node);
142-
$object = Utils::findObject($metadata, $option->entry);
143-
if (count($object) > 0) {
144-
$resultHtml = $option->renderOptions($object, $metadata);
145-
}
80+
}
81+
return $resultHtml;
82+
});
14683
}
14784
return $resultHtml;
14885
}
14986

150-
private static function findEmbeddedObject(\DOMDocument $doc): array {
151-
$xpath = new \DOMXPath($doc);
152-
$elements = $xpath->query('//*[contains(@class, "embedded-asset") or contains(@class, "embedded-entry")]');
153-
$metadataArray = array();
154-
foreach ($elements as $node) {
155-
$metadataArray[] = new Metadata($node);
156-
}
157-
return $metadataArray;
158-
}
159-
160-
private static function findObject(Metadata $metadata, array $entry): array
87+
protected static function findObject(Metadata $metadata, array $entry): array
16188
{
16289
if (array_key_exists('_embedded_items', $entry))
16390
{
@@ -174,14 +101,4 @@ private static function findObject(Metadata $metadata, array $entry): array
174101
}
175102
return [];
176103
}
177-
178-
static function innerHTML(\DOMElement $element)
179-
{
180-
$doc = $element->ownerDocument;
181-
$html = '';
182-
foreach ($element->childNodes as $node) {
183-
$html .= $doc->saveHTML($node);
184-
}
185-
return $html;
186-
}
187104
}

0 commit comments

Comments
 (0)