list-item.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict'
  2. module.exports = listItem
  3. var u = require('unist-builder')
  4. var wrap = require('../wrap')
  5. var all = require('../all')
  6. function listItem(h, node, parent) {
  7. var children = node.children
  8. var head = children[0]
  9. var props = {}
  10. var single = false
  11. var result
  12. var container
  13. if (
  14. (!parent || !parent.loose) &&
  15. children.length === 1 &&
  16. head.type === 'paragraph'
  17. ) {
  18. single = true
  19. }
  20. result = all(h, single ? head : node)
  21. if (typeof node.checked === 'boolean') {
  22. if (!single && (!head || head.type !== 'paragraph')) {
  23. result.unshift(h(null, 'p', []))
  24. }
  25. container = single ? result : result[0].children
  26. if (container.length !== 0) {
  27. container.unshift(u('text', ' '))
  28. }
  29. container.unshift(
  30. h(null, 'input', {
  31. type: 'checkbox',
  32. checked: node.checked,
  33. disabled: true
  34. })
  35. )
  36. // According to github-markdown-css, this class hides bullet.
  37. props.className = ['task-list-item']
  38. }
  39. if (!single && result.length !== 0) {
  40. result = wrap(result, true)
  41. }
  42. return h(node, 'li', props, result)
  43. }