block-quote.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @import {
  3. * Construct,
  4. * Exiter,
  5. * State,
  6. * TokenizeContext,
  7. * Tokenizer
  8. * } from 'micromark-util-types'
  9. */
  10. import { factorySpace } from 'micromark-factory-space';
  11. import { markdownSpace } from 'micromark-util-character';
  12. /** @type {Construct} */
  13. export const blockQuote = {
  14. continuation: {
  15. tokenize: tokenizeBlockQuoteContinuation
  16. },
  17. exit,
  18. name: 'blockQuote',
  19. tokenize: tokenizeBlockQuoteStart
  20. };
  21. /**
  22. * @this {TokenizeContext}
  23. * Context.
  24. * @type {Tokenizer}
  25. */
  26. function tokenizeBlockQuoteStart(effects, ok, nok) {
  27. const self = this;
  28. return start;
  29. /**
  30. * Start of block quote.
  31. *
  32. * ```markdown
  33. * > | > a
  34. * ^
  35. * ```
  36. *
  37. * @type {State}
  38. */
  39. function start(code) {
  40. if (code === 62) {
  41. const state = self.containerState;
  42. if (!state.open) {
  43. effects.enter("blockQuote", {
  44. _container: true
  45. });
  46. state.open = true;
  47. }
  48. effects.enter("blockQuotePrefix");
  49. effects.enter("blockQuoteMarker");
  50. effects.consume(code);
  51. effects.exit("blockQuoteMarker");
  52. return after;
  53. }
  54. return nok(code);
  55. }
  56. /**
  57. * After `>`, before optional whitespace.
  58. *
  59. * ```markdown
  60. * > | > a
  61. * ^
  62. * ```
  63. *
  64. * @type {State}
  65. */
  66. function after(code) {
  67. if (markdownSpace(code)) {
  68. effects.enter("blockQuotePrefixWhitespace");
  69. effects.consume(code);
  70. effects.exit("blockQuotePrefixWhitespace");
  71. effects.exit("blockQuotePrefix");
  72. return ok;
  73. }
  74. effects.exit("blockQuotePrefix");
  75. return ok(code);
  76. }
  77. }
  78. /**
  79. * Start of block quote continuation.
  80. *
  81. * ```markdown
  82. * | > a
  83. * > | > b
  84. * ^
  85. * ```
  86. *
  87. * @this {TokenizeContext}
  88. * Context.
  89. * @type {Tokenizer}
  90. */
  91. function tokenizeBlockQuoteContinuation(effects, ok, nok) {
  92. const self = this;
  93. return contStart;
  94. /**
  95. * Start of block quote continuation.
  96. *
  97. * Also used to parse the first block quote opening.
  98. *
  99. * ```markdown
  100. * | > a
  101. * > | > b
  102. * ^
  103. * ```
  104. *
  105. * @type {State}
  106. */
  107. function contStart(code) {
  108. if (markdownSpace(code)) {
  109. // Always populated by defaults.
  110. return factorySpace(effects, contBefore, "linePrefix", self.parser.constructs.disable.null.includes('codeIndented') ? undefined : 4)(code);
  111. }
  112. return contBefore(code);
  113. }
  114. /**
  115. * At `>`, after optional whitespace.
  116. *
  117. * Also used to parse the first block quote opening.
  118. *
  119. * ```markdown
  120. * | > a
  121. * > | > b
  122. * ^
  123. * ```
  124. *
  125. * @type {State}
  126. */
  127. function contBefore(code) {
  128. return effects.attempt(blockQuote, ok, nok)(code);
  129. }
  130. }
  131. /** @type {Exiter} */
  132. function exit(effects) {
  133. effects.exit("blockQuote");
  134. }