content.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @import {
  3. * InitialConstruct,
  4. * Initializer,
  5. * State,
  6. * TokenizeContext,
  7. * Token
  8. * } from 'micromark-util-types'
  9. */
  10. import { factorySpace } from 'micromark-factory-space';
  11. import { markdownLineEnding } from 'micromark-util-character';
  12. /** @type {InitialConstruct} */
  13. export const content = {
  14. tokenize: initializeContent
  15. };
  16. /**
  17. * @this {TokenizeContext}
  18. * Context.
  19. * @type {Initializer}
  20. * Content.
  21. */
  22. function initializeContent(effects) {
  23. const contentStart = effects.attempt(this.parser.constructs.contentInitial, afterContentStartConstruct, paragraphInitial);
  24. /** @type {Token} */
  25. let previous;
  26. return contentStart;
  27. /** @type {State} */
  28. function afterContentStartConstruct(code) {
  29. if (code === null) {
  30. effects.consume(code);
  31. return;
  32. }
  33. effects.enter("lineEnding");
  34. effects.consume(code);
  35. effects.exit("lineEnding");
  36. return factorySpace(effects, contentStart, "linePrefix");
  37. }
  38. /** @type {State} */
  39. function paragraphInitial(code) {
  40. effects.enter("paragraph");
  41. return lineStart(code);
  42. }
  43. /** @type {State} */
  44. function lineStart(code) {
  45. const token = effects.enter("chunkText", {
  46. contentType: "text",
  47. previous
  48. });
  49. if (previous) {
  50. previous.next = token;
  51. }
  52. previous = token;
  53. return data(code);
  54. }
  55. /** @type {State} */
  56. function data(code) {
  57. if (code === null) {
  58. effects.exit("chunkText");
  59. effects.exit("paragraph");
  60. effects.consume(code);
  61. return;
  62. }
  63. if (markdownLineEnding(code)) {
  64. effects.consume(code);
  65. effects.exit("chunkText");
  66. return lineStart;
  67. }
  68. // Data.
  69. effects.consume(code);
  70. return data;
  71. }
  72. }