From 2f3eeb7a5a258f21942c82ba47c47edb6b3284f6 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 13 Dec 2022 18:01:51 +0400 Subject: [PATCH] Remove unneeded escapes of `-`, `*` Related-to: GH-57. --- lib/unsafe.js | 4 ++-- test/index.js | 54 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/unsafe.js b/lib/unsafe.js index 4c96ae9..832d18f 100644 --- a/lib/unsafe.js +++ b/lib/unsafe.js @@ -85,13 +85,13 @@ export const unsafe = [ {atBreak: true, before: '\\d+', character: ')'}, {character: ')', inConstruct: 'destinationRaw'}, // An asterisk can start thematic breaks, list items, emphasis, strong. - {atBreak: true, character: '*'}, + {atBreak: true, character: '*', after: '(?:[ \t\r\n*])'}, {character: '*', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans}, // A plus sign could start a list item. {atBreak: true, character: '+', after: '(?:[ \t\r\n])'}, // A dash can start thematic breaks, list items, and setext heading // underlines. - {atBreak: true, character: '-'}, + {atBreak: true, character: '-', after: '(?:[ \t\r\n-])'}, // A dot could start a list item. {atBreak: true, before: '\\d+', character: '.', after: '(?:[ \t\r\n]|$)'}, // Slash, colon, and semicolon are not used in markdown for constructs. diff --git a/test/index.js b/test/index.js index fd56080..11273e4 100644 --- a/test/index.js +++ b/test/index.js @@ -1646,8 +1646,8 @@ test('code (text)', (t) => { ) t.equal( - to({type: 'inlineCode', value: 'a\n-'}), - '`a -`\n', + to({type: 'inlineCode', value: 'a\n- b'}), + '`a - b`\n', 'should prevent breaking out of code (-)' ) @@ -1664,14 +1664,14 @@ test('code (text)', (t) => { ) t.equal( - to({type: 'inlineCode', value: 'a\r-'}), - '`a -`\n', + to({type: 'inlineCode', value: 'a\r- b'}), + '`a - b`\n', 'should prevent breaking out of code (cr)' ) t.equal( - to({type: 'inlineCode', value: 'a\r\n-'}), - '`a -`\n', + to({type: 'inlineCode', value: 'a\r\n- b'}), + '`a - b`\n', 'should prevent breaking out of code (crlf)' ) @@ -3031,12 +3031,6 @@ test('escape', (t) => { 'should escape what would otherwise be a character escape' ) - t.equal( - to({type: 'paragraph', children: [{type: 'text', value: '+a'}]}), - '+a\n', - 'should not escape a `+` if it is not followed by a whitespace' - ) - t.equal( to({ type: 'paragraph', @@ -3171,6 +3165,12 @@ test('escape', (t) => { 'should escape what would otherwise be a list item (plus)' ) + t.equal( + to({type: 'paragraph', children: [{type: 'text', value: '+a'}]}), + '+a\n', + 'should not escape `+` when not followed by whitespace' + ) + t.equal( to({type: 'paragraph', children: [{type: 'text', value: '- a\n- b'}]}), '\\- a\n\\- b\n', @@ -3178,11 +3178,37 @@ test('escape', (t) => { ) t.equal( - to({type: 'paragraph', children: [{type: 'text', value: '* a\n* b'}]}), - '\\* a\n\\* b\n', + to({type: 'paragraph', children: [{type: 'text', value: '-a'}]}), + '-a\n', + 'should not escape `-` when not followed by whitespace' + ) + + t.equal( + to({type: 'paragraph', children: [{type: 'text', value: '--a'}]}), + '\\--a\n', + 'should escape `-` when followed by another `-` (as it looks like a thematic break, setext underline)' + ) + + // Note: these are in titles, because the `*` case here is about flow nodes, + // not phrasing (emphasis). + t.equal( + to({type: 'definition', identifier: 'x', url: 'y', title: 'a\n* b\n* c'}), + '[x]: y "a\n\\* b\n\\* c"\n', 'should escape what would otherwise be a list item (asterisk)' ) + t.equal( + to({type: 'definition', identifier: 'x', url: 'y', title: 'a\n*b'}), + '[x]: y "a\n*b"\n', + 'should not escape `*` when not followed by whitespace' + ) + + t.equal( + to({type: 'definition', identifier: 'x', url: 'y', title: 'a\n**b'}), + '[x]: y "a\n\\**b"\n', + 'should escape `*` when followed by another `*` (as it looks like a thematic break)' + ) + t.equal( to({type: 'paragraph', children: [{type: 'text', value: '1. a\n2. b'}]}), '1\\. a\n2\\. b\n',