@@ -343,9 +343,6 @@ function tidyMarkdown(markdown: string): string {
343343 . trim ( ) ;
344344}
345345
346- const DELIMITED_TABLE_MARKER = "\uE000" ;
347- const DELIMITED_TABLE_PLACEHOLDER = / \uE000 ( \d + ) \uE000 / g;
348-
349346/**
350347 * Serializes a rendered fragment back to markdown. Tables follow `tableFormat`;
351348 * delimited formats replace each table with its TSV or CSV rows.
@@ -356,22 +353,23 @@ export function serializeRenderedMarkdownFragment(
356353) : string {
357354 const codeBlock = soleCodeBlock ( container ) ;
358355 if ( codeBlock ) return ( codeBlock . textContent ?? "" ) . replace ( / \n $ / , "" ) ;
359- if ( tableFormat === "markdown" ) return tidyMarkdown ( serializeChildren ( container ) ) ;
356+ if ( tableFormat === "markdown" || container . nodeType !== Node . ELEMENT_NODE ) {
357+ return tidyMarkdown ( serializeChildren ( container ) ) ;
358+ }
360359 // Tag a copy so the caller's fragment still produces untouched HTML. Tables
361360 // stay placeholders through tidyMarkdown, which would strip the tabs of empty
362- // edge cells and shift the columns after them.
361+ // edge cells and shift the columns after them. The per-copy marker cannot
362+ // collide with copied text.
363363 const tagged = container . cloneNode ( true ) as Element ;
364+ const marker = `\uE000${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } \uE000` ;
364365 const delimitedTables : string [ ] = [ ] ;
365366 for ( const table of tagged . querySelectorAll ( "table" ) ) {
366- table . setAttribute (
367- "data-markdown-copy" ,
368- `${ DELIMITED_TABLE_MARKER } ${ delimitedTables . length } ${ DELIMITED_TABLE_MARKER } \n\n` ,
369- ) ;
367+ table . setAttribute ( "data-markdown-copy" , `${ marker } ${ delimitedTables . length } ${ marker } \n\n` ) ;
370368 delimitedTables . push ( serializeTableElementToDelimited ( table , tableFormat ) ) ;
371369 }
372370 return tidyMarkdown ( serializeChildren ( tagged ) ) . replace (
373- DELIMITED_TABLE_PLACEHOLDER ,
374- ( _ , index : string ) => delimitedTables [ Number ( index ) ] ?? "" ,
371+ new RegExp ( ` ${ marker } (\\d+) ${ marker } ` , "g" ) ,
372+ ( match , index : string ) => delimitedTables [ Number ( index ) ] ?? match ,
375373 ) ;
376374}
377375
@@ -457,7 +455,12 @@ export function markdownWithTableCopyFormat(markdown: string, format: TableCopyF
457455 return cell ? markdownPlainText ( cell ) : "" ;
458456 } ) ,
459457 ) ;
460- result = `${ result . slice ( 0 , start ) } ${ delimitedRows ( rows , format ) } ${ result . slice ( end ) } ` ;
458+ // A table in a blockquote or list starts after its line's container prefix
459+ // ("> ", indentation) and swallows that prefix on later lines. Replace from
460+ // the line start so no row keeps a stray marker.
461+ const lineStart = result . lastIndexOf ( "\n" , start - 1 ) + 1 ;
462+ const replaceFrom = / ^ [ \s > ] * $ / . test ( result . slice ( lineStart , start ) ) ? lineStart : start ;
463+ result = `${ result . slice ( 0 , replaceFrom ) } ${ delimitedRows ( rows , format ) } ${ result . slice ( end ) } ` ;
461464 }
462465 return result ;
463466}
0 commit comments