flow.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @import {
  3. * InitialConstruct,
  4. * Initializer,
  5. * State,
  6. * TokenizeContext
  7. * } from 'micromark-util-types'
  8. */
  9. import {ok as assert} from 'devlop'
  10. import {blankLine, content} from 'micromark-core-commonmark'
  11. import {factorySpace} from 'micromark-factory-space'
  12. import {markdownLineEnding} from 'micromark-util-character'
  13. import {codes, types} from 'micromark-util-symbol'
  14. /** @type {InitialConstruct} */
  15. export const flow = {tokenize: initializeFlow}
  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,
  27. atBlankEnding,
  28. // Try to parse initial flow (essentially, only code).
  29. effects.attempt(
  30. this.parser.constructs.flowInitial,
  31. afterConstruct,
  32. factorySpace(
  33. effects,
  34. effects.attempt(
  35. this.parser.constructs.flow,
  36. afterConstruct,
  37. effects.attempt(content, afterConstruct)
  38. ),
  39. types.linePrefix
  40. )
  41. )
  42. )
  43. return initial
  44. /** @type {State} */
  45. function atBlankEnding(code) {
  46. assert(
  47. code === codes.eof || markdownLineEnding(code),
  48. 'expected eol or eof'
  49. )
  50. if (code === codes.eof) {
  51. effects.consume(code)
  52. return
  53. }
  54. effects.enter(types.lineEndingBlank)
  55. effects.consume(code)
  56. effects.exit(types.lineEndingBlank)
  57. self.currentConstruct = undefined
  58. return initial
  59. }
  60. /** @type {State} */
  61. function afterConstruct(code) {
  62. assert(
  63. code === codes.eof || markdownLineEnding(code),
  64. 'expected eol or eof'
  65. )
  66. if (code === codes.eof) {
  67. effects.consume(code)
  68. return
  69. }
  70. effects.enter(types.lineEnding)
  71. effects.consume(code)
  72. effects.exit(types.lineEnding)
  73. self.currentConstruct = undefined
  74. return initial
  75. }
  76. }