@@ -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
131163end
0 commit comments