Skip to content

Commit bc7d794

Browse files
authored
Merge pull request #4 from contentstack/feature/supercharged-rte
Feature/supercharged rte
2 parents f144e8e + a6b1fe5 commit bc7d794

4 files changed

Lines changed: 64 additions & 23 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ require 'contentstack'
8686
Contentstack.render_content(@entry.rte_field_uid, ContentstackUtils::Model::Option.new(@entry))
8787
end
8888
```
89+
### GQL Json RTE to HTML
90+
To parse JSON RTE content from GQL response to HTML content use `ContentstackUtils::GQL.json_to_html` function as below:
91+
92+
```ruby
93+
require 'contentstack_utils'
94+
95+
result = ContentstackUtils::GQL.json_to_html(entry['single_rte'], ContentstackUtils::Model::Options.new())
96+
97+
```

lib/contentstack_utils/model/options.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def render_option(embeddedObject, metadata)
1717
renderString = ''
1818
case metadata.style_type
1919
when 'block'
20-
renderString = "<div><p>#{embeddedObject['title'] || embeddedObject['uid']}</p><p>Content type: <span>#{embeddedObject['_content_type_uid']}</span></p></div>"
20+
renderString = "<div><p>#{embeddedObject['title'] || embeddedObject['uid']}</p><p>Content type: <span>#{embeddedObject['_content_type_uid'] || embeddedObject['system']['content_type_uid']}</span></p></div>"
2121
when 'inline'
2222
renderString = "<span>#{embeddedObject["title"] || embeddedObject["uid"]}</span>";
2323
when 'link'

lib/contentstack_utils/utils.rb

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,49 @@ def self.render_content(content, options)
1717
end
1818

1919
def self.json_to_html(content, options)
20+
reference = -> (metadata){
21+
result = ""
22+
if options.entry != nil
23+
object = findObject(metadata, options.entry)
24+
if object!= nil && object.length() > 0
25+
result = options.render_option(object[0], metadata)
26+
end
27+
end
28+
result
29+
}
2030
if (content.instance_of? Array)
2131
result = []
2232
content.each do |n|
23-
result.push(json_doc_to_html(n, options))
33+
result.push(json_doc_to_html(n, options, reference) )
2434
end
2535
result
2636
elsif content.instance_of? Hash
27-
json_doc_to_html(content, options)
37+
json_doc_to_html(content, options, reference)
2838
end
2939
end
3040

31-
private_class_method def self.json_doc_to_html(node, options)
41+
def self.json_doc_to_html(node, options, callback)
3242
result = ""
3343
if node["children"] && node["children"].length() > 0
34-
result = node_children_to_html(node["children"], options)
44+
result = node_children_to_html(node["children"], options, callback)
3545
end
3646
result
3747
end
3848

39-
private_class_method def self.node_children_to_html(nodes, options)
40-
nodes.map {|node| node_to_html(node, options)}.join("")
49+
private_class_method def self.node_children_to_html(nodes, options, callback)
50+
nodes.map {|node| node_to_html(node, options, callback)}.join("")
4151
end
4252

43-
private_class_method def self.node_to_html(node, options)
53+
private_class_method def self.node_to_html(node, options, callback)
4454
html_result = ""
4555
if node["type"] == nil && node["text"]
4656
html_result = text_to_htms(node, options)
4757
elsif node["type"]
4858
if node["type"] == "reference"
49-
html_result = reference_to_html(node, options)
59+
metadata = Model::Metadata.new(node)
60+
html_result = callback.call(metadata)
5061
else
51-
inner_html = json_doc_to_html(node, options)
62+
inner_html = json_doc_to_html(node, options, callback)
5263
html_result = options.render_node(node["type"], node, inner_html)
5364
end
5465
end
@@ -81,18 +92,6 @@ def self.json_to_html(content, options)
8192
text
8293
end
8394

84-
private_class_method def self.reference_to_html(node, options)
85-
result = ""
86-
if options.entry != nil
87-
metadata = Model::Metadata.new(node)
88-
object = findObject(metadata, options.entry)
89-
if object!= nil && object.length() > 0
90-
result = options.render_option(object[0], metadata)
91-
end
92-
end
93-
result
94-
end
95-
9695
private_class_method def self.render_string(string, options)
9796
xml_doc = Nokogiri::HTML(appendFrame(string))
9897
result = xml_doc.xpath('//documentfragmentcontainer').inner_html
@@ -128,4 +127,37 @@ def self.json_to_html(content, options)
128127
end
129128
return nil
130129
end
130+
131+
module GQL
132+
include ContentstackUtils
133+
def self.json_to_html(content, options)
134+
embeddedItems = []
135+
if content.has_key? 'embedded_itemsConnection'
136+
embeddedItems = content['embedded_itemsConnection']['edges'] || []
137+
end
138+
reference = -> (metadata){
139+
result = ""
140+
if embeddedItems != nil
141+
object = embeddedItems.select { |embedObject| embedObject['node']["system"]["uid"] == metadata.item_uid }
142+
if object != nil && object.length() > 0
143+
result = options.render_option(object[0]["node"], metadata)
144+
end
145+
end
146+
result
147+
}
148+
149+
if content.has_key? 'json'
150+
json = content['json']
151+
if (json.instance_of? Array)
152+
result = []
153+
json.each do |n|
154+
result.push(ContentstackUtils.json_doc_to_html(n, options, reference))
155+
end
156+
result
157+
elsif json.instance_of? Hash
158+
ContentstackUtils.json_doc_to_html(json, options, reference)
159+
end
160+
end
161+
end
162+
end
131163
end

lib/contentstack_utils/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ContentstackUtils
2-
VERSION = "1.0.2"
2+
VERSION = "1.1.0"
33
end

0 commit comments

Comments
 (0)