Skip to content

Commit 583714b

Browse files
MelvinBotinimaga
andcommitted
Extract isInAnyRange helper to deduplicate range-membership checks
Co-authored-by: Issa Nimaga <inimaga@users.noreply.github.com>
1 parent 7a10158 commit 583714b

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/pages/inbox/conciergeDraftState.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const INLINE_CODE_DELIMITER = '`';
3030
const BOLD_DELIMITER = '**';
3131
const STRIKETHROUGH_DELIMITER = '~~';
3232

33+
function isInAnyRange(position: number, ranges: TextRange[]): boolean {
34+
return ranges.some((range) => position >= range.start && position < range.end);
35+
}
36+
3337
function isEscaped(text: string, index: number): boolean {
3438
let slashCount = 0;
3539
let pos = index - 1;
@@ -68,8 +72,7 @@ function getCodeRanges(text: string): {ranges: TextRange[]; unclosedCodeBlockSta
6872
let openingDelimiterIndex: number | null = null;
6973

7074
for (let pos = lineStart; pos < lineEnd; pos++) {
71-
const isInCodeRange = ranges.some((range) => pos >= range.start && pos < range.end);
72-
if (text[pos] !== INLINE_CODE_DELIMITER || isEscaped(text, pos) || isInCodeRange) {
75+
if (text[pos] !== INLINE_CODE_DELIMITER || isEscaped(text, pos) || isInAnyRange(pos, ranges)) {
7376
continue;
7477
}
7578

@@ -96,8 +99,7 @@ function stripUnpairedLastLineDelimiter(text: string, delimiter: string, ignored
9699
const delimiterIndexes: number[] = [];
97100

98101
for (let pos = lastLineStart; pos <= text.length - delimiter.length; pos++) {
99-
const isInIgnoredRange = ignoredRanges.some((range) => pos >= range.start && pos < range.end);
100-
if (!text.startsWith(delimiter, pos) || isEscaped(text, pos) || isInIgnoredRange) {
102+
if (!text.startsWith(delimiter, pos) || isEscaped(text, pos) || isInAnyRange(pos, ignoredRanges)) {
101103
continue;
102104
}
103105

@@ -116,8 +118,7 @@ function normalizeDelimiterForExpensiMark(text: string, delimiter: string, repla
116118
let result = '';
117119

118120
for (let pos = 0; pos < text.length; pos++) {
119-
const isInIgnoredRange = ignoredRanges.some((range) => pos >= range.start && pos < range.end);
120-
if (text.startsWith(delimiter, pos) && !isEscaped(text, pos) && !isInIgnoredRange) {
121+
if (text.startsWith(delimiter, pos) && !isEscaped(text, pos) && !isInAnyRange(pos, ignoredRanges)) {
121122
result += replacement;
122123
pos += delimiter.length - 1;
123124
continue;
@@ -153,8 +154,7 @@ function stripIncompleteMarkdown(markdown: string): string {
153154

154155
codeRanges = getCodeRanges(result).ranges;
155156
for (let openBracketIndex = result.length - 1; openBracketIndex >= 0; openBracketIndex--) {
156-
const isInCodeRange = codeRanges.some((range) => openBracketIndex >= range.start && openBracketIndex < range.end);
157-
if (result[openBracketIndex] !== '[' || isEscaped(result, openBracketIndex) || isInCodeRange) {
157+
if (result[openBracketIndex] !== '[' || isEscaped(result, openBracketIndex) || isInAnyRange(openBracketIndex, codeRanges)) {
158158
continue;
159159
}
160160

0 commit comments

Comments
 (0)