block.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. module.exports = block;
  3. /* Stringify a block node with block children (e.g., `root`
  4. * or `blockquote`).
  5. * Knows about code following a list, or adjacent lists
  6. * with similar bullets, and places an extra newline
  7. * between them. */
  8. function block(node) {
  9. var self = this;
  10. var values = [];
  11. var children = node.children;
  12. var length = children.length;
  13. var index = -1;
  14. var child;
  15. var prev;
  16. while (++index < length) {
  17. child = children[index];
  18. if (prev) {
  19. /* Duplicate nodes, such as a list
  20. * directly following another list,
  21. * often need multiple new lines.
  22. *
  23. * Additionally, code blocks following a list
  24. * might easily be mistaken for a paragraph
  25. * in the list itself. */
  26. if (child.type === prev.type && prev.type === 'list') {
  27. values.push(prev.ordered === child.ordered ? '\n\n\n' : '\n\n');
  28. } else if (prev.type === 'list' && child.type === 'code' && !child.lang) {
  29. values.push('\n\n\n');
  30. } else {
  31. values.push('\n\n');
  32. }
  33. }
  34. values.push(self.visit(child, node));
  35. prev = child;
  36. }
  37. return values.join('');
  38. }