code-text.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @import {
  3. * Construct,
  4. * Previous,
  5. * Resolver,
  6. * State,
  7. * TokenizeContext,
  8. * Tokenizer,
  9. * Token
  10. * } from 'micromark-util-types'
  11. */
  12. import { markdownLineEnding } from 'micromark-util-character';
  13. /** @type {Construct} */
  14. export const codeText = {
  15. name: 'codeText',
  16. previous,
  17. resolve: resolveCodeText,
  18. tokenize: tokenizeCodeText
  19. };
  20. // To do: next major: don’t resolve, like `markdown-rs`.
  21. /** @type {Resolver} */
  22. function resolveCodeText(events) {
  23. let tailExitIndex = events.length - 4;
  24. let headEnterIndex = 3;
  25. /** @type {number} */
  26. let index;
  27. /** @type {number | undefined} */
  28. let enter;
  29. // If we start and end with an EOL or a space.
  30. if ((events[headEnterIndex][1].type === "lineEnding" || events[headEnterIndex][1].type === 'space') && (events[tailExitIndex][1].type === "lineEnding" || events[tailExitIndex][1].type === 'space')) {
  31. index = headEnterIndex;
  32. // And we have data.
  33. while (++index < tailExitIndex) {
  34. if (events[index][1].type === "codeTextData") {
  35. // Then we have padding.
  36. events[headEnterIndex][1].type = "codeTextPadding";
  37. events[tailExitIndex][1].type = "codeTextPadding";
  38. headEnterIndex += 2;
  39. tailExitIndex -= 2;
  40. break;
  41. }
  42. }
  43. }
  44. // Merge adjacent spaces and data.
  45. index = headEnterIndex - 1;
  46. tailExitIndex++;
  47. while (++index <= tailExitIndex) {
  48. if (enter === undefined) {
  49. if (index !== tailExitIndex && events[index][1].type !== "lineEnding") {
  50. enter = index;
  51. }
  52. } else if (index === tailExitIndex || events[index][1].type === "lineEnding") {
  53. events[enter][1].type = "codeTextData";
  54. if (index !== enter + 2) {
  55. events[enter][1].end = events[index - 1][1].end;
  56. events.splice(enter + 2, index - enter - 2);
  57. tailExitIndex -= index - enter - 2;
  58. index = enter + 2;
  59. }
  60. enter = undefined;
  61. }
  62. }
  63. return events;
  64. }
  65. /**
  66. * @this {TokenizeContext}
  67. * Context.
  68. * @type {Previous}
  69. */
  70. function previous(code) {
  71. // If there is a previous code, there will always be a tail.
  72. return code !== 96 || this.events[this.events.length - 1][1].type === "characterEscape";
  73. }
  74. /**
  75. * @this {TokenizeContext}
  76. * Context.
  77. * @type {Tokenizer}
  78. */
  79. function tokenizeCodeText(effects, ok, nok) {
  80. const self = this;
  81. let sizeOpen = 0;
  82. /** @type {number} */
  83. let size;
  84. /** @type {Token} */
  85. let token;
  86. return start;
  87. /**
  88. * Start of code (text).
  89. *
  90. * ```markdown
  91. * > | `a`
  92. * ^
  93. * > | \`a`
  94. * ^
  95. * ```
  96. *
  97. * @type {State}
  98. */
  99. function start(code) {
  100. effects.enter("codeText");
  101. effects.enter("codeTextSequence");
  102. return sequenceOpen(code);
  103. }
  104. /**
  105. * In opening sequence.
  106. *
  107. * ```markdown
  108. * > | `a`
  109. * ^
  110. * ```
  111. *
  112. * @type {State}
  113. */
  114. function sequenceOpen(code) {
  115. if (code === 96) {
  116. effects.consume(code);
  117. sizeOpen++;
  118. return sequenceOpen;
  119. }
  120. effects.exit("codeTextSequence");
  121. return between(code);
  122. }
  123. /**
  124. * Between something and something else.
  125. *
  126. * ```markdown
  127. * > | `a`
  128. * ^^
  129. * ```
  130. *
  131. * @type {State}
  132. */
  133. function between(code) {
  134. // EOF.
  135. if (code === null) {
  136. return nok(code);
  137. }
  138. // To do: next major: don’t do spaces in resolve, but when compiling,
  139. // like `markdown-rs`.
  140. // Tabs don’t work, and virtual spaces don’t make sense.
  141. if (code === 32) {
  142. effects.enter('space');
  143. effects.consume(code);
  144. effects.exit('space');
  145. return between;
  146. }
  147. // Closing fence? Could also be data.
  148. if (code === 96) {
  149. token = effects.enter("codeTextSequence");
  150. size = 0;
  151. return sequenceClose(code);
  152. }
  153. if (markdownLineEnding(code)) {
  154. effects.enter("lineEnding");
  155. effects.consume(code);
  156. effects.exit("lineEnding");
  157. return between;
  158. }
  159. // Data.
  160. effects.enter("codeTextData");
  161. return data(code);
  162. }
  163. /**
  164. * In data.
  165. *
  166. * ```markdown
  167. * > | `a`
  168. * ^
  169. * ```
  170. *
  171. * @type {State}
  172. */
  173. function data(code) {
  174. if (code === null || code === 32 || code === 96 || markdownLineEnding(code)) {
  175. effects.exit("codeTextData");
  176. return between(code);
  177. }
  178. effects.consume(code);
  179. return data;
  180. }
  181. /**
  182. * In closing sequence.
  183. *
  184. * ```markdown
  185. * > | `a`
  186. * ^
  187. * ```
  188. *
  189. * @type {State}
  190. */
  191. function sequenceClose(code) {
  192. // More.
  193. if (code === 96) {
  194. effects.consume(code);
  195. size++;
  196. return sequenceClose;
  197. }
  198. // Done!
  199. if (size === sizeOpen) {
  200. effects.exit("codeTextSequence");
  201. effects.exit("codeText");
  202. return ok(code);
  203. }
  204. // More or less accents: mark as data.
  205. token.type = "codeTextData";
  206. return data(code);
  207. }
  208. }