217 lines
5.2 KiB
JavaScript
217 lines
5.2 KiB
JavaScript
import {factorySpace} from 'micromark-factory-space'
|
|
import {markdownLineEnding} from 'micromark-util-character'
|
|
// . import prefixSize from 'micromark/dist/util/prefix-size'
|
|
import {factoryAttributes} from './factory-attributes.js'
|
|
import {factoryLabel} from './factory-label.js'
|
|
import {factoryName} from './factory-name.js'
|
|
|
|
export const directiveContainer = {
|
|
tokenize: tokenizeDirectiveContainer,
|
|
concrete: true
|
|
}
|
|
|
|
var label = {tokenize: tokenizeLabel, partial: true}
|
|
var attributes = {tokenize: tokenizeAttributes, partial: true}
|
|
|
|
function tokenizeDirectiveContainer(effects, ok, nok) {
|
|
var self = this
|
|
const tail = self.events[self.events.length - 1]
|
|
const initialSize =
|
|
tail && tail[1].type === 'linePrefix'
|
|
? tail[2].sliceSerialize(tail[1], true).length
|
|
: 0
|
|
var sizeOpen = 0
|
|
var previous
|
|
|
|
return start
|
|
|
|
function start(code) {
|
|
/* istanbul ignore if - handled by mm */
|
|
if (code !== 58 /* `:` */) throw new Error('expected `:`')
|
|
effects.enter('directiveContainer')
|
|
effects.enter('directiveContainerFence')
|
|
effects.enter('directiveContainerSequence')
|
|
return sequenceOpen(code)
|
|
}
|
|
|
|
function sequenceOpen(code) {
|
|
if (code === 58 /* `:` */) {
|
|
effects.consume(code)
|
|
sizeOpen++
|
|
return sequenceOpen
|
|
}
|
|
|
|
if (sizeOpen < 3) {
|
|
return nok(code)
|
|
}
|
|
|
|
effects.exit('directiveContainerSequence')
|
|
return factoryName.call(
|
|
self,
|
|
effects,
|
|
afterName,
|
|
nok,
|
|
'directiveContainerName'
|
|
)(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, openAfter, 'whitespace')(code)
|
|
}
|
|
|
|
function openAfter(code) {
|
|
effects.exit('directiveContainerFence')
|
|
|
|
if (code === null) {
|
|
effects.exit('directiveContainer')
|
|
return ok(code)
|
|
}
|
|
|
|
if (markdownLineEnding(code)) {
|
|
effects.enter('lineEnding')
|
|
effects.consume(code)
|
|
effects.exit('lineEnding')
|
|
return self.interrupt ? ok : contentStart
|
|
}
|
|
|
|
return nok(code)
|
|
}
|
|
|
|
function contentStart(code) {
|
|
if (code === null) {
|
|
effects.exit('directiveContainer')
|
|
return ok(code)
|
|
}
|
|
|
|
effects.enter('directiveContainerContent')
|
|
return lineStart(code)
|
|
}
|
|
|
|
function lineStart(code) {
|
|
if (code === null) {
|
|
return after(code)
|
|
}
|
|
|
|
return effects.attempt(
|
|
{tokenize: tokenizeClosingFence, partial: true},
|
|
after,
|
|
initialSize
|
|
? factorySpace(effects, chunkStart, 'linePrefix', initialSize + 1)
|
|
: chunkStart
|
|
)(code)
|
|
}
|
|
|
|
function chunkStart(code) {
|
|
var token
|
|
|
|
if (code === null) {
|
|
return after(code)
|
|
}
|
|
|
|
token = effects.enter('chunkDocument', {contentType: 'document', previous})
|
|
if (previous) previous.next = token
|
|
previous = token
|
|
return contentContinue(code)
|
|
}
|
|
|
|
function contentContinue(code) {
|
|
if (code === null) {
|
|
effects.exit('chunkDocument')
|
|
return after(code)
|
|
}
|
|
|
|
if (markdownLineEnding(code)) {
|
|
effects.consume(code)
|
|
effects.exit('chunkDocument')
|
|
return lineStart
|
|
}
|
|
|
|
effects.consume(code)
|
|
return contentContinue
|
|
}
|
|
|
|
function after(code) {
|
|
effects.exit('directiveContainerContent')
|
|
effects.exit('directiveContainer')
|
|
return ok(code)
|
|
}
|
|
|
|
function tokenizeClosingFence(effects, ok, nok) {
|
|
var size = 0
|
|
|
|
return factorySpace(effects, closingPrefixAfter, 'linePrefix', 4)
|
|
|
|
function closingPrefixAfter(code) {
|
|
effects.enter('directiveContainerFence')
|
|
effects.enter('directiveContainerSequence')
|
|
return closingSequence(code)
|
|
}
|
|
|
|
function closingSequence(code) {
|
|
if (code === 58 /* `:` */) {
|
|
effects.consume(code)
|
|
size++
|
|
return closingSequence
|
|
}
|
|
|
|
if (size < sizeOpen) return nok(code)
|
|
effects.exit('directiveContainerSequence')
|
|
return factorySpace(effects, closingSequenceEnd, 'whitespace')(code)
|
|
}
|
|
|
|
function closingSequenceEnd(code) {
|
|
if (code === null || markdownLineEnding(code)) {
|
|
effects.exit('directiveContainerFence')
|
|
return ok(code)
|
|
}
|
|
|
|
return nok(code)
|
|
}
|
|
}
|
|
}
|
|
|
|
function tokenizeLabel(effects, ok, nok) {
|
|
// Always a `[`
|
|
return factoryLabel(
|
|
effects,
|
|
ok,
|
|
nok,
|
|
'directiveContainerLabel',
|
|
'directiveContainerLabelMarker',
|
|
'directiveContainerLabelString',
|
|
true
|
|
)
|
|
}
|
|
|
|
function tokenizeAttributes(effects, ok, nok) {
|
|
// Always a `{`
|
|
return factoryAttributes(
|
|
effects,
|
|
ok,
|
|
nok,
|
|
'directiveContainerAttributes',
|
|
'directiveContainerAttributesMarker',
|
|
'directiveContainerAttribute',
|
|
'directiveContainerAttributeId',
|
|
'directiveContainerAttributeClass',
|
|
'directiveContainerAttributeName',
|
|
'directiveContainerAttributeInitializerMarker',
|
|
'directiveContainerAttributeValueLiteral',
|
|
'directiveContainerAttributeValue',
|
|
'directiveContainerAttributeValueMarker',
|
|
'directiveContainerAttributeValueData',
|
|
true
|
|
)
|
|
}
|