break.js 705 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * @import {Break, Parents} from 'mdast'
  3. * @import {Info, State} from 'mdast-util-to-markdown'
  4. */
  5. import {patternInScope} from '../util/pattern-in-scope.js'
  6. /**
  7. * @param {Break} _
  8. * @param {Parents | undefined} _1
  9. * @param {State} state
  10. * @param {Info} info
  11. * @returns {string}
  12. */
  13. export function hardBreak(_, _1, state, info) {
  14. let index = -1
  15. while (++index < state.unsafe.length) {
  16. // If we can’t put eols in this construct (setext headings, tables), use a
  17. // space instead.
  18. if (
  19. state.unsafe[index].character === '\n' &&
  20. patternInScope(state.stack, state.unsafe[index])
  21. ) {
  22. return /[ \t]/.test(info.before) ? '' : ' '
  23. }
  24. }
  25. return '\\\n'
  26. }