This repository was archived by the owner on Sep 11, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -182,7 +182,8 @@ export function formatRangeAsCode(range: Range): void {
182182
183183 const hasBlockFormatting = ( range . length > 0 )
184184 && range . text . startsWith ( "```" )
185- && range . text . endsWith ( "```" ) ;
185+ && range . text . endsWith ( "```" )
186+ && range . text . includes ( '\n' ) ;
186187 const hasBacktick = ( range . text . includes ( "`" ) )
187188 && ! range . text . startsWith ( "`" )
188189 && ! range . text . endsWith ( "`" ) ;
@@ -208,11 +209,16 @@ export function formatRangeAsCode(range: Range): void {
208209 parts . push ( partCreator . newline ( ) ) ;
209210 }
210211 } else {
212+ const fenceLen = longestBacktickSequence ( range . text ) ;
211213 if ( hasBacktick ) {
212- parts . unshift ( partCreator . plain ( "`" . repeat ( longestBacktickSequence ( range . text ) + 1 ) ) ) ;
213- parts . push ( partCreator . plain ( "`" . repeat ( longestBacktickSequence ( range . text ) + 1 ) ) ) ;
214+ parts . unshift ( partCreator . plain ( "`" . repeat ( fenceLen + 1 ) ) ) ;
215+ parts . push ( partCreator . plain ( "`" . repeat ( fenceLen + 1 ) ) ) ;
214216 } else {
215- toggleInlineFormat ( range , "`" ) ;
217+ if ( fenceLen === 0 ) {
218+ toggleInlineFormat ( range , "`" ) ;
219+ return ;
220+ }
221+ toggleInlineFormat ( range , "`" . repeat ( fenceLen ) ) ;
216222 return ;
217223 }
218224 }
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import {
2121 toggleInlineFormat ,
2222 selectRangeOfWordAtCaret ,
2323 formatRange ,
24+ formatRangeAsCode ,
2425} from "../../src/editor/operations" ;
2526import { Formatting } from "../../src/components/views/rooms/MessageComposerFormatBar" ;
2627import { longestBacktickSequence } from '../../src/editor/deserialize' ;
@@ -58,7 +59,7 @@ describe('editor/operations: formatting operations', () => {
5859 expect ( range . parts [ 0 ] . text . trim ( ) . includes ( "`" ) ) . toBeTruthy ( ) ;
5960 expect ( longestBacktickSequence ( range . parts [ 0 ] . text . trim ( ) ) ) . toBe ( 1 ) ;
6061 expect ( model . serializeParts ( ) ) . toEqual ( [ { "text" : "hello ` world!" , "type" : "plain" } ] ) ;
61- toggleInlineFormat ( range , "`" . repeat ( longestBacktickSequence ( range . parts [ 0 ] . text . trim ( ) ) + 1 ) ) ;
62+ formatRangeAsCode ( range ) ;
6263 expect ( model . serializeParts ( ) ) . toEqual ( [ { "text" : "``hello ` world``!" , "type" : "plain" } ] ) ;
6364 } ) ;
6465
You can’t perform that action at this time.
0 commit comments