@@ -33,17 +33,10 @@ export default function headingAnchor() {
3333 return value
3434 }
3535 const headings = extractHeadings ( newState . doc )
36- // if the headings are the same, keep the decorations
37- if ( sameHeadings ( headings , value . headings ) ) {
38- return {
39- headings,
40- decorations : value . decorations . map ( tr . mapping , tr . doc ) ,
41- }
42- }
43- return {
44- headings,
45- decorations : anchorDecorations ( newState . doc , headings ) ,
46- }
36+ const decorations = headingsChanged ( headings , value . headings )
37+ ? anchorDecorations ( newState . doc , headings )
38+ : value . decorations . map ( tr . mapping , tr . doc )
39+ return { headings, decorations }
4740 } ,
4841 } ,
4942
@@ -56,20 +49,30 @@ export default function headingAnchor() {
5649}
5750
5851/**
59- * Check if the headings provided have the same ids.
60- *
61- * This is enough to ensure no updates are needed
62- * as the id includes a slugified version of the text
63- * and the level.
52+ * Check if the headings provided are equivalent.
6453 *
6554 * @param {Array } current - array of headings
6655 * @param {Array } other - headings to compare against
6756 *
6857 * @return {boolean }
6958 */
70- function sameHeadings ( current , other ) {
71- if ( current . length !== other . length ) return false
72- return current . every ( ( heading , i ) => heading . id === other [ i ] . id )
59+ function headingsChanged ( current , prev ) {
60+ return ( current . length !== prev . length )
61+ || current . some ( isDifferentFrom ( prev ) )
62+ }
63+
64+ /**
65+ * Check if headings are different - i.e. have different id or level
66+ *
67+ * @param {Array } other - headings to compare against
68+ *
69+ * Returns a function to be used in a call to Array#some.
70+ * The returned function takes a heading and an index (as provided by iterators)
71+ * and compares the id and level of the heading to the one in `other` with the same index.
72+ */
73+ const isDifferentFrom = ( other ) => ( heading , i ) => {
74+ return heading . id !== other [ i ] . id
75+ || heading . level !== other [ i ] . level
7376}
7477
7578/**
0 commit comments