@@ -31,16 +31,17 @@ class FileStore {
3131 }
3232}
3333
34+ const allframes = / < d r a w : f r a m e ( .* ?) < \/ d r a w : f r a m e > / g;
35+
3436class OdtPostProcessor {
35- allframes = / < d r a w : f r a m e ( .* ?) < \/ d r a w : f r a m e > / g;
3637
3738 constructor ( template , data , options , filestore ) {
3839 const contentXml = template . files . find ( ( f ) => f . name === "content.xml" ) ;
3940 if ( ! contentXml ) return ;
4041 const manifestXml = template . files . find ( f => f . name === "META-INF/manifest.xml" ) ;
4142
4243 // Use base64 data to create new file and update references
43- contentXml . data = contentXml . data . replaceAll ( this . allframes , function ( drawFrame ) {
44+ contentXml . data = contentXml . data . replaceAll ( allframes , function ( drawFrame ) {
4445 const [ , , mime , content ] = / < s v g : t i t l e > ( d a t a : ( [ ^ ; ] + ) ; b a s e 6 4 , ( .* ?) ) < \/ s v g : t i t l e > / . exec ( drawFrame ) || [ ] ;
4546 if ( ! content || ! mime ) return drawFrame ;
4647 const [ , extension ] = mime . split ( "/" ) ;
@@ -63,17 +64,19 @@ class OdtPostProcessor {
6364 }
6465}
6566
67+ const alldrawings = / < w : d r a w i n g > ( .* ?) < \/ w : d r a w i n g > / g;
68+ const allrels = / < R e l a t i o n s h i p ( .* ?) \/ > / g;
69+
6670class DocxPostProcessor {
67- alldrawings = / < w : d r a w i n g > ( .* ?) < \/ w : d r a w i n g > / g;
6871 pattern = / ( < w : d r a w i n g > .* < w p : d o c P r .* t i t l e = ) ( " d a t a : i m a g e \/ ( .* ) ; b a s e 6 4 , ( .+ ?) " ) ( .* : e m b e d = " ) ( .* ?) ( " .* < \/ w : d r a w i n g > ) / g;
6972
7073 constructor ( template , data , options , filestore ) {
7174 const documentXmlFile = template . files . find ( f => f . name === "word/document.xml" ) ;
7275 if ( ! documentXmlFile ) return ;
7376 const documentXmlRelsFile = template . files . find ( f => f . name === "word/_rels/document.xml.rels" ) ;
7477
75- documentXmlFile . data = documentXmlFile . data . replaceAll ( this . alldrawings , function ( drawing ) {
76- const [ , , mime , content ] = / t i t l e = " ( d a t a : ( [ ^ ; ] + ) ; b a s e 6 4 , ( .* ?) ) " / . exec ( drawing ) || [ ] ;
78+ documentXmlFile . data = documentXmlFile . data . replaceAll ( alldrawings , function ( drawing ) {
79+ const [ , , , mime , content ] = / ( t i t l e | d e s c r ) = " ( d a t a : ( [ ^ ; ] + ) ; b a s e 6 4 , ( .* ?) ) " / . exec ( drawing ) || [ ] ;
7780 const [ , relationshipId ] = / e m b e d = " ( .* ?) " / . exec ( drawing ) || [ ] ;
7881 if ( ! content || ! mime || ! relationshipId ) return drawing ;
7982 const [ , extension ] = mime . split ( "/" ) ;
@@ -83,12 +86,13 @@ class DocxPostProcessor {
8386 if ( newfile ) {
8487 template . files . push ( { name : imgFile , isMarked : false , data : Buffer . from ( content , "base64" ) , parent : "" } ) ;
8588 // Update corresponding entry in word/_rels/document.xml.rels file
86- const regex = new RegExp ( `(.*Target=")(.*)(".*Id="${ relationshipId } ".*\/>.*<\/Relationships>)` , "g" ) ;
87- documentXmlRelsFile . data = documentXmlRelsFile . data . replaceAll ( regex , function ( _match , p1 , _p2 , p3 ) {
88- return [ p1 , `/${ imgFile } ` , p3 ] . join ( "" )
89+ documentXmlRelsFile . data = documentXmlRelsFile . data . replaceAll ( allrels , function ( relationship ) {
90+ const [ , id ] = / I d = " ( .* ?) " / . exec ( relationship ) || [ ] ;
91+ if ( id != relationshipId ) return relationship ;
92+ return relationship . replace ( / T a r g e t = " .* ?" / g, `Target="/${ imgFile } "` ) ;
8993 } ) ;
9094 }
91- return drawing . replace ( / t i t l e = " d a t a : [ ^ " ] + " / , 'title =""' ) ;
95+ return drawing . replace ( / ( t i t l e | d e s c r ) = " d a t a : [ ^ ; ] + ; b a s e 6 4 , . * ? " / g , '$1 =""' ) ;
9296 } ) ;
9397 }
9498}
0 commit comments