list-item.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. var repeat = require('repeat-string');
  3. var pad = require('../util/pad');
  4. module.exports = listItem;
  5. /* Which checkbox to use. */
  6. var CHECKBOX_MAP = {
  7. undefined: '',
  8. null: '',
  9. true: '[x] ',
  10. false: '[ ] '
  11. };
  12. /* Stringify a list item.
  13. *
  14. * Prefixes the content with a checked checkbox when
  15. * `checked: true`:
  16. *
  17. * [x] foo
  18. *
  19. * Prefixes the content with an unchecked checkbox when
  20. * `checked: false`:
  21. *
  22. * [ ] foo
  23. */
  24. function listItem(node, parent, position, bullet) {
  25. var self = this;
  26. var style = self.options.listItemIndent;
  27. var loose = node.loose;
  28. var children = node.children;
  29. var length = children.length;
  30. var values = [];
  31. var index = -1;
  32. var value;
  33. var indent;
  34. var spacing;
  35. while (++index < length) {
  36. values[index] = self.visit(children[index], node);
  37. }
  38. value = CHECKBOX_MAP[node.checked] + values.join(loose ? '\n\n' : '\n');
  39. if (style === '1' || (style === 'mixed' && value.indexOf('\n') === -1)) {
  40. indent = bullet.length + 1;
  41. spacing = ' ';
  42. } else {
  43. indent = Math.ceil((bullet.length + 1) / 4) * 4;
  44. spacing = repeat(' ', indent - bullet.length);
  45. }
  46. value = bullet + spacing + pad(value, indent / 4).slice(indent);
  47. if (loose && parent.children.length - 1 !== position) {
  48. value += '\n';
  49. }
  50. return value;
  51. }