flow.js 1.7 KB

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