@@ -148,65 +148,69 @@ export function aem2doc(html, ydoc) {
148148 html = EMPTY_DOC ;
149149 }
150150 const tree = fromHtml ( html , { fragment : true } ) ;
151- const main = tree . children . find ( ( child ) => child . tagName === 'main' ) || { children : [ ] } ;
152- fixImageLinks ( main ) ;
153- removeComments ( main ) ;
154- ( main . children || [ ] ) . forEach ( ( parent ) => {
155- if ( parent . tagName === 'div' && parent . children ) {
156- const children = [ ] ;
157- let modified = false ;
158- parent . children . forEach ( ( child ) => {
159- if ( child . tagName === 'div' && child . properties . className ?. length > 0 ) {
160- modified = true ;
161- blockToTable ( child , children ) ;
162- } else if ( child . tagName === 'da-loc-deleted' || child . tagName === 'da-loc-added' ) {
163- modified = true ;
164- const locChildren = [ ] ;
165- child . children . forEach ( ( locChild ) => {
166- if ( locChild . tagName === 'div' && locChild . properties . className ?. length > 0 ) {
167- blockToTable ( locChild , locChildren ) ;
168- } else {
169- locChildren . push ( locChild ) ;
170- }
171- } ) ;
151+ const main = tree . children . find ( ( child ) => child . tagName === 'main' ) ;
152+ console . log ( 'tree' , tree ) ;
153+ console . log ( 'main' , main ) ;
154+ if ( main ) {
155+ fixImageLinks ( main ) ;
156+ removeComments ( main ) ;
157+ ( main . children || [ ] ) . forEach ( ( parent ) => {
158+ if ( parent . tagName === 'div' && parent . children ) {
159+ const children = [ ] ;
160+ let modified = false ;
161+ parent . children . forEach ( ( child ) => {
162+ if ( child . tagName === 'div' && child . properties . className ?. length > 0 ) {
163+ modified = true ;
164+ blockToTable ( child , children ) ;
165+ } else if ( child . tagName === 'da-loc-deleted' || child . tagName === 'da-loc-added' ) {
166+ modified = true ;
167+ const locChildren = [ ] ;
168+ child . children . forEach ( ( locChild ) => {
169+ if ( locChild . tagName === 'div' && locChild . properties . className ?. length > 0 ) {
170+ blockToTable ( locChild , locChildren ) ;
171+ } else {
172+ locChildren . push ( locChild ) ;
173+ }
174+ } ) ;
175+ // eslint-disable-next-line no-param-reassign
176+ child . children = locChildren ;
177+ children . push ( child ) ;
178+ } else {
179+ children . push ( child ) ;
180+ }
181+ } ) ;
182+ if ( modified ) {
172183 // eslint-disable-next-line no-param-reassign
173- child . children = locChildren ;
174- children . push ( child ) ;
175- } else {
176- children . push ( child ) ;
184+ parent . children = children ;
177185 }
178- } ) ;
179- if ( modified ) {
180- // eslint-disable-next-line no-param-reassign
181- parent . children = children ;
182186 }
183- }
184- } ) ;
185- convertSectionBreak ( main ) ;
186- let count = 0 ;
187- main . children = main . children . flatMap ( ( node ) => {
188- const result = [ ] ;
189- if ( node . tagName === 'div' ) {
190- if ( count > 0 ) {
191- result . push ( {
192- type : 'element' , tagName : 'p' , children : [ ] , properties : { } ,
193- } ) ;
194- result . push ( {
195- type : 'element' , tagName : 'hr' , children : [ ] , properties : { } ,
196- } ) ;
197- result . push ( {
198- type : 'element' , tagName : 'p' , children : [ ] , properties : { } ,
199- } ) ;
200- result . push ( ...node . children ) ;
187+ } ) ;
188+ convertSectionBreak ( main ) ;
189+ let count = 0 ;
190+ main . children = main . children . flatMap ( ( node ) => {
191+ const result = [ ] ;
192+ if ( node . tagName === 'div' ) {
193+ if ( count > 0 ) {
194+ result . push ( {
195+ type : 'element' , tagName : 'p' , children : [ ] , properties : { } ,
196+ } ) ;
197+ result . push ( {
198+ type : 'element' , tagName : 'hr' , children : [ ] , properties : { } ,
199+ } ) ;
200+ result . push ( {
201+ type : 'element' , tagName : 'p' , children : [ ] , properties : { } ,
202+ } ) ;
203+ result . push ( ...node . children ) ;
204+ } else {
205+ result . push ( node ) ;
206+ }
207+ count += 1 ;
201208 } else {
202209 result . push ( node ) ;
203210 }
204- count += 1 ;
205- } else {
206- result . push ( node ) ;
207- }
208- return result ;
209- } ) ;
211+ return result ;
212+ } ) ;
213+ }
210214 const handler2 = {
211215 get ( target , prop ) {
212216 const source = target ;
@@ -277,7 +281,7 @@ export function aem2doc(html, ydoc) {
277281 } ,
278282 } ;
279283
280- const json = DOMParser . fromSchema ( getSchema ( ) ) . parse ( new Proxy ( main , handler2 ) ) ;
284+ const json = DOMParser . fromSchema ( getSchema ( ) ) . parse ( new Proxy ( main || tree , handler2 ) ) ;
281285 prosemirrorToYXmlFragment ( json , ydoc . getXmlFragment ( 'prosemirror' ) ) ;
282286}
283287
0 commit comments