Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 46771de

Browse files
committed
fix-update: escape backticks in between text
1 parent 1e060fe commit 46771de

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/editor/deserialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function escape(text: string): string {
3232

3333
// Finds the length of the longest backtick sequence in the given text, used for
3434
// escaping backticks in code blocks
35-
function longestBacktickSequence(text: string): number {
35+
export function longestBacktickSequence(text: string): number {
3636
let length = 0;
3737
let currentLength = 0;
3838

src/editor/operations.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import Range from "./range";
1818
import { Part, Type } from "./parts";
1919
import { Formatting } from "../components/views/rooms/MessageComposerFormatBar";
20+
import { longestBacktickSequence } from './deserialize';
2021

2122
/**
2223
* Some common queries and transformations on the editor model
@@ -182,11 +183,13 @@ export function formatRangeAsCode(range: Range): void {
182183
const hasBlockFormatting = (range.length > 0)
183184
&& range.text.startsWith("```")
184185
&& range.text.endsWith("```");
186+
const hasBacktick = (range.text.includes("`"))
187+
&& !range.text.startsWith("`")
188+
&& !range.text.endsWith("`");
185189

186190
const needsBlockFormatting = parts.some(p => p.type === Type.Newline);
187191

188192
if (hasBlockFormatting) {
189-
// Remove previously pushed backticks and new lines
190193
parts.shift();
191194
parts.pop();
192195
if (parts[0]?.text === "\n" && parts[parts.length - 1]?.text === "\n") {
@@ -205,8 +208,13 @@ export function formatRangeAsCode(range: Range): void {
205208
parts.push(partCreator.newline());
206209
}
207210
} else {
208-
toggleInlineFormat(range, "`");
209-
return;
211+
if (hasBacktick) {
212+
parts.unshift(partCreator.plain("`".repeat(longestBacktickSequence(range.text)+ 1)));
213+
parts.push(partCreator.plain("`".repeat(longestBacktickSequence(range.text)+ 1)));
214+
} else {
215+
toggleInlineFormat(range, "`");
216+
return;
217+
}
210218
}
211219

212220
replaceRangeAndExpandSelection(range, parts);
@@ -240,6 +248,7 @@ export function toggleInlineFormat(range: Range, prefix: string, suffix = prefix
240248
// compute paragraph [start, end] indexes
241249
const paragraphIndexes = [];
242250
let startIndex = 0;
251+
243252
// start at i=2 because we look at i and up to two parts behind to detect paragraph breaks at their end
244253
for (let i = 2; i < parts.length; i++) {
245254
// paragraph breaks can be denoted in a multitude of ways,

0 commit comments

Comments
 (0)