Skip to content

Commit 49bae06

Browse files
committed
Fix to not escape when unneeded in headings
Related to Mavrin/remark-typograf#2.
1 parent e40e015 commit 49bae06

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/handle/heading.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function heading(node, _, context) {
3535
sequence = repeat('#', rank)
3636
exit = context.enter('headingAtx')
3737
subexit = context.enter('phrasing')
38-
value = phrasing(node, context, {before: '\n', after: '\n'})
38+
value = phrasing(node, context, {before: '# ', after: '\n'})
3939
value = value ? sequence + ' ' + value : sequence
4040
if (context.options.closeAtx) {
4141
value += ' ' + sequence

lib/util/safe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function safe(context, input, config) {
4949

5050
positions.sort(numerical)
5151

52-
start = config.before ? 1 : 0
52+
start = config.before ? config.before.length : 0
5353
end = value.length - (config.after ? 1 : 0)
5454
index = -1
5555
length = positions.length

test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,42 @@ test('heading', function (t) {
10451045
'should serialize a with a closing sequence when `closeAtx` (content)'
10461046
)
10471047

1048+
t.equal(
1049+
to({type: 'heading', depth: 2, children: [{type: 'text', value: '# a'}]}),
1050+
'## # a\n',
1051+
'should not escape a `#` at the start of phrasing in a heading'
1052+
)
1053+
1054+
t.equal(
1055+
to({type: 'heading', depth: 2, children: [{type: 'text', value: '1) a'}]}),
1056+
'## 1) a\n',
1057+
'should not escape a `1)` at the start of phrasing in a heading'
1058+
)
1059+
1060+
t.equal(
1061+
to({type: 'heading', depth: 2, children: [{type: 'text', value: '+ a'}]}),
1062+
'## + a\n',
1063+
'should not escape a `+` at the start of phrasing in a heading'
1064+
)
1065+
1066+
t.equal(
1067+
to({type: 'heading', depth: 2, children: [{type: 'text', value: '- a'}]}),
1068+
'## - a\n',
1069+
'should not escape a `-` at the start of phrasing in a heading'
1070+
)
1071+
1072+
t.equal(
1073+
to({type: 'heading', depth: 2, children: [{type: 'text', value: '= a'}]}),
1074+
'## = a\n',
1075+
'should not escape a `=` at the start of phrasing in a heading'
1076+
)
1077+
1078+
t.equal(
1079+
to({type: 'heading', depth: 2, children: [{type: 'text', value: '> a'}]}),
1080+
'## > a\n',
1081+
'should not escape a `>` at the start of phrasing in a heading'
1082+
)
1083+
10481084
t.end()
10491085
})
10501086

0 commit comments

Comments
 (0)