join.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * @typedef {import('./types.js').Join} Join
  3. */
  4. import {formatCodeAsIndented} from './util/format-code-as-indented.js'
  5. import {formatHeadingAsSetext} from './util/format-heading-as-setext.js'
  6. /** @type {Array<Join>} */
  7. export const join = [joinDefaults]
  8. /** @type {Join} */
  9. function joinDefaults(left, right, parent, state) {
  10. // Indented code after list or another indented code.
  11. if (
  12. right.type === 'code' &&
  13. formatCodeAsIndented(right, state) &&
  14. (left.type === 'list' ||
  15. (left.type === right.type && formatCodeAsIndented(left, state)))
  16. ) {
  17. return false
  18. }
  19. // Join children of a list or an item.
  20. // In which case, `parent` has a `spread` field.
  21. if ('spread' in parent && typeof parent.spread === 'boolean') {
  22. if (
  23. left.type === 'paragraph' &&
  24. // Two paragraphs.
  25. (left.type === right.type ||
  26. right.type === 'definition' ||
  27. // Paragraph followed by a setext heading.
  28. (right.type === 'heading' && formatHeadingAsSetext(right, state)))
  29. ) {
  30. return
  31. }
  32. return parent.spread ? 1 : 0
  33. }
  34. }