flow.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @import {
  3. * InitialConstruct,
  4. * Initializer,
  5. * State,
  6. * TokenizeContext
  7. * } from 'micromark-util-types'
  8. */
  9. import { blankLine, content } from 'micromark-core-commonmark';
  10. import { factorySpace } from 'micromark-factory-space';
  11. import { markdownLineEnding } from 'micromark-util-character';
  12. /** @type {InitialConstruct} */
  13. export const flow = {
  14. tokenize: initializeFlow
  15. };
  16. /**
  17. * @this {TokenizeContext}
  18. * Self.
  19. * @type {Initializer}
  20. * Initializer.
  21. */
  22. function initializeFlow(effects) {
  23. const self = this;
  24. const initial = effects.attempt(
  25. // Try to parse a blank line.
  26. blankLine, atBlankEnding,
  27. // Try to parse initial flow (essentially, only code).
  28. effects.attempt(this.parser.constructs.flowInitial, afterConstruct, factorySpace(effects, effects.attempt(this.parser.constructs.flow, afterConstruct, effects.attempt(content, afterConstruct)), "linePrefix")));
  29. return initial;
  30. /** @type {State} */
  31. function atBlankEnding(code) {
  32. if (code === null) {
  33. effects.consume(code);
  34. return;
  35. }
  36. effects.enter("lineEndingBlank");
  37. effects.consume(code);
  38. effects.exit("lineEndingBlank");
  39. self.currentConstruct = undefined;
  40. return initial;
  41. }
  42. /** @type {State} */
  43. function afterConstruct(code) {
  44. if (code === null) {
  45. effects.consume(code);
  46. return;
  47. }
  48. effects.enter("lineEnding");
  49. effects.consume(code);
  50. effects.exit("lineEnding");
  51. self.currentConstruct = undefined;
  52. return initial;
  53. }
  54. }