@@ -57,6 +57,13 @@ function codeFenceFor(code: string): string {
5757 return "`" . repeat ( Math . max ( 3 , longestRun + 1 ) ) ;
5858}
5959
60+ function outsideCodeFences ( markdown : string , tidy : ( prose : string ) => string ) : string {
61+ return markdown
62+ . split ( / ( ` ` ` [ \s \S ] * ?(?: ` ` ` | $ ) ) / )
63+ . map ( ( part , index ) => ( index % 2 === 1 ? part : tidy ( part ) ) )
64+ . join ( "" ) ;
65+ }
66+
6067function resolveCodeBlockLanguage ( pre : Element ) : string | null {
6168 const declared =
6269 pre . closest ( "[data-language]" ) ?. getAttribute ( "data-language" ) ??
@@ -120,11 +127,11 @@ function serializeListItem(
120127 const task = checkbox ? `[${ ( checkbox as HTMLInputElement ) . checked ? "x" : " " } ] ` : "" ;
121128 const bullet = ordered ? `${ index } . ` : "- " ;
122129 const marker = `${ bullet } ${ task } ` ;
123- let content = serializeChildren ( item , inTableCell )
124- . replace ( / \n { 3 , } / g, "\n\n" )
125- . trim ( ) ;
130+ let content = outsideCodeFences ( serializeChildren ( item , inTableCell ) , ( prose ) =>
131+ prose . replace ( / \n { 3 , } / g, "\n\n" ) ,
132+ ) . trim ( ) ;
126133 if ( ! item . querySelector ( ":scope > p" ) ) {
127- content = content . replace ( / \n { 2 , } / g, "\n" ) ;
134+ content = outsideCodeFences ( content , ( prose ) => prose . replace ( / \n { 2 , } / g, "\n" ) ) ;
128135 }
129136 // Continuations align after the bullet; task boxes belong to the item text.
130137 const continuationIndent = " " . repeat ( bullet . length ) ;
@@ -143,9 +150,9 @@ function serializeList(list: Element, ordered: boolean, inTableCell: boolean): s
143150}
144151
145152function serializeBlockquote ( quote : Element , inTableCell : boolean ) : string {
146- const content = serializeChildren ( quote , inTableCell )
147- . replace ( / \n { 3 , } / g, "\n\n" )
148- . trim ( ) ;
153+ const content = outsideCodeFences ( serializeChildren ( quote , inTableCell ) , ( prose ) =>
154+ prose . replace ( / \n { 3 , } / g, "\n\n" ) ,
155+ ) . trim ( ) ;
149156 if ( ! content ) return "" ;
150157 const quoted = content
151158 . split ( "\n" )
@@ -316,13 +323,9 @@ function soleCodeBlock(container: Node): Element | null {
316323}
317324
318325function tidyMarkdown ( markdown : string ) : string {
319- return markdown
320- . split ( / ( ` ` ` [ \s \S ] * ?(?: ` ` ` | $ ) ) / )
321- . map ( ( part , index ) =>
322- index % 2 === 1 ? part : part . replace ( / [ \t ] + (? = \n ) / g, "" ) . replace ( / \n { 3 , } / g, "\n\n" ) ,
323- )
324- . join ( "" )
325- . trim ( ) ;
326+ return outsideCodeFences ( markdown , ( prose ) =>
327+ prose . replace ( / [ \t ] + (? = \n ) / g, "" ) . replace ( / \n { 3 , } / g, "\n\n" ) ,
328+ ) . trim ( ) ;
326329}
327330
328331export function serializeRenderedMarkdownFragment ( container : Node ) : string {
0 commit comments