@@ -22,7 +22,8 @@ public function getFunctions(): array
2222 return [
2323 new TwigFunction ('getGridClass ' , [$ this , 'getGridClass ' ]),
2424 new TwigFunction ('generateHtmlBasedOnProperty ' , [$ this , 'generateHtmlBasedOnProperty ' ], ['is_safe ' => ['html ' ]]),
25- new TwigFunction ('htmlAttr ' , [$ this , 'htmlAttr ' ], ['is_safe ' => ['html ' ]])
25+ new TwigFunction ('htmlAttr ' , [$ this , 'htmlAttr ' ], ['is_safe ' => ['html ' ]]),
26+ new TwigFunction ('renderProseMirrorContent ' , [$ this , 'renderProseMirrorContent ' ], ['is_safe ' => ['html ' ]])
2627 ];
2728 }
2829
@@ -75,4 +76,88 @@ public function getContainersData(array $containers, array $containerRef): array
7576
7677 return $ containerData ;
7778 }
79+
80+ public function renderProseMirrorContent ($ content ): string
81+ {
82+ if (empty ($ content ) || !isset ($ content ['content ' ])) {
83+ return '' ;
84+ }
85+
86+ $ html = '' ;
87+ foreach ($ content ['content ' ] as $ block ) {
88+ $ html .= $ this ->renderProseMirrorBlock ($ block );
89+ }
90+
91+ return $ html ;
92+ }
93+
94+ private function renderProseMirrorBlock (array $ block ): string
95+ {
96+ $ type = $ block ['type ' ] ?? '' ;
97+ $ template = 'dotcms/prosemirror/ ' . $ type . '.twig ' ;
98+
99+ if ($ this ->twig ->getLoader ()->exists ($ template )) {
100+ return $ this ->twig ->render ($ template , ['block ' => $ block ]);
101+ }
102+
103+ // Fallback rendering for unknown block types
104+ return $ this ->renderFallbackBlock ($ block );
105+ }
106+
107+ private function renderFallbackBlock (array $ block ): string
108+ {
109+ $ type = $ block ['type ' ] ?? 'unknown ' ;
110+
111+ switch ($ type ) {
112+ case 'paragraph ' :
113+ return $ this ->renderParagraph ($ block );
114+ case 'bulletList ' :
115+ return $ this ->renderBulletList ($ block );
116+ case 'listItem ' :
117+ return $ this ->renderListItem ($ block );
118+ default :
119+ return '' ;
120+ }
121+ }
122+
123+ private function renderParagraph (array $ block ): string
124+ {
125+ $ content = $ this ->extractTextContent ($ block );
126+ return $ content ? "<p> {$ content }</p> " : '' ;
127+ }
128+
129+ private function renderBulletList (array $ block ): string
130+ {
131+ $ items = '' ;
132+ if (isset ($ block ['content ' ])) {
133+ foreach ($ block ['content ' ] as $ item ) {
134+ $ items .= $ this ->renderProseMirrorBlock ($ item );
135+ }
136+ }
137+ return $ items ? "<ul> {$ items }</ul> " : '' ;
138+ }
139+
140+ private function renderListItem (array $ block ): string
141+ {
142+ $ content = '' ;
143+ if (isset ($ block ['content ' ])) {
144+ foreach ($ block ['content ' ] as $ item ) {
145+ $ content .= $ this ->renderProseMirrorBlock ($ item );
146+ }
147+ }
148+ return $ content ? "<li> {$ content }</li> " : '' ;
149+ }
150+
151+ private function extractTextContent (array $ block ): string
152+ {
153+ $ text = '' ;
154+ if (isset ($ block ['content ' ])) {
155+ foreach ($ block ['content ' ] as $ item ) {
156+ if (isset ($ item ['text ' ])) {
157+ $ text .= htmlspecialchars ($ item ['text ' ]);
158+ }
159+ }
160+ }
161+ return $ text ;
162+ }
78163}
0 commit comments