diff --git a/dev/lib/directive-container.js b/dev/lib/directive-container.js index 801757f..08fc838 100644 --- a/dev/lib/directive-container.js +++ b/dev/lib/directive-container.js @@ -23,6 +23,7 @@ export const directiveContainer = { const label = {tokenize: tokenizeLabel, partial: true} const attributes = {tokenize: tokenizeAttributes, partial: true} +const nonLazyLine = {tokenize: tokenizeNonLazyLine, partial: true} /** @type {Tokenizer} */ function tokenizeDirectiveContainer(effects, ok, nok) { @@ -93,20 +94,26 @@ function tokenizeDirectiveContainer(effects, ok, nok) { effects.exit('directiveContainerFence') if (code === codes.eof) { - effects.exit('directiveContainer') - return ok(code) + return afterOpening(code) } if (markdownLineEnding(code)) { - effects.enter(types.lineEnding) - effects.consume(code) - effects.exit(types.lineEnding) - return self.interrupt ? ok : contentStart + if (self.interrupt) { + return ok(code) + } + + return effects.attempt(nonLazyLine, contentStart, afterOpening)(code) } return nok(code) } + /** @type {State} */ + function afterOpening(code) { + effects.exit('directiveContainer') + return ok(code) + } + /** @type {State} */ function contentStart(code) { if (code === codes.eof) { @@ -157,16 +164,28 @@ function tokenizeDirectiveContainer(effects, ok, nok) { } if (markdownLineEnding(code)) { - effects.consume(code) - const t = effects.exit(types.chunkDocument) - self.parser.lazy[t.start.line] = false - return lineStart + return effects.check(nonLazyLine, nonLazyLineAfter, lineAfter)(code) } effects.consume(code) return contentContinue } + /** @type {State} */ + function nonLazyLineAfter(code) { + effects.consume(code) + const t = effects.exit(types.chunkDocument) + self.parser.lazy[t.start.line] = false + return lineStart + } + + /** @type {State} */ + function lineAfter(code) { + const t = effects.exit(types.chunkDocument) + self.parser.lazy[t.start.line] = false + return after(code) + } + /** @type {State} */ function after(code) { effects.exit('directiveContainerContent') @@ -252,3 +271,24 @@ function tokenizeAttributes(effects, ok, nok) { true ) } + +/** @type {Tokenizer} */ +function tokenizeNonLazyLine(effects, ok, nok) { + const self = this + + return start + + /** @type {State} */ + function start(code) { + assert(markdownLineEnding(code), 'expected eol') + effects.enter(types.lineEnding) + effects.consume(code) + effects.exit(types.lineEnding) + return lineStart + } + + /** @type {State} */ + function lineStart(code) { + return self.parser.lazy[self.now().line] ? nok(code) : ok(code) + } +} diff --git a/test/index.js b/test/index.js index 605a066..9390cc3 100644 --- a/test/index.js +++ b/test/index.js @@ -710,6 +710,18 @@ test('micromark-extension-directive (syntax)', (t) => { 'should support a thematic break before a leaf' ) + t.equal( + micromark('> ::a\nb', options({'*': h})), + '
\n
\n

b

', + 'should not support lazyness (1)' + ) + + t.equal( + micromark('> a\n::b', options({'*': h})), + '
\n

a

\n
\n', + 'should not support lazyness (2)' + ) + t.end() }) @@ -1032,7 +1044,7 @@ test('micromark-extension-directive (syntax)', (t) => { t.equal( micromark('>a\n:::a\nb', options()), - '
\n

a

\n
', + '
\n

a

\n
\n', 'should support a block quote before a container' ) @@ -1074,7 +1086,7 @@ test('micromark-extension-directive (syntax)', (t) => { t.equal( micromark('* a\n:::a\nb', options()), - '', + '\n', 'should support a list before a container' ) @@ -1114,6 +1126,30 @@ test('micromark-extension-directive (syntax)', (t) => { 'should support prefixed containers (4)' ) + t.equal( + micromark('> :::a\nb', options({'*': h})), + '
\n
\n

b

', + 'should not support lazyness (1)' + ) + + t.equal( + micromark('> :::a\n> b\nc', options({'*': h})), + '
\n

b

\n
\n
\n

c

', + 'should not support lazyness (2)' + ) + + t.equal( + micromark('> a\n:::b', options({'*': h})), + '
\n

a

\n
\n', + 'should not support lazyness (3)' + ) + + t.equal( + micromark('> :::a\n:::', options({'*': h})), + '
\n
\n

:::

', + 'should not support lazyness (4)' + ) + t.end() })