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

Commit

Permalink
backticks tests and untoggling update
Browse files Browse the repository at this point in the history
  • Loading branch information
yaya-usman committed Apr 2, 2022
1 parent b743e8b commit df033cc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/editor/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function escape(text: string): string {
// Finds the length of the longest backtick sequence in the given text, used for
// escaping backticks in code blocks
export function longestBacktickSequence(text: string): number {
let length = 0;
let length = 1;
let currentLength = 0;

for (const c of text) {
Expand Down
8 changes: 4 additions & 4 deletions src/editor/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ export function formatRangeAsCode(range: Range): void {
parts.push(partCreator.newline());
}
} else {
const fenceLen = longestBacktickSequence(range.text);
if (hasBacktick) {
parts.unshift(partCreator.plain("`".repeat(longestBacktickSequence(range.text)+ 1)));
parts.push(partCreator.plain("`".repeat(longestBacktickSequence(range.text)+ 1)));
parts.unshift(partCreator.plain("`".repeat(fenceLen + 1)));
parts.push(partCreator.plain("`".repeat(fenceLen+ 1)));
} else {
toggleInlineFormat(range, "`".repeat(longestBacktickSequence(range.text)));
toggleInlineFormat(range, "`".repeat(fenceLen));
return;
}
}

replaceRangeAndExpandSelection(range, parts);
}

Expand Down
3 changes: 2 additions & 1 deletion test/editor/operations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
toggleInlineFormat,
selectRangeOfWordAtCaret,
formatRange,
formatRangeAsCode,
} from "../../src/editor/operations";
import { Formatting } from "../../src/components/views/rooms/MessageComposerFormatBar";
import { longestBacktickSequence } from '../../src/editor/deserialize';
Expand Down Expand Up @@ -58,7 +59,7 @@ describe('editor/operations: formatting operations', () => {
expect(range.parts[0].text.trim().includes("`")).toBeTruthy();
expect(longestBacktickSequence(range.parts[0].text.trim())).toBe(1);
expect(model.serializeParts()).toEqual([{ "text": "hello ` world!", "type": "plain" }]);
toggleInlineFormat(range, "`".repeat(longestBacktickSequence(range.parts[0].text.trim()) + 1));
formatRangeAsCode(range);
expect(model.serializeParts()).toEqual([{ "text": "``hello ` world``!", "type": "plain" }]);
});

Expand Down

0 comments on commit df033cc

Please sign in to comment.