math-flow.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * @import {Construct, State, TokenizeContext, Tokenizer} from 'micromark-util-types'
  3. */
  4. import {ok as assert} from 'devlop'
  5. import {factorySpace} from 'micromark-factory-space'
  6. import {markdownLineEnding} from 'micromark-util-character'
  7. import {codes, constants, types} from 'micromark-util-symbol'
  8. /** @type {Construct} */
  9. export const mathFlow = {
  10. tokenize: tokenizeMathFenced,
  11. concrete: true,
  12. name: 'mathFlow'
  13. }
  14. /** @type {Construct} */
  15. const nonLazyContinuation = {
  16. tokenize: tokenizeNonLazyContinuation,
  17. partial: true
  18. }
  19. /**
  20. * @this {TokenizeContext}
  21. * @type {Tokenizer}
  22. */
  23. function tokenizeMathFenced(effects, ok, nok) {
  24. const self = this
  25. const tail = self.events[self.events.length - 1]
  26. const initialSize =
  27. tail && tail[1].type === types.linePrefix
  28. ? tail[2].sliceSerialize(tail[1], true).length
  29. : 0
  30. let sizeOpen = 0
  31. return start
  32. /**
  33. * Start of math.
  34. *
  35. * ```markdown
  36. * > | $$
  37. * ^
  38. * | \frac{1}{2}
  39. * | $$
  40. * ```
  41. *
  42. * @type {State}
  43. */
  44. function start(code) {
  45. assert(code === codes.dollarSign, 'expected `$`')
  46. effects.enter('mathFlow')
  47. effects.enter('mathFlowFence')
  48. effects.enter('mathFlowFenceSequence')
  49. return sequenceOpen(code)
  50. }
  51. /**
  52. * In opening fence sequence.
  53. *
  54. * ```markdown
  55. * > | $$
  56. * ^
  57. * | \frac{1}{2}
  58. * | $$
  59. * ```
  60. *
  61. * @type {State}
  62. */
  63. function sequenceOpen(code) {
  64. if (code === codes.dollarSign) {
  65. effects.consume(code)
  66. sizeOpen++
  67. return sequenceOpen
  68. }
  69. if (sizeOpen < 2) {
  70. return nok(code)
  71. }
  72. effects.exit('mathFlowFenceSequence')
  73. return factorySpace(effects, metaBefore, types.whitespace)(code)
  74. }
  75. /**
  76. * In opening fence, before meta.
  77. *
  78. * ```markdown
  79. * > | $$asciimath
  80. * ^
  81. * | x < y
  82. * | $$
  83. * ```
  84. *
  85. * @type {State}
  86. */
  87. function metaBefore(code) {
  88. if (code === codes.eof || markdownLineEnding(code)) {
  89. return metaAfter(code)
  90. }
  91. effects.enter('mathFlowFenceMeta')
  92. effects.enter(types.chunkString, {contentType: constants.contentTypeString})
  93. return meta(code)
  94. }
  95. /**
  96. * In meta.
  97. *
  98. * ```markdown
  99. * > | $$asciimath
  100. * ^
  101. * | x < y
  102. * | $$
  103. * ```
  104. *
  105. * @type {State}
  106. */
  107. function meta(code) {
  108. if (code === codes.eof || markdownLineEnding(code)) {
  109. effects.exit(types.chunkString)
  110. effects.exit('mathFlowFenceMeta')
  111. return metaAfter(code)
  112. }
  113. if (code === codes.dollarSign) {
  114. return nok(code)
  115. }
  116. effects.consume(code)
  117. return meta
  118. }
  119. /**
  120. * After meta.
  121. *
  122. * ```markdown
  123. * > | $$
  124. * ^
  125. * | \frac{1}{2}
  126. * | $$
  127. * ```
  128. *
  129. * @type {State}
  130. */
  131. function metaAfter(code) {
  132. // Guaranteed to be eol/eof.
  133. effects.exit('mathFlowFence')
  134. if (self.interrupt) {
  135. return ok(code)
  136. }
  137. return effects.attempt(
  138. nonLazyContinuation,
  139. beforeNonLazyContinuation,
  140. after
  141. )(code)
  142. }
  143. /**
  144. * After eol/eof in math, at a non-lazy closing fence or content.
  145. *
  146. * ```markdown
  147. * | $$
  148. * > | \frac{1}{2}
  149. * ^
  150. * > | $$
  151. * ^
  152. * ```
  153. *
  154. * @type {State}
  155. */
  156. function beforeNonLazyContinuation(code) {
  157. return effects.attempt(
  158. {tokenize: tokenizeClosingFence, partial: true},
  159. after,
  160. contentStart
  161. )(code)
  162. }
  163. /**
  164. * Before math content, definitely not before a closing fence.
  165. *
  166. * ```markdown
  167. * | $$
  168. * > | \frac{1}{2}
  169. * ^
  170. * | $$
  171. * ```
  172. *
  173. * @type {State}
  174. */
  175. function contentStart(code) {
  176. return (
  177. initialSize
  178. ? factorySpace(
  179. effects,
  180. beforeContentChunk,
  181. types.linePrefix,
  182. initialSize + 1
  183. )
  184. : beforeContentChunk
  185. )(code)
  186. }
  187. /**
  188. * Before math content, after optional prefix.
  189. *
  190. * ```markdown
  191. * | $$
  192. * > | \frac{1}{2}
  193. * ^
  194. * | $$
  195. * ```
  196. *
  197. * @type {State}
  198. */
  199. function beforeContentChunk(code) {
  200. if (code === codes.eof) {
  201. return after(code)
  202. }
  203. if (markdownLineEnding(code)) {
  204. return effects.attempt(
  205. nonLazyContinuation,
  206. beforeNonLazyContinuation,
  207. after
  208. )(code)
  209. }
  210. effects.enter('mathFlowValue')
  211. return contentChunk(code)
  212. }
  213. /**
  214. * In math content.
  215. *
  216. * ```markdown
  217. * | $$
  218. * > | \frac{1}{2}
  219. * ^
  220. * | $$
  221. * ```
  222. *
  223. * @type {State}
  224. */
  225. function contentChunk(code) {
  226. if (code === codes.eof || markdownLineEnding(code)) {
  227. effects.exit('mathFlowValue')
  228. return beforeContentChunk(code)
  229. }
  230. effects.consume(code)
  231. return contentChunk
  232. }
  233. /**
  234. * After math (ha!).
  235. *
  236. * ```markdown
  237. * | $$
  238. * | \frac{1}{2}
  239. * > | $$
  240. * ^
  241. * ```
  242. *
  243. * @type {State}
  244. */
  245. function after(code) {
  246. effects.exit('mathFlow')
  247. return ok(code)
  248. }
  249. /** @type {Tokenizer} */
  250. function tokenizeClosingFence(effects, ok, nok) {
  251. let size = 0
  252. assert(self.parser.constructs.disable.null, 'expected `disable.null`')
  253. /**
  254. * Before closing fence, at optional whitespace.
  255. *
  256. * ```markdown
  257. * | $$
  258. * | \frac{1}{2}
  259. * > | $$
  260. * ^
  261. * ```
  262. */
  263. return factorySpace(
  264. effects,
  265. beforeSequenceClose,
  266. types.linePrefix,
  267. self.parser.constructs.disable.null.includes('codeIndented')
  268. ? undefined
  269. : constants.tabSize
  270. )
  271. /**
  272. * In closing fence, after optional whitespace, at sequence.
  273. *
  274. * ```markdown
  275. * | $$
  276. * | \frac{1}{2}
  277. * > | $$
  278. * ^
  279. * ```
  280. *
  281. * @type {State}
  282. */
  283. function beforeSequenceClose(code) {
  284. effects.enter('mathFlowFence')
  285. effects.enter('mathFlowFenceSequence')
  286. return sequenceClose(code)
  287. }
  288. /**
  289. * In closing fence sequence.
  290. *
  291. * ```markdown
  292. * | $$
  293. * | \frac{1}{2}
  294. * > | $$
  295. * ^
  296. * ```
  297. *
  298. * @type {State}
  299. */
  300. function sequenceClose(code) {
  301. if (code === codes.dollarSign) {
  302. size++
  303. effects.consume(code)
  304. return sequenceClose
  305. }
  306. if (size < sizeOpen) {
  307. return nok(code)
  308. }
  309. effects.exit('mathFlowFenceSequence')
  310. return factorySpace(effects, afterSequenceClose, types.whitespace)(code)
  311. }
  312. /**
  313. * After closing fence sequence, after optional whitespace.
  314. *
  315. * ```markdown
  316. * | $$
  317. * | \frac{1}{2}
  318. * > | $$
  319. * ^
  320. * ```
  321. *
  322. * @type {State}
  323. */
  324. function afterSequenceClose(code) {
  325. if (code === codes.eof || markdownLineEnding(code)) {
  326. effects.exit('mathFlowFence')
  327. return ok(code)
  328. }
  329. return nok(code)
  330. }
  331. }
  332. }
  333. /**
  334. * @this {TokenizeContext}
  335. * @type {Tokenizer}
  336. */
  337. function tokenizeNonLazyContinuation(effects, ok, nok) {
  338. const self = this
  339. return start
  340. /** @type {State} */
  341. function start(code) {
  342. if (code === null) {
  343. return ok(code)
  344. }
  345. assert(markdownLineEnding(code), 'expected eol')
  346. effects.enter(types.lineEnding)
  347. effects.consume(code)
  348. effects.exit(types.lineEnding)
  349. return lineStart
  350. }
  351. /** @type {State} */
  352. function lineStart(code) {
  353. return self.parser.lazy[self.now().line] ? nok(code) : ok(code)
  354. }
  355. }