Skip to content

Commit

Permalink
Add per-block caching to getFootnotesOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Jul 12, 2023
1 parent 1b49eb6 commit e2e5891
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions packages/core-data/src/footnotes/get-footnotes-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
*/
import getRichTextValuesCached from './get-rich-text-values-cached';

const cache = new WeakMap();

function getBlockFootnotesOrder( block ) {
const content = getRichTextValuesCached( block ).join( '' );
const newOrder = [];
if ( ! cache.has( block ) ) {
const content = getRichTextValuesCached( block ).join( '' );
const newOrder = [];

// https://github.com/WordPress/gutenberg/pull/43204 lands. We can then
// get the order directly from the rich text values.
if ( content.indexOf( 'data-fn' ) !== -1 ) {
const regex = /data-fn="([^"]+)"/g;
let match;
while ( ( match = regex.exec( content ) ) !== null ) {
newOrder.push( match[ 1 ] );
// https://github.com/WordPress/gutenberg/pull/43204 lands. We can then
// get the order directly from the rich text values.
if ( content.indexOf( 'data-fn' ) !== -1 ) {
const regex = /data-fn="([^"]+)"/g;
let match;
while ( ( match = regex.exec( content ) ) !== null ) {
newOrder.push( match[ 1 ] );
}
}
cache.set( block, newOrder );
}

return newOrder;
return cache.get( block );
}

export default function getFootnotesOrder( blocks ) {
Expand Down

0 comments on commit e2e5891

Please sign in to comment.