index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @typedef {import('micromark-util-types').Code} Code
  3. * @typedef {import('micromark-util-types').Effects} Effects
  4. * @typedef {import('micromark-util-types').State} State
  5. * @typedef {import('micromark-util-types').TokenType} TokenType
  6. */
  7. import {factorySpace} from 'micromark-factory-space'
  8. import {markdownLineEnding} from 'micromark-util-character'
  9. /**
  10. * Parse titles.
  11. *
  12. * ###### Examples
  13. *
  14. * ```markdown
  15. * "a"
  16. * 'b'
  17. * (c)
  18. * "a
  19. * b"
  20. * 'a
  21. * b'
  22. * (a\)b)
  23. * ```
  24. *
  25. * @param {Effects} effects
  26. * Context.
  27. * @param {State} ok
  28. * State switched to when successful.
  29. * @param {State} nok
  30. * State switched to when unsuccessful.
  31. * @param {TokenType} type
  32. * Type of the whole title (`"a"`, `'b'`, `(c)`).
  33. * @param {TokenType} markerType
  34. * Type for the markers (`"`, `'`, `(`, and `)`).
  35. * @param {TokenType} stringType
  36. * Type for the value (`a`).
  37. * @returns {State}
  38. * Start state.
  39. */ // eslint-disable-next-line max-params
  40. export function factoryTitle(effects, ok, nok, type, markerType, stringType) {
  41. /** @type {NonNullable<Code>} */
  42. let marker
  43. return start
  44. /**
  45. * Start of title.
  46. *
  47. * ```markdown
  48. * > | "a"
  49. * ^
  50. * ```
  51. *
  52. * @type {State}
  53. */
  54. function start(code) {
  55. if (code === 34 || code === 39 || code === 40) {
  56. effects.enter(type)
  57. effects.enter(markerType)
  58. effects.consume(code)
  59. effects.exit(markerType)
  60. marker = code === 40 ? 41 : code
  61. return begin
  62. }
  63. return nok(code)
  64. }
  65. /**
  66. * After opening marker.
  67. *
  68. * This is also used at the closing marker.
  69. *
  70. * ```markdown
  71. * > | "a"
  72. * ^
  73. * ```
  74. *
  75. * @type {State}
  76. */
  77. function begin(code) {
  78. if (code === marker) {
  79. effects.enter(markerType)
  80. effects.consume(code)
  81. effects.exit(markerType)
  82. effects.exit(type)
  83. return ok
  84. }
  85. effects.enter(stringType)
  86. return atBreak(code)
  87. }
  88. /**
  89. * At something, before something else.
  90. *
  91. * ```markdown
  92. * > | "a"
  93. * ^
  94. * ```
  95. *
  96. * @type {State}
  97. */
  98. function atBreak(code) {
  99. if (code === marker) {
  100. effects.exit(stringType)
  101. return begin(marker)
  102. }
  103. if (code === null) {
  104. return nok(code)
  105. }
  106. // Note: blank lines can’t exist in content.
  107. if (markdownLineEnding(code)) {
  108. // To do: use `space_or_tab_eol_with_options`, connect.
  109. effects.enter('lineEnding')
  110. effects.consume(code)
  111. effects.exit('lineEnding')
  112. return factorySpace(effects, atBreak, 'linePrefix')
  113. }
  114. effects.enter('chunkString', {
  115. contentType: 'string'
  116. })
  117. return inside(code)
  118. }
  119. /**
  120. *
  121. *
  122. * @type {State}
  123. */
  124. function inside(code) {
  125. if (code === marker || code === null || markdownLineEnding(code)) {
  126. effects.exit('chunkString')
  127. return atBreak(code)
  128. }
  129. effects.consume(code)
  130. return code === 92 ? escape : inside
  131. }
  132. /**
  133. * After `\`, at a special character.
  134. *
  135. * ```markdown
  136. * > | "a\*b"
  137. * ^
  138. * ```
  139. *
  140. * @type {State}
  141. */
  142. function escape(code) {
  143. if (code === marker || code === 92) {
  144. effects.consume(code)
  145. return inside
  146. }
  147. return inside(code)
  148. }
  149. }