@@ -191,7 +191,9 @@ export const getRangeBlocks = (doc, start, end) => {
191
191
export const getHeadingsBlocksMap = ( doc , start , end ) => {
192
192
const titleHMap = [ ]
193
193
194
- doc . nodesBetween ( start , end , function ( node , pos , parent , index ) {
194
+ const newEnd = Math . min ( end , doc . content . size )
195
+
196
+ doc . nodesBetween ( start , newEnd , function ( node , pos , parent , index ) {
195
197
if ( node . type . name === ENUMS . NODES . HEADING_TYPE ) {
196
198
const headingLevel = node . firstChild ?. attrs ?. level
197
199
const depth = doc . resolve ( pos ) . depth
@@ -280,10 +282,11 @@ export const getPrevHeadingPos = (doc, startPos, endPos) => {
280
282
let prevHStartPos = 0
281
283
let prevHEndPos = 0
282
284
283
- doc . nodesBetween ( startPos , endPos , function ( node , pos ) {
285
+ // Ensure endPos doesn't exceed document size
286
+ const newEndPos = Math . min ( endPos , doc . content . size )
287
+ doc ?. nodesBetween ( startPos , newEndPos , function ( node , pos ) {
284
288
if ( node . type . name === ENUMS . NODES . HEADING_TYPE ) {
285
289
const depth = doc . resolve ( pos ) . depth
286
-
287
290
// INFO: this the trick I've looking for
288
291
if ( depth === 2 ) {
289
292
prevHStartPos = pos
@@ -410,7 +413,7 @@ export const insertRemainingHeadings = ({
410
413
mapHPost . at ( 0 ) . startBlockPos ,
411
414
mapHPost . length === 1 && mapHPost . at ( 0 ) . le === 1
412
415
? mapHPost . at ( 0 ) . endBlockPos
413
- : tr . mapping . map ( mapHPost . at ( 0 ) . endBlockPos )
416
+ : Math . max ( tr . mapping . map ( mapHPost . at ( 0 ) . endBlockPos ) , mapHPost . at ( 0 ) . endBlockPos )
414
417
)
415
418
416
419
const prevHBlock = getPrevHeadingPos ( tr . doc , titleStartPos , tr . mapping . map ( titleEndPos ) )
@@ -823,8 +826,7 @@ export const insertHeadingsByNodeBlocks = (
823
826
// Iterate over each heading and insert it into the correct position
824
827
headings . forEach ( ( heading ) => {
825
828
const comingLevel = heading . attrs . level || heading . content . firstChild . attrs . level
826
- const startBlock =
827
- lastH1Inserted . startBlockPos === 0 ? tr . mapping . map ( from ) : lastH1Inserted . startBlockPos
829
+ const startBlock = lastH1Inserted . startBlockPos
828
830
const endBlock =
829
831
lastH1Inserted . endBlockPos === 0
830
832
? tr . mapping . map ( from )
@@ -840,10 +842,8 @@ export const insertHeadingsByNodeBlocks = (
840
842
841
843
// Find the last occurrence of the previous block level if there are duplicates
842
844
const robob = mapHPost . filter ( ( x ) => prevBlock ?. le === x ?. le )
843
- if ( robob . length > 1 ) {
844
- prevBlock = robob . at ( - 1 )
845
- }
846
845
846
+ if ( robob . length > 1 ) prevBlock = robob . at ( - 1 )
847
847
lastBlockPos = prevBlock ?. endBlockPos
848
848
849
849
// Update the previous heading start position if the previous block is at depth 2
@@ -863,3 +863,26 @@ export const insertHeadingsByNodeBlocks = (
863
863
864
864
return { lastBlockPos, prevHStartPos }
865
865
}
866
+
867
+ const removeBoldMark = ( node ) => {
868
+ if ( ! node . marks ) return node
869
+ return {
870
+ ...node ,
871
+ marks : node . marks . filter ( ( mark ) => mark . type !== 'bold' )
872
+ }
873
+ }
874
+
875
+ const convertContentBlockToParagraph = ( contentBlock ) => {
876
+ if ( ! contentBlock . level ) return contentBlock
877
+
878
+ return {
879
+ ...contentBlock ,
880
+ type : 'paragraph' ,
881
+ content : contentBlock . content ?. map ( removeBoldMark ) || contentBlock . content
882
+ }
883
+ }
884
+
885
+ export const convertHeadingsToParagraphs = ( contentBlocks ) => {
886
+ if ( ! Array . isArray ( contentBlocks ) ) return [ ]
887
+ return contentBlocks . map ( convertContentBlockToParagraph )
888
+ }
0 commit comments