Skip to content

Commit

Permalink
Remove unneeded escapes of -, *
Browse files Browse the repository at this point in the history
Related-to: GH-57.
  • Loading branch information
wooorm committed Dec 13, 2022
1 parent 4cb437c commit 2f3eeb7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/unsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 40 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (-)'
)

Expand All @@ -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)'
)

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -3171,18 +3165,50 @@ 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',
'should escape what would otherwise be a list item (dash)'
)

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',
Expand Down

0 comments on commit 2f3eeb7

Please sign in to comment.