Skip to content

Commit 2932008

Browse files
committed
Fix to not escape superfluous character
If a character is supposed to be escaped, but only because it has a condition on the next character, and the next character is definitely being escaped), then leave the first one and defer to the next. Closes GH-5.
1 parent 9efedb3 commit 2932008

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

lib/util/safe.js

+40-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function safe(context, input, config) {
1010
var index = -1
1111
var positions = []
1212
var result = []
13+
var infos = {}
14+
var info
1315
var position
1416
var character
1517
var pattern
@@ -36,12 +38,30 @@ function safe(context, input, config) {
3638
continue
3739
}
3840

39-
expression = toExpression(pattern)
41+
expression =
42+
pattern._compiled || (pattern._compiled = toExpression(pattern))
4043

4144
while ((match = expression.exec(value))) {
42-
positions.push(
45+
position =
4346
match.index + (pattern.before || pattern.atBreak ? match[1].length : 0)
44-
)
47+
48+
info = {
49+
before: pattern.atBreak || 'before' in pattern,
50+
after: 'after' in pattern
51+
}
52+
53+
if (positions.indexOf(position) === -1) {
54+
positions.push(position)
55+
infos[position] = info
56+
} else {
57+
if (infos[position].before && !info.before) {
58+
infos[position].before = false
59+
}
60+
61+
if (infos[position].after && !info.after) {
62+
infos[position].after = false
63+
}
64+
}
4565
}
4666
}
4767

@@ -54,22 +74,35 @@ function safe(context, input, config) {
5474

5575
while (++index < length) {
5676
position = positions[index]
77+
info = infos[position]
5778

5879
if (
5980
// Character before or after matched:
6081
position < start ||
61-
position >= end ||
62-
// Character matched multiple times:
63-
position === positions[index + 1]
82+
position >= end
83+
) {
84+
continue
85+
}
86+
87+
// If this character is supposed to be escaped, but only because it has a
88+
// condition on the next character, and the next character is definitly
89+
// being escaped), then skip this escape.
90+
if (
91+
info.after &&
92+
position + 1 < end &&
93+
positions[index + 1] === position + 1 &&
94+
!infos[position + 1].before &&
95+
!infos[position + 1].after
6496
) {
6597
continue
6698
}
6799

100+
character = value.charAt(position)
101+
68102
if (start !== position) {
69103
result.push(value.slice(start, position))
70104
}
71105

72-
character = value.charAt(position)
73106
start = position
74107

75108
if (

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"prettier": true,
8181
"esnext": false,
8282
"rules": {
83+
"complexity": "off",
8384
"unicorn/prefer-includes": "off"
8485
},
8586
"ignores": [

test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2472,13 +2472,13 @@ test('escape', function (t) {
24722472

24732473
t.equal(
24742474
to({type: 'paragraph', children: [{type: 'text', value: '![a][b]'}]}),
2475-
'\\!\\[a]\\[b]\n',
2475+
'!\\[a]\\[b]\n',
24762476
'should escape what would otherwise be an image (reference)'
24772477
)
24782478

24792479
t.equal(
24802480
to({type: 'paragraph', children: [{type: 'text', value: '![](a.jpg)'}]}),
2481-
'\\!\\[]\\(a.jpg)\n',
2481+
'!\\[]\\(a.jpg)\n',
24822482
'should escape what would otherwise be an image (resource)'
24832483
)
24842484

0 commit comments

Comments
 (0)