content.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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').Token} Token
  6. * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
  7. */
  8. import {factorySpace} from 'micromark-factory-space'
  9. import {markdownLineEnding} from 'micromark-util-character'
  10. /** @type {InitialConstruct} */
  11. export const content = {
  12. tokenize: initializeContent
  13. }
  14. /**
  15. * @this {TokenizeContext}
  16. * @type {Initializer}
  17. */
  18. function initializeContent(effects) {
  19. const contentStart = effects.attempt(
  20. this.parser.constructs.contentInitial,
  21. afterContentStartConstruct,
  22. paragraphInitial
  23. )
  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. }