@@ -241,6 +241,35 @@ export const hasAttribute = (attrs: Record<string, string | number>, name: strin
241
241
return attrs [ name ] !== undefined
242
242
}
243
243
244
+ const contentCache : { [ key : string ] : string } = { }
245
+ type Node = AdocTypes . Block | AdocTypes . AbstractBlock | AdocTypes . Table . Cell
246
+
247
+ const getContent = ( node : Node ) => {
248
+ const cacheKey = ( node as Node & { $$id : string } ) . $$id
249
+
250
+ if ( contentCache [ cacheKey ] ) {
251
+ return contentCache [ cacheKey ]
252
+ }
253
+
254
+ const newContent = ( node . getContent && node . getContent ( ) ) || ''
255
+ contentCache [ cacheKey ] = newContent
256
+ return newContent
257
+ }
258
+
259
+ const getText = (
260
+ node : AdocTypes . ListItem | AdocTypes . Document . Footnote | AdocTypes . Table . Cell ,
261
+ ) => {
262
+ const cacheKey = ( node as Node & { $$id : string } ) . $$id
263
+
264
+ if ( contentCache [ cacheKey ] ) {
265
+ return contentCache [ cacheKey ]
266
+ }
267
+
268
+ const newContent = ( node . getText && node . getText ( ) ) || ''
269
+ contentCache [ cacheKey ] = newContent
270
+ return newContent
271
+ }
272
+
244
273
export const prepareDocument = ( document : AdocTypes . Document ) => {
245
274
let preparedDocument : DocumentBlock
246
275
@@ -256,7 +285,7 @@ export const prepareDocument = (document: AdocTypes.Document) => {
256
285
id : block . getId && block . getId ( ) ,
257
286
type,
258
287
blocks,
259
- content : blocks . length > 0 ? undefined : block . getContent && block . getContent ( ) ,
288
+ content : blocks . length > 0 ? undefined : getContent ( block ) ,
260
289
attributes : block . getAttributes && block . getAttributes ( ) ,
261
290
contentModel,
262
291
lineNumber : block . getLineNumber && block . getLineNumber ( ) ,
@@ -348,7 +377,7 @@ export const prepareDocument = (document: AdocTypes.Document) => {
348
377
if ( type === 'list_item' ) {
349
378
let listItemBlock = processedBlock as ListItemBlock
350
379
const adocListItem = block as unknown as AdocTypes . ListItem
351
- listItemBlock . text = adocListItem . hasText ( ) ? adocListItem . getText ( ) : undefined
380
+ listItemBlock . text = adocListItem . hasText ( ) ? getText ( adocListItem ) : undefined
352
381
}
353
382
354
383
if ( type === 'table' ) {
@@ -401,7 +430,7 @@ export const prepareDocument = (document: AdocTypes.Document) => {
401
430
let tableCellBlock : Cell = {
402
431
...processedBlock ,
403
432
type : 'table_cell' ,
404
- text : adocListItem . getText ( ) ,
433
+ text : getText ( adocListItem ) ,
405
434
columnSpan : adocListItem . getColumnSpan ( ) ,
406
435
rowSpan : adocListItem . getRowSpan ( ) ,
407
436
source : adocListItem . getSource ( ) ,
0 commit comments