list.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 'use strict'
  2. var asciiDigit = require('../character/ascii-digit.js')
  3. var codes = require('../character/codes.js')
  4. var markdownSpace = require('../character/markdown-space.js')
  5. var constants = require('../constant/constants.js')
  6. var types = require('../constant/types.js')
  7. var prefixSize = require('../util/prefix-size.js')
  8. var sizeChunks = require('../util/size-chunks.js')
  9. var factorySpace = require('./factory-space.js')
  10. var partialBlankLine = require('./partial-blank-line.js')
  11. var thematicBreak = require('./thematic-break.js')
  12. var list = {
  13. name: 'list',
  14. tokenize: tokenizeListStart,
  15. continuation: {tokenize: tokenizeListContinuation},
  16. exit: tokenizeListEnd
  17. }
  18. var listItemPrefixWhitespaceConstruct = {
  19. tokenize: tokenizeListItemPrefixWhitespace,
  20. partial: true
  21. }
  22. var indentConstruct = {tokenize: tokenizeIndent, partial: true}
  23. function tokenizeListStart(effects, ok, nok) {
  24. var self = this
  25. var initialSize = prefixSize(self.events, types.linePrefix)
  26. var size = 0
  27. return start
  28. function start(code) {
  29. var kind =
  30. self.containerState.type ||
  31. (code === codes.asterisk || code === codes.plusSign || code === codes.dash
  32. ? types.listUnordered
  33. : types.listOrdered)
  34. if (
  35. kind === types.listUnordered
  36. ? !self.containerState.marker || code === self.containerState.marker
  37. : asciiDigit(code)
  38. ) {
  39. if (!self.containerState.type) {
  40. self.containerState.type = kind
  41. effects.enter(kind, {_container: true})
  42. }
  43. if (kind === types.listUnordered) {
  44. effects.enter(types.listItemPrefix)
  45. return code === codes.asterisk || code === codes.dash
  46. ? effects.check(thematicBreak, nok, atMarker)(code)
  47. : atMarker(code)
  48. }
  49. if (!self.interrupt || code === codes.digit1) {
  50. effects.enter(types.listItemPrefix)
  51. effects.enter(types.listItemValue)
  52. return inside(code)
  53. }
  54. }
  55. return nok(code)
  56. }
  57. function inside(code) {
  58. if (asciiDigit(code) && ++size < constants.listItemValueSizeMax) {
  59. effects.consume(code)
  60. return inside
  61. }
  62. if (
  63. (!self.interrupt || size < 2) &&
  64. (self.containerState.marker
  65. ? code === self.containerState.marker
  66. : code === codes.rightParenthesis || code === codes.dot)
  67. ) {
  68. effects.exit(types.listItemValue)
  69. return atMarker(code)
  70. }
  71. return nok(code)
  72. }
  73. function atMarker(code) {
  74. effects.enter(types.listItemMarker)
  75. effects.consume(code)
  76. effects.exit(types.listItemMarker)
  77. self.containerState.marker = self.containerState.marker || code
  78. return effects.check(
  79. partialBlankLine,
  80. // Can’t be empty when interrupting.
  81. self.interrupt ? nok : onBlank,
  82. effects.attempt(
  83. listItemPrefixWhitespaceConstruct,
  84. endOfPrefix,
  85. otherPrefix
  86. )
  87. )
  88. }
  89. function onBlank(code) {
  90. self.containerState.initialBlankLine = true
  91. initialSize++
  92. return endOfPrefix(code)
  93. }
  94. function otherPrefix(code) {
  95. if (markdownSpace(code)) {
  96. effects.enter(types.listItemPrefixWhitespace)
  97. effects.consume(code)
  98. effects.exit(types.listItemPrefixWhitespace)
  99. return endOfPrefix
  100. }
  101. return nok(code)
  102. }
  103. function endOfPrefix(code) {
  104. self.containerState.size =
  105. initialSize +
  106. sizeChunks(self.sliceStream(effects.exit(types.listItemPrefix)))
  107. return ok(code)
  108. }
  109. }
  110. function tokenizeListContinuation(effects, ok, nok) {
  111. var self = this
  112. self.containerState._closeFlow = undefined
  113. return effects.check(partialBlankLine, onBlank, notBlank)
  114. function onBlank(code) {
  115. self.containerState.furtherBlankLines =
  116. self.containerState.furtherBlankLines ||
  117. self.containerState.initialBlankLine
  118. // We have a blank line.
  119. // Still, try to consume at most the items size.
  120. return factorySpace(
  121. effects,
  122. ok,
  123. types.listItemIndent,
  124. self.containerState.size + 1
  125. )(code)
  126. }
  127. function notBlank(code) {
  128. if (self.containerState.furtherBlankLines || !markdownSpace(code)) {
  129. self.containerState.furtherBlankLines = self.containerState.initialBlankLine = undefined
  130. return notInCurrentItem(code)
  131. }
  132. self.containerState.furtherBlankLines = self.containerState.initialBlankLine = undefined
  133. return effects.attempt(indentConstruct, ok, notInCurrentItem)(code)
  134. }
  135. function notInCurrentItem(code) {
  136. // While we do continue, we signal that the flow should be closed.
  137. self.containerState._closeFlow = true
  138. // As we’re closing flow, we’re no longer interrupting.
  139. self.interrupt = undefined
  140. return factorySpace(
  141. effects,
  142. effects.attempt(list, ok, nok),
  143. types.linePrefix,
  144. self.parser.constructs.disable.null.indexOf('codeIndented') > -1
  145. ? undefined
  146. : constants.tabSize
  147. )(code)
  148. }
  149. }
  150. function tokenizeIndent(effects, ok, nok) {
  151. var self = this
  152. return factorySpace(
  153. effects,
  154. afterPrefix,
  155. types.listItemIndent,
  156. self.containerState.size + 1
  157. )
  158. function afterPrefix(code) {
  159. return prefixSize(self.events, types.listItemIndent) ===
  160. self.containerState.size
  161. ? ok(code)
  162. : nok(code)
  163. }
  164. }
  165. function tokenizeListEnd(effects) {
  166. effects.exit(this.containerState.type)
  167. }
  168. function tokenizeListItemPrefixWhitespace(effects, ok, nok) {
  169. var self = this
  170. return factorySpace(
  171. effects,
  172. afterPrefix,
  173. types.listItemPrefixWhitespace,
  174. self.parser.constructs.disable.null.indexOf('codeIndented') > -1
  175. ? undefined
  176. : constants.tabSize + 1
  177. )
  178. function afterPrefix(code) {
  179. return markdownSpace(code) ||
  180. !prefixSize(self.events, types.listItemPrefixWhitespace)
  181. ? nok(code)
  182. : ok(code)
  183. }
  184. }
  185. module.exports = list