Skip to content

Commit 2147a87

Browse files
committed
Fix control characters in link, image destinations
Closes GH-46.
1 parent a57b805 commit 2147a87

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/handle/image.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export function image(node, _, context) {
2424
if (
2525
// If there’s no url but there is a title…
2626
(!node.url && node.title) ||
27-
// Or if there’s markdown whitespace or an eol, enclose.
28-
/[ \t\r\n]/.test(node.url)
27+
// If there are control characters or whitespace.
28+
/[\0- \u007F]/.test(node.url)
2929
) {
3030
subexit = context.enter('destinationLiteral')
3131
value += '<' + safe(context, node.url, {before: '<', after: '>'}) + '>'

lib/handle/link.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function link(node, _, context) {
4646
if (
4747
// If there’s no url but there is a title…
4848
(!node.url && node.title) ||
49-
// Or if there’s markdown whitespace or an eol, enclose.
50-
/[ \t\r\n]/.test(node.url)
49+
// If there are control characters or whitespace.
50+
/[\0- \u007F]/.test(node.url)
5151
) {
5252
subexit = context.enter('destinationLiteral')
5353
value += '<' + safe(context, node.url, {before: '<', after: '>'}) + '>'

test/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,12 @@ test('image', (t) => {
14271427
'should escape a backslash in `url` in a raw url'
14281428
)
14291429

1430+
t.equal(
1431+
to({type: 'image', url: '\f'}),
1432+
'![](<\f>)\n',
1433+
'should support control characters in images'
1434+
)
1435+
14301436
t.equal(
14311437
to({type: 'image', url: '', title: 'b"c'}),
14321438
'![](<> "b\\"c")\n',
@@ -1781,6 +1787,12 @@ test('link', (t) => {
17811787
'should escape a backslash in `url` in a raw url'
17821788
)
17831789

1790+
t.equal(
1791+
to({type: 'link', url: '\f', children: []}),
1792+
'[](<\f>)\n',
1793+
'should support control characters in links'
1794+
)
1795+
17841796
t.equal(
17851797
to({type: 'link', url: '', title: 'b"c', children: []}),
17861798
'[](<> "b\\"c")\n',

0 commit comments

Comments
 (0)