@@ -48,7 +48,8 @@ function convertHTMLtoDDE(html, options = {}) {
4848
4949 try {
5050 const parsed = parse ( html ) ;
51- return nodeToDDE ( parsed . children [ 0 ] , options ) ;
51+ const content = parsed . children [ 0 ] || parsed . childNodes [ 0 ] ;
52+ return ! content ? "" : nodeToDDE ( content , options ) ;
5253 } catch ( error ) {
5354 console . error ( "Parsing error:" , error ) ;
5455 return `// Error parsing HTML: ${ error . message } ` ;
@@ -64,10 +65,14 @@ const NODE_TYPE = {
6465
6566// Convert a parsed node to dd<el> code
6667function nodeToDDE ( node , options = { } , level = 0 ) {
68+ const tab = options . indent === "-1" ? "\t" : " " . repeat ( options . indent ) ;
69+ const indent = tab . repeat ( level ) ;
70+ const nextIndent = tab . repeat ( level + 1 ) ;
71+
6772 const { nodeType } = node ;
6873 // Handle text nodes
6974 if ( nodeType === NODE_TYPE . TEXT ) {
70- const text = node . nodeValue ;
75+ const text = el ( "i" , { innerText : node . nodeValue } ) . textContent ;
7176 if ( ! text . trim ( ) ) return null ;
7277
7378 // Return as plain text or template string for longer text
@@ -78,15 +83,15 @@ function nodeToDDE(node, options = {}, level = 0) {
7883
7984 // Handle comment nodes
8085 if ( nodeType === NODE_TYPE . COMMENT ) {
81- return null ; // TODO: Skip comments?
86+ const text = node . nodeValue ;
87+ if ( ! text . trim ( ) ) return null ;
88+ return text . includes ( "\n" )
89+ ? [ "/*" , ...text . trim ( ) . split ( "\n" ) . map ( l => tab + l ) , "*/" ]
90+ : [ `// ${ text } ` ] ;
8291 }
8392
8493 // For element nodes
8594 if ( nodeType === NODE_TYPE . ELEMENT ) {
86- const tab = options . indent === "-1" ? "\t" : " " . repeat ( options . indent ) ;
87- const indent = tab . repeat ( level ) ;
88- const nextIndent = tab . repeat ( level + 1 ) ;
89-
9095 // Special case for SVG elements
9196 const isNS = node . tagName === "svg" ;
9297 const elFunction = isNS ? "elNS" : "el" ;
@@ -171,12 +176,14 @@ function nodeToDDE(node, options = {}, level = 0) {
171176 // Process children
172177 const children = [ ] ;
173178 for ( const child of node . childNodes ) {
174- const childCode = nodeToDDE ( child , options , level + 1 ) ;
175- if ( childCode ) children . push ( childCode ) ;
179+ const childCode = nodeToDDE ( child , options , level + 1 ) ;
180+ if ( ! childCode ) continue ;
181+
182+ children . push ( childCode ) ;
176183 }
177- if ( children . length === 1 && node . childNodes [ 0 ] . nodeType === NODE_TYPE . TEXT ) {
178- const textContent = children . pop ( ) . slice ( 1 , - 1 ) ;
179- attrs . unshift ( `textContent: "${ textContent } "` ) ;
184+ if ( node . childNodes . length === 1 && node . childNodes [ 0 ] . nodeType === NODE_TYPE . TEXT ) {
185+ const textContent = children . pop ( ) . slice ( 1 , - 1 ) ;
186+ attrs . unshift ( `textContent: "${ textContent } "` ) ;
180187 }
181188
182189 // Build the element creation code
@@ -189,15 +196,16 @@ function nodeToDDE(node, options = {}, level = 0) {
189196 result += `, {\n${ nextIndent } ${ attrs . join ( `,\n${ nextIndent } ` ) } ,\n${ indent } }` ;
190197 else
191198 result += `, { ${ attrs . join ( ", " ) } }` ;
192- } else if ( children . length > 0 ) {
193- result += ", null" ;
194199 }
195200
196201 // Add children if any
197202 if ( children . length > 0 ) {
198- result += `).append(\n${ nextIndent } ${ children . join ( `,\n${ nextIndent } ` ) } ,\n${ indent } )` ;
203+ const chs = children . map ( ch =>
204+ Array . isArray ( ch ) ? ch . map ( l => nextIndent + l ) . join ( "\n" ) :
205+ nextIndent + ch + "," ) ;
206+ result += `).append(\n${ chs . join ( "\n" ) } \n${ indent } )` ;
199207 } else {
200- result += ")" ;
208+ result += ")" ;
201209 }
202210
203211 return result ;
0 commit comments