You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

'use strict'
var repeat = require('repeat-string')
var pad = require('../util/pad')
module.exports = listItem
var lineFeed = '\n'
var space = ' '
var leftSquareBracket = '['
var rightSquareBracket = ']'
var lowercaseX = 'x'
var ceil = Math.ceil
var blank = lineFeed + lineFeed
var tabSize = 4
// Stringify a list item.
//
// Prefixes the content with a checked checkbox when `checked: true`:
//
// ```markdown
// [x] foo
// ```
//
// Prefixes the content with an unchecked checkbox when `checked: false`:
//
// ```markdown
// [ ] foo
// ```
function listItem(node, parent, position, bullet) {
var self = this
var style = self.options.listItemIndent
var marker = bullet || self.options.bullet
var spread = node.spread == null ? true : node.spread
var checked = node.checked
var children = node.children
var length = children.length
var values = []
var index = -1
var value
var indent
var spacing
while (++index < length) {
values[index] = self.visit(children[index], node)
}
value = values.join(spread ? blank : lineFeed)
if (typeof checked === 'boolean') {
// Note: Id like to be able to only add the space between the check and
// the value, but unfortunately github does not support empty list-items
// with a checkbox :(
value =
leftSquareBracket +
(checked ? lowercaseX : space) +
rightSquareBracket +
space +
value
}
if (style === '1' || (style === 'mixed' && value.indexOf(lineFeed) === -1)) {
indent = marker.length + 1
spacing = space
} else {
indent = ceil((marker.length + 1) / tabSize) * tabSize
spacing = repeat(space, indent - marker.length)
}
return value
? marker + spacing + pad(value, indent / tabSize).slice(indent)
: marker
}