Fix lazy lines

This commit is contained in:
Titus Wormer 2021-06-20 17:00:06 +02:00
parent d40008f5c7
commit d9489c7489
No known key found for this signature in database
GPG key ID: E6E581152ED04E2E
2 changed files with 88 additions and 12 deletions

View file

@ -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,14 +164,26 @@ function tokenizeDirectiveContainer(effects, ok, nok) {
}
if (markdownLineEnding(code)) {
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
}
effects.consume(code)
return contentContinue
/** @type {State} */
function lineAfter(code) {
const t = effects.exit(types.chunkDocument)
self.parser.lazy[t.start.line] = false
return after(code)
}
/** @type {State} */
@ -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)
}
}

View file

@ -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})),
'<blockquote><a></a>\n</blockquote>\n<p>b</p>',
'should not support lazyness (1)'
)
t.equal(
micromark('> a\n::b', options({'*': h})),
'<blockquote>\n<p>a</p>\n</blockquote>\n<b></b>',
'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()),
'<blockquote>\n<p>a</p>\n</blockquote>',
'<blockquote>\n<p>a</p>\n</blockquote>\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()),
'<ul>\n<li>a</li>\n</ul>',
'<ul>\n<li>a</li>\n</ul>\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})),
'<blockquote><a></a>\n</blockquote>\n<p>b</p>',
'should not support lazyness (1)'
)
t.equal(
micromark('> :::a\n> b\nc', options({'*': h})),
'<blockquote><a>\n<p>b</p>\n</a>\n</blockquote>\n<p>c</p>',
'should not support lazyness (2)'
)
t.equal(
micromark('> a\n:::b', options({'*': h})),
'<blockquote>\n<p>a</p>\n</blockquote>\n<b></b>',
'should not support lazyness (3)'
)
t.equal(
micromark('> :::a\n:::', options({'*': h})),
'<blockquote><a></a>\n</blockquote>\n<p>:::</p>',
'should not support lazyness (4)'
)
t.end()
})