diff --git a/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts b/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts index 67ee973af..d6ed452df 100644 --- a/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts +++ b/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts @@ -57,7 +57,6 @@ export class HighlightRuleComponent { if (!lineNumber) return null; const isUnbounded = groups.every(x => x === ''); - if (isUnbounded) { return new HighlightRuleComponent(lineNumber, true, []); } @@ -115,36 +114,17 @@ export class HighlightRuleComponent { let [start, end] = bound; if (start === UNBOUNDED) { - if (isAbsoluteIndexing) { - start = 0; - } else { - start = indents.length; - } - } else if (!isAbsoluteIndexing) { - start += indents.length; - // Clamp values - if (start < indents.length) { - start = indents.length; - } else if (start > line.length) { - start = line.length; - } - } else if (start > line.length) { - start = line.length; + start = isAbsoluteIndexing ? 0 : indents.length; + } else { + start = isAbsoluteIndexing ? start : Math.max(start + indents.length, indents.length); + start = Math.min(start, line.length); } if (end === UNBOUNDED) { end = line.length; - } else if (!isAbsoluteIndexing) { - end += indents.length; - - // Clamp values - if (end < indents.length) { - end = indents.length; - } else if (end > line.length) { - end = line.length; - } - } else if (end > line.length) { - end = line.length; + } else { + end = isAbsoluteIndexing ? end : Math.max(end + indents.length, indents.length); + end = Math.min(end, line.length); } return [start, end]; diff --git a/packages/core/test/unit/lib/markdown-it/highlight/HighlightRuleComponent.test.ts b/packages/core/test/unit/lib/markdown-it/highlight/HighlightRuleComponent.test.ts index 12e5eef6e..8ee9420f9 100644 --- a/packages/core/test/unit/lib/markdown-it/highlight/HighlightRuleComponent.test.ts +++ b/packages/core/test/unit/lib/markdown-it/highlight/HighlightRuleComponent.test.ts @@ -87,9 +87,9 @@ describe('computeCharBounds, absolute value bounds', () => { expect(bounds).toEqual([' some text'.length, ' some text'.length]); }); - test('handles line-length end correctly', () => { - const bounds = HighlightRuleComponent.computeCharBounds([0, 4], ' abcd', true); - expect(bounds).toEqual([0, 4]); + test('handles bounds spanning from start to line length correctly', () => { + const bounds = HighlightRuleComponent.computeCharBounds([0, 11], ' some text', true); + expect(bounds).toEqual([0, 11]); }); });