diff --git a/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts b/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts index 7ddc47a10..67ee973af 100644 --- a/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts +++ b/packages/core/src/lib/markdown-it/highlight/HighlightRuleComponent.ts @@ -1,6 +1,6 @@ import { splitCodeAndIndentation } from './helper'; -const LINESLICE_CHAR_REGEX = /(_?)(\d+)\[(\d*):(\d*)]/; +const LINESLICE_CHAR_REGEX = /(\+?)(\d+)\[(\d*):(\d*)]/; const LINESLICE_WORD_REGEX = /(\d+)\[(\d*)::(\d*)]/; const LINEPART_REGEX = /(\d+)\[(["'])((?:\\.|[^\\])*?)\2]/; const UNBOUNDED = -1; @@ -43,13 +43,13 @@ export class HighlightRuleComponent { const linesliceWordMatch = compString.match(LINESLICE_WORD_REGEX); const sliceMatch = linesliceCharMatch || linesliceWordMatch; if (sliceMatch) { - // There are four/five capturing groups: [full match, is highlight spaces (for char slice), + // There are four/five capturing groups: [full match, is absolute indexing (for char match), // line number, start bound, end bound] const groups = sliceMatch.slice(1); // discard full match - let isHighlightSpaces = false; + let isAbsoluteIndexing = false; if (sliceMatch === linesliceCharMatch) { - isHighlightSpaces = groups.shift() === '_'; + isAbsoluteIndexing = groups.shift() === '+'; } const lineNumber = HighlightRuleComponent @@ -65,7 +65,7 @@ export class HighlightRuleComponent { let bound = groups.map(x => (x !== '' ? parseInt(x, 10) : UNBOUNDED)) as [number, number]; const isCharSlice = sliceMatch === linesliceCharMatch; bound = isCharSlice - ? HighlightRuleComponent.computeCharBounds(bound, lines[lineNumber - 1], isHighlightSpaces) + ? HighlightRuleComponent.computeCharBounds(bound, lines[lineNumber - 1], isAbsoluteIndexing) : HighlightRuleComponent.computeWordBounds(bound, lines[lineNumber - 1]); return new HighlightRuleComponent(lineNumber, true, [bound]); @@ -102,26 +102,25 @@ export class HighlightRuleComponent { * comparing the bounds and the line's range. * * If the bound does not specify either the start or the end bound, the computed bound will default - * to the start or end of line. The bound will either include or exclude leading whitespaces depending - * on the user's preference. + * to the start or end of line. The bound can be either absolute or relative to the indentation level. * * @param bound The user-defined bound * @param line The given line - * @param isHighlightSpaces Whether to highlight leading whitespaces + * @param isAbsoluteIndexing Whether the bound is absolute or relative to the indentation level * @returns {[number, number]} The actual bound computed */ static computeCharBounds(bound: [number, number], line: string, - isHighlightSpaces: boolean): [number, number] { + isAbsoluteIndexing: boolean): [number, number] { const [indents] = splitCodeAndIndentation(line); let [start, end] = bound; if (start === UNBOUNDED) { - if (isHighlightSpaces) { + if (isAbsoluteIndexing) { start = 0; } else { start = indents.length; } - } else if (!isHighlightSpaces) { + } else if (!isAbsoluteIndexing) { start += indents.length; // Clamp values if (start < indents.length) { @@ -135,7 +134,7 @@ export class HighlightRuleComponent { if (end === UNBOUNDED) { end = line.length; - } else if (!isHighlightSpaces) { + } else if (!isAbsoluteIndexing) { end += indents.length; // Clamp values 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 0a17b159a..12e5eef6e 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 @@ -44,7 +44,7 @@ describe('parseRuleComponent', () => { }); }); -describe('computeCharBounds, no whitespace highlighting', () => { +describe('computeCharBounds, relative to indent-level', () => { test('computes character bounds correctly', () => { const bounds = HighlightRuleComponent.computeCharBounds([2, 5], ' some text', false); expect(bounds).toEqual([4, 7]); @@ -66,7 +66,7 @@ describe('computeCharBounds, no whitespace highlighting', () => { }); }); -describe('computeCharBounds, with whitespace highlighting', () => { +describe('computeCharBounds, absolute value bounds', () => { test('computes character bounds correctly', () => { const bounds = HighlightRuleComponent.computeCharBounds([2, 5], ' some text', true); expect(bounds).toEqual([2, 5]);