index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @typedef {import('micromark-util-types').Effects} Effects
  3. * @typedef {import('micromark-util-types').State} State
  4. */
  5. import {factorySpace} from 'micromark-factory-space'
  6. import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
  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('lineEnding')
  33. effects.consume(code)
  34. effects.exit('lineEnding')
  35. seen = true
  36. return start
  37. }
  38. if (markdownSpace(code)) {
  39. return factorySpace(
  40. effects,
  41. start,
  42. seen ? 'linePrefix' : 'lineSuffix'
  43. )(code)
  44. }
  45. return ok(code)
  46. }
  47. }