Add _ to identifier names

*   Add support for underscore in names to prevent interference w/ gemoji
*   Disallow underscore at end of name to prevent interference w/ emphasis

Related to GH-651.
This commit is contained in:
Titus 2021-05-05 11:29:51 +02:00 committed by GitHub
parent fb00d9b52b
commit e3486e51ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 6 deletions

View file

@ -6,6 +6,8 @@ var asciiAlpha = require('micromark/dist/character/ascii-alpha')
var asciiAlphanumeric = require('micromark/dist/character/ascii-alphanumeric')
function createName(effects, ok, nok, nameType) {
var self = this
return start
function start(code) {
@ -19,12 +21,17 @@ function createName(effects, ok, nok, nameType) {
}
function name(code) {
if (code === 45 /* `-` */ || asciiAlphanumeric(code)) {
if (
code === 45 /* `-` */ ||
code === 95 /* `_` */ ||
asciiAlphanumeric(code)
) {
effects.consume(code)
return name
}
effects.exit(nameType)
return ok(code)
// To do next major: disallow `-` at end of name too, for consistency.
return self.previous === 95 /* `_` */ ? nok(code) : ok(code)
}
}

View file

@ -42,7 +42,13 @@ function tokenizeDirectiveContainer(effects, ok, nok) {
}
effects.exit('directiveContainerSequence')
return createName(effects, afterName, nok, 'directiveContainerName')(code)
return createName.call(
self,
effects,
afterName,
nok,
'directiveContainerName'
)(code)
}
function afterName(code) {

View file

@ -12,6 +12,8 @@ var label = {tokenize: tokenizeLabel, partial: true}
var attributes = {tokenize: tokenizeAttributes, partial: true}
function tokenizeDirectiveLeaf(effects, ok, nok) {
var self = this
return start
function start(code) {
@ -28,7 +30,7 @@ function tokenizeDirectiveLeaf(effects, ok, nok) {
if (code === 58 /* `:` */) {
effects.consume(code)
effects.exit('directiveLeafSequence')
return createName(effects, afterName, nok, 'directiveLeafName')
return createName.call(self, effects, afterName, nok, 'directiveLeafName')
}
return nok(code)

View file

@ -36,7 +36,7 @@ function tokenizeDirectiveText(effects, ok, nok) {
effects.enter('directiveTextMarker')
effects.consume(code)
effects.exit('directiveTextMarker')
return createName(effects, afterName, nok, 'directiveTextName')
return createName.call(self, effects, afterName, nok, 'directiveTextName')
}
function afterName(code) {

View file

@ -146,7 +146,8 @@ He dies.
:::
The `name` part is required. The first character must be a letter, other
characters can be alphanumerical and `-`.
characters can be alphanumerical, `-`, and `_`.
`_` cannot end a name.
The `[label]` part is optional (`:x` and `:x[]` are equivalent)†.
When used, it can include text constructs such as emphasis and so on: `x[a *b*

42
test.js
View file

@ -48,6 +48,12 @@ test('micromark-extension-directive (syntax)', function (t) {
'should not support a colon followed by a dash'
)
t.equal(
micromark(':_', options()),
'<p>:_</p>',
'should not support a colon followed by an underscore'
)
t.equal(
micromark(':a9', options()),
'<p></p>',
@ -60,12 +66,48 @@ test('micromark-extension-directive (syntax)', function (t) {
'should support a dash in a name'
)
t.equal(
micromark(':a_b', options()),
'<p></p>',
'should support an underscore in a name'
)
t.equal(
micromark(':a_', options()),
'<p>:a_</p>',
'should *not* support an underscore at the end of a name'
)
t.equal(
micromark(':a:', options()),
'<p>:a:</p>',
'should *not* support a colon right after a name'
)
t.equal(
micromark(':+1:', options()),
'<p>:+1:</p>',
'should not interfere w/ gemoji (1)'
)
t.equal(
micromark(':heart:', options()),
'<p>:heart:</p>',
'should not interfere w/ gemoji (2)'
)
t.equal(
micromark(':call_me_hand:', options()),
'<p>:call_me_hand:</p>',
'should not interfere w/ gemoji (3)'
)
t.equal(
micromark('_:directive_', options()),
'<p><em>:directive</em></p>',
'should not interfere w/ emphasis (`_`)'
)
t.equal(
micromark(':a[', options()),
'<p>[</p>',