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 2e1533c commit 04e00ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/editor/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ export function formatRangeAsCode(range: Range): void {

const hasBlockFormatting = (range.length > 0)
&& range.text.startsWith("```")
&& range.text.endsWith("```");
&& range.text.endsWith("```")
&& range.text.includes('\n');
const hasBacktick = (range.text.includes("`"))
&& !range.text.startsWith("`")
&& !range.text.endsWith("`");
Expand All @@ -208,11 +209,16 @@ 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, "`");
if (fenceLen === 0) {
toggleInlineFormat(range, "`");
return;
}
toggleInlineFormat(range, "`".repeat(fenceLen));
return;
}
}
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 04e00ef

Please sign in to comment.