@@ -156,6 +156,31 @@ const createNumberingRegistry = (): NumberingRegistry => {
156156 } ;
157157} ;
158158
159+ const composeBuilders = (
160+ pluginsBuilders : readonly NodeBuilders [ ] ,
161+ defaultBuilders : NodeBuilders ,
162+ ) : NodeBuilders => {
163+ return pluginsBuilders . reduceRight < NodeBuilders > ( ( acc , p ) => {
164+ type Key = keyof typeof p ;
165+ for ( const k of Object . keys ( p ) ) {
166+ const cur = p [ k as Key ] ! ;
167+ const prev = acc [ k as Key ] ;
168+ acc [ k as Key ] = (
169+ prev
170+ ? ( n , c ) => {
171+ const r = cur ( n as any , c ) ;
172+ if ( r ) {
173+ return r ;
174+ }
175+ return prev ( n as any , c ) ;
176+ }
177+ : cur
178+ ) as NodeBuilder < any > ;
179+ }
180+ return acc ;
181+ } , defaultBuilders ) ;
182+ } ;
183+
159184export interface DocxOptions extends Pick <
160185 IPropertiesOptions ,
161186 | "title"
@@ -192,16 +217,8 @@ export const mdastToDocx = async (
192217
193218 const pluginCtx = { root : node , definition } ;
194219
195- const builders = (
196- await Promise . all ( plugins . map ( ( p ) => p ( pluginCtx ) ) )
197- ) . reduceRight < NodeBuilders > (
198- ( acc , p ) => {
199- type Key = keyof typeof p ;
200- for ( const k of Object . keys ( p ) ) {
201- acc [ k as Key ] = p [ k as Key ] as any ;
202- }
203- return acc ;
204- } ,
220+ const builders = composeBuilders (
221+ await Promise . all ( plugins . map ( ( p ) => p ( pluginCtx ) ) ) ,
205222 {
206223 paragraph : buildParagraph ,
207224 heading : buildHeading ,
0 commit comments