@@ -221,9 +221,9 @@ function collapseRegion(tr: Transaction, region: TextRange, type: MarkType): voi
221221function collapseMarkdownSource (
222222 tr : Transaction ,
223223 keep : ( region : TextRange ) => boolean = ( ) => false ,
224- ) : void {
224+ ) : TextRange [ ] {
225225 const type = tr . doc . type . schema . marks [ MARKDOWN_SOURCE_MARK ] ;
226- if ( ! type ) return ;
226+ if ( ! type ) return [ ] ;
227227 const regions : TextRange [ ] = [ ] ;
228228 for ( const region of sourceRegions ( tr . doc , type ) ) {
229229 const extended = withAdjacentDelimiters ( tr . doc , region ) ;
@@ -235,6 +235,7 @@ function collapseMarkdownSource(
235235 if ( keep ( region ) ) tr . addMark ( region . from , region . to , type . create ( ) ) ;
236236 else collapseRegion ( tr , region , type ) ;
237237 }
238+ return regions ;
238239}
239240
240241function mapSelection ( tr : Transaction , selection : Selection , map : ( pos : number ) => number ) {
@@ -248,7 +249,14 @@ function mapSelection(tr: Transaction, selection: Selection, map: (pos: number)
248249
249250/** Parses all shown source first, so a command that splits it keeps its styles. */
250251export const collapseShownMarkdownSource : Command = ( { tr } ) => {
251- collapseMarkdownSource ( tr ) ;
252+ const { selection } = tr ;
253+ const type = tr . doc . type . schema . marks [ MARKDOWN_SOURCE_MARK ] ;
254+ const marks = tr . storedMarks ?? selection . $from . marks ( ) ;
255+ const regions = collapseMarkdownSource ( tr ) ;
256+ // A caret after the closing markers is outside the styles, though parsed text there is inclusive.
257+ if ( type && selection . empty && regions . some ( ( { to } ) => to === selection . from ) ) {
258+ tr . setStoredMarks ( type . removeFromSet ( marks ) ) ;
259+ }
252260 return true ;
253261} ;
254262
0 commit comments