index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @import {Effects, State} from 'micromark-util-types'
  3. */
  4. import {factorySpace} from 'micromark-factory-space'
  5. import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
  6. import {types} from 'micromark-util-symbol'
  7. /**
  8. * Parse spaces and tabs.
  9. *
  10. * There is no `nok` parameter:
  11. *
  12. * * line endings or spaces in markdown are often optional, in which case this
  13. * factory can be used and `ok` will be switched to whether spaces were found
  14. * or not
  15. * * one line ending or space can be detected with
  16. * `markdownLineEndingOrSpace(code)` right before using `factoryWhitespace`
  17. *
  18. * @param {Effects} effects
  19. * Context.
  20. * @param {State} ok
  21. * State switched to when successful.
  22. * @returns {State}
  23. * Start state.
  24. */
  25. export function factoryWhitespace(effects, ok) {
  26. /** @type {boolean} */
  27. let seen
  28. return start
  29. /** @type {State} */
  30. function start(code) {
  31. if (markdownLineEnding(code)) {
  32. effects.enter(types.lineEnding)
  33. effects.consume(code)
  34. effects.exit(types.lineEnding)
  35. seen = true
  36. return start
  37. }
  38. if (markdownSpace(code)) {
  39. return factorySpace(
  40. effects,
  41. start,
  42. seen ? types.linePrefix : types.lineSuffix
  43. )(code)
  44. }
  45. return ok(code)
  46. }
  47. }