Sha256: 0243f70d5ab7983fdfa6107e4ae6a43365f6c087b63c646c930f5d7e6fbab01f

Contents?: true

Size: 1.73 KB

Versions: 23

Compression:

Stored size: 1.73 KB

Contents

'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: I’d 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
}

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.6 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.5 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.4 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.3 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.2 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.1 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.3.5 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-5.0.0 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.3.4 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.3.3 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.3.2 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.3.1 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.3 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.2.3 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.2.2 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.2.1 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.2 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.1.9 node_modules/remark-stringify/lib/visitors/list-item.js
trusty-cms-4.1.8 node_modules/remark-stringify/lib/visitors/list-item.js