blob: ede130e80dbc84e2f9f7a4ad11dc6e5a1cb220f9 [file] [log] [blame]
'use strict'
module.exports = block
var lineFeed = '\n'
var blank = lineFeed + lineFeed
var triple = blank + lineFeed
var comment = blank + '<!---->' + blank
// Stringify a block node with block children (e.g., `root` or `blockquote`).
// Knows about code following a list, or adjacent lists with similar bullets,
// and places an extra line feed between them.
function block(node) {
var self = this
var options = self.options
var fences = options.fences
var gap = options.commonmark ? comment : triple
var values = []
var children = node.children
var length = children.length
var index = -1
var prev
var child
while (++index < length) {
prev = child
child = children[index]
if (prev) {
// A list preceding another list that are equally ordered, or a
// list preceding an indented code block, need a gap between them,
// so as not to see them as one list, or content of the list,
// respectively.
//
// In commonmark, only something that breaks both up can do that,
// so we opt for an empty, invisible comment. In other flavours,
// two blank lines are fine.
if (
prev.type === 'list' &&
((child.type === 'list' && prev.ordered === child.ordered) ||
(child.type === 'code' && (!child.lang && !fences)))
) {
values.push(gap)
} else {
values.push(blank)
}
}
values.push(self.visit(child, node))
}
return values.join('')
}