Skip to content

Commit

Permalink
Add handling of laziness
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 16, 2021
1 parent e93e51f commit 5a13899
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
54 changes: 42 additions & 12 deletions dev/lib/math-flow.js
Expand Up @@ -17,6 +17,9 @@ export const mathFlow = {
concrete: true
}

/** @type {Construct} */
const nonLazyLine = {tokenize: tokenizeNonLazyLine, partial: true}

/** @type {Tokenizer} */
function tokenizeMathFenced(effects, ok, nok) {
const self = this
Expand Down Expand Up @@ -79,26 +82,32 @@ function tokenizeMathFenced(effects, ok, nok) {
/** @type {State} */
function openAfter(code) {
effects.exit('mathFlowFence')
return self.interrupt ? ok(code) : content(code)
return self.interrupt ? ok(code) : contentStart(code)
}

/** @type {State} */
function content(code) {
function contentStart(code) {
if (code === codes.eof) {
return after(code)
}

if (markdownLineEnding(code)) {
effects.enter(types.lineEnding)
effects.consume(code)
effects.exit(types.lineEnding)
return effects.attempt(
{tokenize: tokenizeClosingFence, partial: true},
after,
initialSize
? factorySpace(effects, content, types.linePrefix, initialSize + 1)
: content
)
nonLazyLine,
effects.attempt(
{tokenize: tokenizeClosingFence, partial: true},
after,
initialSize
? factorySpace(
effects,
contentStart,
types.linePrefix,
initialSize + 1
)
: contentStart
),
after
)(code)
}

effects.enter('mathFlowValue')
Expand All @@ -109,7 +118,7 @@ function tokenizeMathFenced(effects, ok, nok) {
function contentContinue(code) {
if (code === codes.eof || markdownLineEnding(code)) {
effects.exit('mathFlowValue')
return content(code)
return contentStart(code)
}

effects.consume(code)
Expand Down Expand Up @@ -164,3 +173,24 @@ function tokenizeMathFenced(effects, ok, nok) {
}
}
}

/** @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)
}
}
37 changes: 37 additions & 0 deletions test/index.js
Expand Up @@ -281,5 +281,42 @@ test('markdown -> html (micromark)', (t) => {
'should support options'
)

t.equal(
micromark('> $$\na\n$$', {
extensions: [syntax],
htmlExtensions: [html()]
}),
'<blockquote>\n<div class="math math-display">' +
katex.renderToString('', {displayMode: true}) +
'</div>\n</blockquote>\n<p>a</p>\n<div class="math math-display">' +
katex.renderToString('', {displayMode: true}) +
'</div>',
'should not support laziness (1)'
)

t.equal(
micromark('> $$\n> a\n$$', {
extensions: [syntax],
htmlExtensions: [html()]
}),
'<blockquote>\n<div class="math math-display">' +
katex.renderToString('a', {displayMode: true}) +
'</div>\n</blockquote>\n<div class="math math-display">' +
katex.renderToString('', {displayMode: true}) +
'</div>',
'should not support laziness (2)'
)

t.equal(
micromark('a\n> $$', {
extensions: [syntax],
htmlExtensions: [html()]
}),
'<p>a</p>\n<blockquote>\n<div class="math math-display">' +
katex.renderToString('', {displayMode: true}) +
'</div>\n</blockquote>',
'should not support laziness (3)'
)

t.end()
})

0 comments on commit 5a13899

Please sign in to comment.