@@ -5,13 +5,13 @@ import { DocxExporter, exportSchemaToJson } from './exporter';
55import { createDocumentJson , addDefaultStylesIfMissing } from './v2/importer/docxImporter.js' ;
66import { deobfuscateFont , getArrayBufferFromUrl } from './helpers.js' ;
77import { baseNumbering } from './v2/exporter/helpers/base-list.definitions.js' ;
8- import { DEFAULT_CUSTOM_XML , SETTINGS_CUSTOM_XML } from './exporter-docx-defs.js' ;
8+ import { DEFAULT_CUSTOM_XML , DEFAULT_DOCX_DEFS , SETTINGS_CUSTOM_XML } from './exporter-docx-defs.js' ;
99import {
1010 getCommentDefinition ,
1111 prepareCommentParaIds ,
1212 prepareCommentsXmlFilesForExport ,
1313} from './v2/exporter/commentsExporter.js' ;
14- import { HYPERLINK_RELATIONSHIP_TYPE } from './constants.js' ;
14+ import { FOOTER_RELATIONSHIP_TYPE , HEADER_RELATIONSHIP_TYPE , HYPERLINK_RELATIONSHIP_TYPE } from './constants.js' ;
1515
1616class SuperConverter {
1717 static allowedElements = Object . freeze ( {
@@ -420,11 +420,11 @@ class SuperConverter {
420420 }
421421
422422 this . convertedXml = { ...this . convertedXml , ...updatedXml } ;
423+
424+ const headFootRels = this . #exportProcessHeadersFooters( { isFinalDoc } ) ;
423425
424426 // Update the rels table
425- this . #exportProcessNewRelationships( [ ...params . relationships , ...commentsRels ] ) ;
426-
427- this . #exportProcessHeadersFooters( { isFinalDoc } ) ;
427+ this . #exportProcessNewRelationships( [ ...params . relationships , ...commentsRels , ...headFootRels ] ) ;
428428
429429 // Store the SuperDoc version
430430 storeSuperdocVersion ( this . convertedXml ) ;
@@ -502,9 +502,10 @@ class SuperConverter {
502502 #exportProcessHeadersFooters( { isFinalDoc = false } ) {
503503 const relsData = this . convertedXml [ 'word/_rels/document.xml.rels' ] ;
504504 const relationships = relsData . elements . find ( ( x ) => x . name === 'Relationships' ) ;
505+ const newDocRels = [ ] ;
505506
506- Object . entries ( this . headers ) . forEach ( ( [ id , header ] ) => {
507- const fileName = relationships . elements . find ( ( el ) => el . attributes . Id === id ) ?. attributes . Target ;
507+ Object . entries ( this . headers ) . forEach ( ( [ id , header ] , index ) => {
508+ const fileName = relationships . elements . find ( ( el ) => el . attributes . Id === id ) ?. attributes . Target || `header ${ index + 1 } .xml` ;
508509 const headerEditor = this . headerEditors . find ( ( item ) => item . id === id ) ;
509510
510511 if ( ! headerEditor ) return ;
@@ -521,7 +522,29 @@ class SuperConverter {
521522
522523 const bodyContent = result . elements [ 0 ] . elements ;
523524 const file = this . convertedXml [ `word/${ fileName } ` ] ;
524- file . elements [ 0 ] . elements = bodyContent ;
525+
526+ if ( ! file ) {
527+ this . convertedXml [ `word/${ fileName } ` ] = {
528+ declaration : this . initialJSON ?. declaration ,
529+ elements : [ {
530+ attributes : DEFAULT_DOCX_DEFS ,
531+ name : 'w:hdr' ,
532+ type : 'element' ,
533+ elements : [ ]
534+ } ]
535+ } ;
536+ newDocRels . push ( {
537+ type : 'element' ,
538+ name : 'Relationship' ,
539+ attributes : {
540+ Id : id ,
541+ Type : 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/header' ,
542+ Target : fileName ,
543+ } ,
544+ } ) ;
545+ }
546+
547+ this . convertedXml [ `word/${ fileName } ` ] . elements [ 0 ] . elements = bodyContent ;
525548
526549 if ( params . relationships . length ) {
527550 const relationships = this . convertedXml [ `word/_rels/${ fileName } .rels` ] ?. elements ?. find ( ( x ) => x . name === 'Relationships' ) ?. elements || [ ] ;
@@ -541,8 +564,8 @@ class SuperConverter {
541564 }
542565 } ) ;
543566
544- Object . entries ( this . footers ) . forEach ( ( [ id , footer ] ) => {
545- const fileName = relationships . elements . find ( ( el ) => el . attributes . Id === id ) ?. attributes . Target ;
567+ Object . entries ( this . footers ) . forEach ( ( [ id , footer ] , index ) => {
568+ const fileName = relationships . elements . find ( ( el ) => el . attributes . Id === id ) ?. attributes . Target || `footer ${ index + 1 } .xml` ;
546569 const footerEditor = this . footerEditors . find ( ( item ) => item . id === id ) ;
547570
548571 if ( ! footerEditor ) return ;
@@ -559,7 +582,29 @@ class SuperConverter {
559582
560583 const bodyContent = result . elements [ 0 ] . elements ;
561584 const file = this . convertedXml [ `word/${ fileName } ` ] ;
562- file . elements [ 0 ] . elements = bodyContent ;
585+
586+ if ( ! file ) {
587+ this . convertedXml [ `word/${ fileName } ` ] = {
588+ declaration : this . initialJSON ?. declaration ,
589+ elements : [ {
590+ attributes : DEFAULT_DOCX_DEFS ,
591+ name : 'w:ftr' ,
592+ type : 'element' ,
593+ elements : [ ]
594+ } ]
595+ } ;
596+ newDocRels . push ( {
597+ type : 'element' ,
598+ name : 'Relationship' ,
599+ attributes : {
600+ Id : id ,
601+ Type : 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer' ,
602+ Target : fileName ,
603+ } ,
604+ } ) ;
605+ }
606+
607+ this . convertedXml [ `word/${ fileName } ` ] . elements [ 0 ] . elements = bodyContent ;
563608
564609 if ( params . relationships . length ) {
565610 const relationships = this . convertedXml [ `word/_rels/${ fileName } .rels` ] ?. elements ?. find ( ( x ) => x . name === 'Relationships' ) ?. elements || [ ] ;
@@ -578,6 +623,8 @@ class SuperConverter {
578623 } ;
579624 }
580625 } ) ;
626+
627+ return newDocRels ;
581628 }
582629
583630 #exportProcessNewRelationships( rels = [ ] ) {
@@ -593,8 +640,9 @@ class SuperConverter {
593640 const existingTarget = relationships . elements . find ( ( el ) => el . attributes . Target === rel . attributes . Target ) ;
594641 const isNewMedia = rel . attributes . Target ?. startsWith ( 'media/' ) && existingId . length > 6 ;
595642 const isNewHyperlink = rel . attributes . Type === HYPERLINK_RELATIONSHIP_TYPE && existingId . length > 6 ;
596-
597- if ( existingTarget && ! isNewMedia && ! isNewHyperlink ) {
643+ const isNewHeadFoot = rel . attributes . Type === ( HEADER_RELATIONSHIP_TYPE || rel . attributes . Type === FOOTER_RELATIONSHIP_TYPE ) && existingId . length > 6 ;
644+
645+ if ( existingTarget && ! isNewMedia && ! isNewHyperlink && ! isNewHeadFoot ) {
598646 return ;
599647 }
600648
0 commit comments