break.js 791 B

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