103 lines
2.4 KiB
JavaScript
103 lines
2.4 KiB
JavaScript
import {factorySpace} from 'micromark-factory-space'
|
|
import {markdownLineEnding} from 'micromark-util-character'
|
|
import {factoryAttributes} from './factory-attributes.js'
|
|
import {factoryLabel} from './factory-label.js'
|
|
import {factoryName} from './factory-name.js'
|
|
|
|
export const directiveLeaf = {
|
|
tokenize: tokenizeDirectiveLeaf
|
|
}
|
|
|
|
const label = {tokenize: tokenizeLabel, partial: true}
|
|
const attributes = {tokenize: tokenizeAttributes, partial: true}
|
|
|
|
function tokenizeDirectiveLeaf(effects, ok, nok) {
|
|
const self = this
|
|
|
|
return start
|
|
|
|
function start(code) {
|
|
/* istanbul ignore if - handled by mm */
|
|
if (code !== 58 /* `:` */) throw new Error('expected `:`')
|
|
|
|
effects.enter('directiveLeaf')
|
|
effects.enter('directiveLeafSequence')
|
|
effects.consume(code)
|
|
return inStart
|
|
}
|
|
|
|
function inStart(code) {
|
|
if (code === 58 /* `:` */) {
|
|
effects.consume(code)
|
|
effects.exit('directiveLeafSequence')
|
|
return factoryName.call(
|
|
self,
|
|
effects,
|
|
afterName,
|
|
nok,
|
|
'directiveLeafName'
|
|
)
|
|
}
|
|
|
|
return nok(code)
|
|
}
|
|
|
|
function afterName(code) {
|
|
return code === 91 /* `[` */
|
|
? effects.attempt(label, afterLabel, afterLabel)(code)
|
|
: afterLabel(code)
|
|
}
|
|
|
|
function afterLabel(code) {
|
|
return code === 123 /* `{` */
|
|
? effects.attempt(attributes, afterAttributes, afterAttributes)(code)
|
|
: afterAttributes(code)
|
|
}
|
|
|
|
function afterAttributes(code) {
|
|
return factorySpace(effects, end, 'whitespace')(code)
|
|
}
|
|
|
|
function end(code) {
|
|
if (code === null || markdownLineEnding(code)) {
|
|
effects.exit('directiveLeaf')
|
|
return ok(code)
|
|
}
|
|
|
|
return nok(code)
|
|
}
|
|
}
|
|
|
|
function tokenizeLabel(effects, ok, nok) {
|
|
// Always a `[`
|
|
return factoryLabel(
|
|
effects,
|
|
ok,
|
|
nok,
|
|
'directiveLeafLabel',
|
|
'directiveLeafLabelMarker',
|
|
'directiveLeafLabelString',
|
|
true
|
|
)
|
|
}
|
|
|
|
function tokenizeAttributes(effects, ok, nok) {
|
|
// Always a `{`
|
|
return factoryAttributes(
|
|
effects,
|
|
ok,
|
|
nok,
|
|
'directiveLeafAttributes',
|
|
'directiveLeafAttributesMarker',
|
|
'directiveLeafAttribute',
|
|
'directiveLeafAttributeId',
|
|
'directiveLeafAttributeClass',
|
|
'directiveLeafAttributeName',
|
|
'directiveLeafAttributeInitializerMarker',
|
|
'directiveLeafAttributeValueLiteral',
|
|
'directiveLeafAttributeValue',
|
|
'directiveLeafAttributeValueMarker',
|
|
'directiveLeafAttributeValueData',
|
|
true
|
|
)
|
|
}
|