Skip to content

Commit 9dee3fd

Browse files
committed
fix(minimap): space after tag should reset element type
1 parent 3ae909b commit 9dee3fd

2 files changed

Lines changed: 56 additions & 10 deletions

File tree

src/stores/minimap/subscriptions/effects/minimap-canvas/worker/shapes/helpers/calculate-chunk-positions.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,46 @@ describe('calculate-chunk-position', () => {
14791479
const actual = calculateChunkPositions(input, N_CHARS_PER_LINE, '', '');
14801480
expect(actual).toEqual(output);
14811481
});
1482+
1483+
test('case: should not detect italic with a space after the tag', () => {
1484+
const input = 'word1 * word2* word3';
1485+
const output = {
1486+
chunks: [
1487+
{
1488+
chunk: 'word1',
1489+
length_chars: 5,
1490+
line: 0,
1491+
type: null,
1492+
x_chars: 0,
1493+
},
1494+
{
1495+
chunk: '*',
1496+
line: 0,
1497+
x_chars: 6,
1498+
length_chars: 1,
1499+
type: null,
1500+
},
1501+
{
1502+
chunk: 'word2*',
1503+
line: 0,
1504+
x_chars: 8,
1505+
length_chars: 6,
1506+
type: null,
1507+
},
1508+
{
1509+
chunk: 'word3',
1510+
line: 0,
1511+
x_chars: 15,
1512+
length_chars: 5,
1513+
type: null,
1514+
},
1515+
],
1516+
totalLines: 1,
1517+
empty: false,
1518+
};
1519+
const actual = calculateChunkPositions(input, N_CHARS_PER_LINE, '', '');
1520+
expect(actual).toEqual(output);
1521+
});
14821522
});
14831523

14841524
describe('performance-test: calculate-chunk-position', () => {

src/stores/minimap/subscriptions/effects/minimap-canvas/worker/shapes/helpers/calculate-chunk-positions.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ export const calculateChunkPositions = (
153153
) {
154154
const elementName = charToElementName[character];
155155
if (elementName) {
156-
const nextCharacter = content[i + 1];
156+
const nextCharacter_1 = content[i + 1];
157+
const nextCharacter_2 = content[i + 2];
157158
// heading
158159
if (
159160
character === '#' &&
160161
state.x === 0 &&
161-
(nextCharacter === ' ' || nextCharacter === '#')
162+
(nextCharacter_1 === ' ' || nextCharacter_1 === '#')
162163
) {
163164
state.elementMeta = {
164165
elementName,
@@ -168,7 +169,7 @@ export const calculateChunkPositions = (
168169
// tag
169170
else if (
170171
character === '#' &&
171-
!illegalObsidianTagCharacters.has(nextCharacter) &&
172+
!illegalObsidianTagCharacters.has(nextCharacter_1) &&
172173
(!state.elementMeta ||
173174
state.elementMeta.canBeParent ||
174175
state.elementMeta.elementName === elementName)
@@ -185,7 +186,7 @@ export const calculateChunkPositions = (
185186
// bullet point
186187
else if (character === '-' && state.x === 0) {
187188
const isTask =
188-
nextCharacter === ' ' &&
189+
nextCharacter_1 === ' ' &&
189190
content[i + 2] === '[' &&
190191
content[i + 4] === ']';
191192
if (isTask) {
@@ -252,12 +253,12 @@ export const calculateChunkPositions = (
252253
) {
253254
const isHighlight = character === '=';
254255
const isDoubleCharacterTag =
255-
(isHighlight && nextCharacter === '=') ||
256-
(character === '~' && nextCharacter === '~') ||
257-
(character === '[' && nextCharacter === '[') ||
258-
(character === ']' && nextCharacter === ']') ||
256+
(isHighlight && nextCharacter_1 === '=') ||
257+
(character === '~' && nextCharacter_1 === '~') ||
258+
(character === '[' && nextCharacter_1 === '[') ||
259+
(character === ']' && nextCharacter_1 === ']') ||
259260
// **text**
260-
(character === '*' && nextCharacter === '*');
261+
(character === '*' && nextCharacter_1 === '*');
261262
const isDoubleTagElement =
262263
isDoubleCharacterTag ||
263264
// *text*
@@ -267,11 +268,16 @@ export const calculateChunkPositions = (
267268
if (isDoubleTagElement) {
268269
const isClosingTag =
269270
state.elementMeta?.elementName === elementName;
271+
const hasSpaceAfterStart = isClosingTag
272+
? false
273+
: isDoubleCharacterTag
274+
? nextCharacter_2 === ' '
275+
: nextCharacter_1 === ' ';
270276
if (isClosingTag) {
271277
state.closingTagLength = isDoubleCharacterTag
272278
? 2
273279
: 1;
274-
} else {
280+
} else if (!hasSpaceAfterStart) {
275281
const scope = isHighlight
276282
? ElementScope.multi_line
277283
: ElementScope.block;

0 commit comments

Comments
 (0)