Skip to content

Commit caae14a

Browse files
committed
fix: do not escape underscore inside of words
Makes escape rules for `_` more specific to prevent unnecessary escaping inside of words such as HELLO_WORLD or urls (https://some/web_site). Related to remarkjs/remark#1455
1 parent ee3b345 commit caae14a

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

lib/unsafe.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,18 @@ export const unsafe = [
130130
// Caret is not used in markdown for constructs.
131131
// An underscore can start emphasis, strong, or a thematic break.
132132
{atBreak: true, character: '_'},
133-
{character: '_', inConstruct: 'phrasing', notInConstruct: fullPhrasingSpans},
133+
{
134+
character: '_',
135+
before: '[\\s]',
136+
inConstruct: 'phrasing',
137+
notInConstruct: fullPhrasingSpans
138+
},
139+
{
140+
character: '_',
141+
after: '[\\s]',
142+
inConstruct: 'phrasing',
143+
notInConstruct: fullPhrasingSpans
144+
},
134145
// A grave accent can start code (fenced or text), or it can break out of
135146
// a grave accent code fence.
136147
{atBreak: true, character: '`'},

test/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,39 @@ test('escape', async function (t) {
38953895
}
38963896
)
38973897

3898+
await t.test('should escape underscore at word start', async function () {
3899+
assert.equal(
3900+
to({
3901+
type: 'paragraph',
3902+
children: [{type: 'text', value: '_WORLD'}]
3903+
}),
3904+
'\\_WORLD\n'
3905+
)
3906+
})
3907+
3908+
await t.test('should escape underscore at word end', async function () {
3909+
assert.equal(
3910+
to({
3911+
type: 'paragraph',
3912+
children: [{type: 'text', value: 'HELLO_'}]
3913+
}),
3914+
'HELLO\\_\n'
3915+
)
3916+
})
3917+
3918+
await t.test(
3919+
'should not escape underscore inside of word',
3920+
async function () {
3921+
assert.equal(
3922+
to({
3923+
type: 'paragraph',
3924+
children: [{type: 'text', value: 'HELLO_WORLD'}]
3925+
}),
3926+
'HELLO_WORLD\n'
3927+
)
3928+
}
3929+
)
3930+
38983931
await t.test(
38993932
'should escape what would otherwise be a heading (atx)',
39003933
async function () {
@@ -4503,6 +4536,15 @@ test('roundtrip', async function (t) {
45034536
)
45044537
})
45054538

4539+
await t.test(
4540+
'should roundtrip underscore inside of words',
4541+
async function () {
4542+
const value = 'HELLO_WORLD https://some/web_site\n'
4543+
4544+
assert.equal(to(from(value)), value)
4545+
}
4546+
)
4547+
45064548
await t.test(
45074549
'should roundtrip a sole blank line in fenced code',
45084550
async function () {

0 commit comments

Comments
 (0)