Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2f3eeb7

Browse files
committedDec 13, 2022
Remove unneeded escapes of -, *
Related-to: GH-57.
1 parent 4cb437c commit 2f3eeb7

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed
 

‎lib/unsafe.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ export const unsafe = [
8585
{atBreak: true, before: '\\d+', character: ')'},
8686
{character: ')', inConstruct: 'destinationRaw'},
8787
// An asterisk can start thematic breaks, list items, emphasis, strong.
88-
{atBreak: true, character: '*'},
88+
{atBreak: true, character: '*', after: '(?:[ \t\r\n*])'},
8989
{character: '*', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans},
9090
// A plus sign could start a list item.
9191
{atBreak: true, character: '+', after: '(?:[ \t\r\n])'},
9292
// A dash can start thematic breaks, list items, and setext heading
9393
// underlines.
94-
{atBreak: true, character: '-'},
94+
{atBreak: true, character: '-', after: '(?:[ \t\r\n-])'},
9595
// A dot could start a list item.
9696
{atBreak: true, before: '\\d+', character: '.', after: '(?:[ \t\r\n]|$)'},
9797
// Slash, colon, and semicolon are not used in markdown for constructs.

‎test/index.js

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,8 +1646,8 @@ test('code (text)', (t) => {
16461646
)
16471647

16481648
t.equal(
1649-
to({type: 'inlineCode', value: 'a\n-'}),
1650-
'`a -`\n',
1649+
to({type: 'inlineCode', value: 'a\n- b'}),
1650+
'`a - b`\n',
16511651
'should prevent breaking out of code (-)'
16521652
)
16531653

@@ -1664,14 +1664,14 @@ test('code (text)', (t) => {
16641664
)
16651665

16661666
t.equal(
1667-
to({type: 'inlineCode', value: 'a\r-'}),
1668-
'`a -`\n',
1667+
to({type: 'inlineCode', value: 'a\r- b'}),
1668+
'`a - b`\n',
16691669
'should prevent breaking out of code (cr)'
16701670
)
16711671

16721672
t.equal(
1673-
to({type: 'inlineCode', value: 'a\r\n-'}),
1674-
'`a -`\n',
1673+
to({type: 'inlineCode', value: 'a\r\n- b'}),
1674+
'`a - b`\n',
16751675
'should prevent breaking out of code (crlf)'
16761676
)
16771677

@@ -3031,12 +3031,6 @@ test('escape', (t) => {
30313031
'should escape what would otherwise be a character escape'
30323032
)
30333033

3034-
t.equal(
3035-
to({type: 'paragraph', children: [{type: 'text', value: '+a'}]}),
3036-
'+a\n',
3037-
'should not escape a `+` if it is not followed by a whitespace'
3038-
)
3039-
30403034
t.equal(
30413035
to({
30423036
type: 'paragraph',
@@ -3171,18 +3165,50 @@ test('escape', (t) => {
31713165
'should escape what would otherwise be a list item (plus)'
31723166
)
31733167

3168+
t.equal(
3169+
to({type: 'paragraph', children: [{type: 'text', value: '+a'}]}),
3170+
'+a\n',
3171+
'should not escape `+` when not followed by whitespace'
3172+
)
3173+
31743174
t.equal(
31753175
to({type: 'paragraph', children: [{type: 'text', value: '- a\n- b'}]}),
31763176
'\\- a\n\\- b\n',
31773177
'should escape what would otherwise be a list item (dash)'
31783178
)
31793179

31803180
t.equal(
3181-
to({type: 'paragraph', children: [{type: 'text', value: '* a\n* b'}]}),
3182-
'\\* a\n\\* b\n',
3181+
to({type: 'paragraph', children: [{type: 'text', value: '-a'}]}),
3182+
'-a\n',
3183+
'should not escape `-` when not followed by whitespace'
3184+
)
3185+
3186+
t.equal(
3187+
to({type: 'paragraph', children: [{type: 'text', value: '--a'}]}),
3188+
'\\--a\n',
3189+
'should escape `-` when followed by another `-` (as it looks like a thematic break, setext underline)'
3190+
)
3191+
3192+
// Note: these are in titles, because the `*` case here is about flow nodes,
3193+
// not phrasing (emphasis).
3194+
t.equal(
3195+
to({type: 'definition', identifier: 'x', url: 'y', title: 'a\n* b\n* c'}),
3196+
'[x]: y "a\n\\* b\n\\* c"\n',
31833197
'should escape what would otherwise be a list item (asterisk)'
31843198
)
31853199

3200+
t.equal(
3201+
to({type: 'definition', identifier: 'x', url: 'y', title: 'a\n*b'}),
3202+
'[x]: y "a\n*b"\n',
3203+
'should not escape `*` when not followed by whitespace'
3204+
)
3205+
3206+
t.equal(
3207+
to({type: 'definition', identifier: 'x', url: 'y', title: 'a\n**b'}),
3208+
'[x]: y "a\n\\**b"\n',
3209+
'should escape `*` when followed by another `*` (as it looks like a thematic break)'
3210+
)
3211+
31863212
t.equal(
31873213
to({type: 'paragraph', children: [{type: 'text', value: '1. a\n2. b'}]}),
31883214
'1\\. a\n2\\. b\n',

0 commit comments

Comments
 (0)