@@ -770,7 +770,7 @@ export function buildTiptapContent(
770770 | { rule : string }
771771 | { heading : { level : number ; space : string ; inline : InlineJson [ ] } }
772772 | { quote : { prefix : string ; inline : InlineJson [ ] } }
773- | { line : DocLine }
773+ | { line : DocLine ; source : string }
774774 ) [ ] = [ ] ;
775775 const restoreSources = ( line : string ) =>
776776 line . split ( sentinel ) . reduce ( ( joined , piece , index ) => {
@@ -784,6 +784,7 @@ export function buildTiptapContent(
784784 if ( index === 0 && literal ) {
785785 entries . push ( {
786786 line : { list : null , inline : [ textJsonForSpan ( literal , [ ] ) , ...buildInline ( line ) ] } ,
787+ source : literal + line ,
787788 } ) ;
788789 continue ;
789790 }
@@ -806,7 +807,7 @@ export function buildTiptapContent(
806807 }
807808 const quote = blockSyntax ? / ^ ( > [ \t ] * ) ( [ ^ \n ] * ) $ / . exec ( line ) : null ;
808809 if ( quote ) entries . push ( { quote : { prefix : quote [ 1 ] ! , inline : buildInline ( quote [ 2 ] ! ) } } ) ;
809- else entries . push ( { line : buildDocLine ( line ) } ) ;
810+ else entries . push ( { line : buildDocLine ( line ) , source : line } ) ;
810811 continue ;
811812 }
812813 const language = restoreSources ( opening . language ) ;
@@ -857,7 +858,23 @@ export function buildTiptapContent(
857858 } ) ;
858859 openQuote = null ;
859860 } ;
860- for ( const entry of entries ) {
861+ const lazyTarget = ( entry : ( typeof entries ) [ number ] | undefined ) =>
862+ ! entry ? null : "quote" in entry ? "quote" : "line" in entry && entry . line . list ? "list" : null ;
863+ for ( const [ index , entry ] of entries . entries ( ) ) {
864+ const next = entries [ index + 1 ] ;
865+ if (
866+ "line" in entry &&
867+ entry . source === "" &&
868+ next &&
869+ "line" in next &&
870+ ! next . line . list &&
871+ needsBlankLineBetween ( lazyTarget ( entries [ index - 1 ] ) , {
872+ empty : next . source === "" ,
873+ indented : / ^ [ \t ] / . test ( next . source ) ,
874+ } )
875+ ) {
876+ continue ;
877+ }
861878 if ( "quote" in entry ) {
862879 flushLists ( ) ;
863880 if ( openQuote && openQuote . prefix !== entry . quote . prefix ) flushQuote ( ) ;
@@ -1004,12 +1021,37 @@ function pushBreakRun(acc: RichAccumulator, position?: number): void {
10041021 acc . md += 1 ;
10051022}
10061023
1007- // Separate prose from quotes and lists to avoid lazy continuation; an empty paragraph already
1008- // separates them. An indented line after a list continues the item on purpose.
1009- function needsBlankLineBeforeParagraph ( previous : ProseMirrorNode , block : ProseMirrorNode ) : boolean {
1010- if ( block . type . name !== "paragraph" || block . content . size === 0 ) return false ;
1011- if ( previous . type . name === "blockquote" ) return true ;
1012- return LIST_NODE_NAMES . has ( previous . type . name ) && ! / ^ [ \t ] / . test ( block . textContent ) ;
1024+ // Markdown folds a paragraph line into the quote or list right above it, so the serializer writes
1025+ // a blank line between them and the parser drops it. An empty paragraph is that line already, and
1026+ // an indented line after a list continues the item on purpose.
1027+ function needsBlankLineBetween (
1028+ previous : "quote" | "list" | null ,
1029+ paragraph : { empty : boolean ; indented : boolean } ,
1030+ ) : boolean {
1031+ if ( ! previous || paragraph . empty ) return false ;
1032+ return previous === "quote" || ! paragraph . indented ;
1033+ }
1034+
1035+ function lazyContinuationTarget ( block : ProseMirrorNode ) : "quote" | "list" | null {
1036+ if ( block . type . name === "blockquote" ) return "quote" ;
1037+ return LIST_NODE_NAMES . has ( block . type . name ) ? "list" : null ;
1038+ }
1039+
1040+ // Loading a draft can add the blank line Markdown needs after a quote or list. Moves an offset in
1041+ // `value` to the same text in `serialized`.
1042+ export function shiftPastAddedBlankLines (
1043+ value : string ,
1044+ serialized : string ,
1045+ offset : number ,
1046+ ) : number {
1047+ if ( value === serialized ) return offset ;
1048+ let shift = 0 ;
1049+ for ( let index = 0 ; index < offset ; index += 1 ) {
1050+ while ( value [ index ] !== serialized [ index + shift ] && serialized [ index + shift ] === "\n" ) {
1051+ shift += 1 ;
1052+ }
1053+ }
1054+ return Math . min ( serialized . length , offset + shift ) ;
10131055}
10141056
10151057// Blank-line runs own no document characters; offsets clamp to the paragraph start.
@@ -1455,7 +1497,16 @@ function serializeDoc(doc: ProseMirrorNode): RichDocMap {
14551497 let pmBlockStart = 0 ;
14561498 blocks . forEach ( ( block , blockIndex ) => {
14571499 if ( blockIndex > 0 && ! isTrailingLineAfterRule ( doc , blockIndex ) ) pushBreakRun ( acc ) ;
1458- if ( blockIndex > 0 && needsBlankLineBeforeParagraph ( blocks [ blockIndex - 1 ] ! , block ) ) {
1500+ const first = block . firstChild ;
1501+ if (
1502+ blockIndex > 0 &&
1503+ block . type . name === "paragraph" &&
1504+ needsBlankLineBetween ( lazyContinuationTarget ( blocks [ blockIndex - 1 ] ! ) , {
1505+ empty : ! first ,
1506+ // Checks the first node, not textContent, which skips a leading chip.
1507+ indented : Boolean ( first ?. isText && ! first . marks . length && / ^ [ \t ] / . test ( first . text ! ) ) ,
1508+ } )
1509+ ) {
14591510 pushBlankLineRun ( acc , pmBlockStart + 1 ) ;
14601511 }
14611512 if ( LIST_NODE_NAMES . has ( block . type . name ) ) {
0 commit comments