index.js 1.2 KB

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