code-fenced.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /**
  2. * @typedef {import('micromark-util-types').Code} Code
  3. * @typedef {import('micromark-util-types').Construct} Construct
  4. * @typedef {import('micromark-util-types').State} State
  5. * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
  6. * @typedef {import('micromark-util-types').Tokenizer} Tokenizer
  7. */
  8. import {factorySpace} from 'micromark-factory-space'
  9. import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
  10. import {codes, constants, types} from 'micromark-util-symbol'
  11. import {ok as assert} from 'devlop'
  12. /** @type {Construct} */
  13. const nonLazyContinuation = {
  14. tokenize: tokenizeNonLazyContinuation,
  15. partial: true
  16. }
  17. /** @type {Construct} */
  18. export const codeFenced = {
  19. name: 'codeFenced',
  20. tokenize: tokenizeCodeFenced,
  21. concrete: true
  22. }
  23. /**
  24. * @this {TokenizeContext}
  25. * @type {Tokenizer}
  26. */
  27. function tokenizeCodeFenced(effects, ok, nok) {
  28. const self = this
  29. /** @type {Construct} */
  30. const closeStart = {tokenize: tokenizeCloseStart, partial: true}
  31. let initialPrefix = 0
  32. let sizeOpen = 0
  33. /** @type {NonNullable<Code>} */
  34. let marker
  35. return start
  36. /**
  37. * Start of code.
  38. *
  39. * ```markdown
  40. * > | ~~~js
  41. * ^
  42. * | alert(1)
  43. * | ~~~
  44. * ```
  45. *
  46. * @type {State}
  47. */
  48. function start(code) {
  49. // To do: parse whitespace like `markdown-rs`.
  50. return beforeSequenceOpen(code)
  51. }
  52. /**
  53. * In opening fence, after prefix, at sequence.
  54. *
  55. * ```markdown
  56. * > | ~~~js
  57. * ^
  58. * | alert(1)
  59. * | ~~~
  60. * ```
  61. *
  62. * @type {State}
  63. */
  64. function beforeSequenceOpen(code) {
  65. assert(
  66. code === codes.graveAccent || code === codes.tilde,
  67. 'expected `` ` `` or `~`'
  68. )
  69. const tail = self.events[self.events.length - 1]
  70. initialPrefix =
  71. tail && tail[1].type === types.linePrefix
  72. ? tail[2].sliceSerialize(tail[1], true).length
  73. : 0
  74. marker = code
  75. effects.enter(types.codeFenced)
  76. effects.enter(types.codeFencedFence)
  77. effects.enter(types.codeFencedFenceSequence)
  78. return sequenceOpen(code)
  79. }
  80. /**
  81. * In opening fence sequence.
  82. *
  83. * ```markdown
  84. * > | ~~~js
  85. * ^
  86. * | alert(1)
  87. * | ~~~
  88. * ```
  89. *
  90. * @type {State}
  91. */
  92. function sequenceOpen(code) {
  93. if (code === marker) {
  94. sizeOpen++
  95. effects.consume(code)
  96. return sequenceOpen
  97. }
  98. if (sizeOpen < constants.codeFencedSequenceSizeMin) {
  99. return nok(code)
  100. }
  101. effects.exit(types.codeFencedFenceSequence)
  102. return markdownSpace(code)
  103. ? factorySpace(effects, infoBefore, types.whitespace)(code)
  104. : infoBefore(code)
  105. }
  106. /**
  107. * In opening fence, after the sequence (and optional whitespace), before info.
  108. *
  109. * ```markdown
  110. * > | ~~~js
  111. * ^
  112. * | alert(1)
  113. * | ~~~
  114. * ```
  115. *
  116. * @type {State}
  117. */
  118. function infoBefore(code) {
  119. if (code === codes.eof || markdownLineEnding(code)) {
  120. effects.exit(types.codeFencedFence)
  121. return self.interrupt
  122. ? ok(code)
  123. : effects.check(nonLazyContinuation, atNonLazyBreak, after)(code)
  124. }
  125. effects.enter(types.codeFencedFenceInfo)
  126. effects.enter(types.chunkString, {contentType: constants.contentTypeString})
  127. return info(code)
  128. }
  129. /**
  130. * In info.
  131. *
  132. * ```markdown
  133. * > | ~~~js
  134. * ^
  135. * | alert(1)
  136. * | ~~~
  137. * ```
  138. *
  139. * @type {State}
  140. */
  141. function info(code) {
  142. if (code === codes.eof || markdownLineEnding(code)) {
  143. effects.exit(types.chunkString)
  144. effects.exit(types.codeFencedFenceInfo)
  145. return infoBefore(code)
  146. }
  147. if (markdownSpace(code)) {
  148. effects.exit(types.chunkString)
  149. effects.exit(types.codeFencedFenceInfo)
  150. return factorySpace(effects, metaBefore, types.whitespace)(code)
  151. }
  152. if (code === codes.graveAccent && code === marker) {
  153. return nok(code)
  154. }
  155. effects.consume(code)
  156. return info
  157. }
  158. /**
  159. * In opening fence, after info and whitespace, before meta.
  160. *
  161. * ```markdown
  162. * > | ~~~js eval
  163. * ^
  164. * | alert(1)
  165. * | ~~~
  166. * ```
  167. *
  168. * @type {State}
  169. */
  170. function metaBefore(code) {
  171. if (code === codes.eof || markdownLineEnding(code)) {
  172. return infoBefore(code)
  173. }
  174. effects.enter(types.codeFencedFenceMeta)
  175. effects.enter(types.chunkString, {contentType: constants.contentTypeString})
  176. return meta(code)
  177. }
  178. /**
  179. * In meta.
  180. *
  181. * ```markdown
  182. * > | ~~~js eval
  183. * ^
  184. * | alert(1)
  185. * | ~~~
  186. * ```
  187. *
  188. * @type {State}
  189. */
  190. function meta(code) {
  191. if (code === codes.eof || markdownLineEnding(code)) {
  192. effects.exit(types.chunkString)
  193. effects.exit(types.codeFencedFenceMeta)
  194. return infoBefore(code)
  195. }
  196. if (code === codes.graveAccent && code === marker) {
  197. return nok(code)
  198. }
  199. effects.consume(code)
  200. return meta
  201. }
  202. /**
  203. * At eol/eof in code, before a non-lazy closing fence or content.
  204. *
  205. * ```markdown
  206. * > | ~~~js
  207. * ^
  208. * > | alert(1)
  209. * ^
  210. * | ~~~
  211. * ```
  212. *
  213. * @type {State}
  214. */
  215. function atNonLazyBreak(code) {
  216. assert(markdownLineEnding(code), 'expected eol')
  217. return effects.attempt(closeStart, after, contentBefore)(code)
  218. }
  219. /**
  220. * Before code content, not a closing fence, at eol.
  221. *
  222. * ```markdown
  223. * | ~~~js
  224. * > | alert(1)
  225. * ^
  226. * | ~~~
  227. * ```
  228. *
  229. * @type {State}
  230. */
  231. function contentBefore(code) {
  232. assert(markdownLineEnding(code), 'expected eol')
  233. effects.enter(types.lineEnding)
  234. effects.consume(code)
  235. effects.exit(types.lineEnding)
  236. return contentStart
  237. }
  238. /**
  239. * Before code content, not a closing fence.
  240. *
  241. * ```markdown
  242. * | ~~~js
  243. * > | alert(1)
  244. * ^
  245. * | ~~~
  246. * ```
  247. *
  248. * @type {State}
  249. */
  250. function contentStart(code) {
  251. return initialPrefix > 0 && markdownSpace(code)
  252. ? factorySpace(
  253. effects,
  254. beforeContentChunk,
  255. types.linePrefix,
  256. initialPrefix + 1
  257. )(code)
  258. : beforeContentChunk(code)
  259. }
  260. /**
  261. * Before code content, after optional prefix.
  262. *
  263. * ```markdown
  264. * | ~~~js
  265. * > | alert(1)
  266. * ^
  267. * | ~~~
  268. * ```
  269. *
  270. * @type {State}
  271. */
  272. function beforeContentChunk(code) {
  273. if (code === codes.eof || markdownLineEnding(code)) {
  274. return effects.check(nonLazyContinuation, atNonLazyBreak, after)(code)
  275. }
  276. effects.enter(types.codeFlowValue)
  277. return contentChunk(code)
  278. }
  279. /**
  280. * In code content.
  281. *
  282. * ```markdown
  283. * | ~~~js
  284. * > | alert(1)
  285. * ^^^^^^^^
  286. * | ~~~
  287. * ```
  288. *
  289. * @type {State}
  290. */
  291. function contentChunk(code) {
  292. if (code === codes.eof || markdownLineEnding(code)) {
  293. effects.exit(types.codeFlowValue)
  294. return beforeContentChunk(code)
  295. }
  296. effects.consume(code)
  297. return contentChunk
  298. }
  299. /**
  300. * After code.
  301. *
  302. * ```markdown
  303. * | ~~~js
  304. * | alert(1)
  305. * > | ~~~
  306. * ^
  307. * ```
  308. *
  309. * @type {State}
  310. */
  311. function after(code) {
  312. effects.exit(types.codeFenced)
  313. return ok(code)
  314. }
  315. /**
  316. * @this {TokenizeContext}
  317. * @type {Tokenizer}
  318. */
  319. function tokenizeCloseStart(effects, ok, nok) {
  320. let size = 0
  321. return startBefore
  322. /**
  323. *
  324. *
  325. * @type {State}
  326. */
  327. function startBefore(code) {
  328. assert(markdownLineEnding(code), 'expected eol')
  329. effects.enter(types.lineEnding)
  330. effects.consume(code)
  331. effects.exit(types.lineEnding)
  332. return start
  333. }
  334. /**
  335. * Before closing fence, at optional whitespace.
  336. *
  337. * ```markdown
  338. * | ~~~js
  339. * | alert(1)
  340. * > | ~~~
  341. * ^
  342. * ```
  343. *
  344. * @type {State}
  345. */
  346. function start(code) {
  347. // Always populated by defaults.
  348. assert(
  349. self.parser.constructs.disable.null,
  350. 'expected `disable.null` to be populated'
  351. )
  352. // To do: `enter` here or in next state?
  353. effects.enter(types.codeFencedFence)
  354. return markdownSpace(code)
  355. ? factorySpace(
  356. effects,
  357. beforeSequenceClose,
  358. types.linePrefix,
  359. self.parser.constructs.disable.null.includes('codeIndented')
  360. ? undefined
  361. : constants.tabSize
  362. )(code)
  363. : beforeSequenceClose(code)
  364. }
  365. /**
  366. * In closing fence, after optional whitespace, at sequence.
  367. *
  368. * ```markdown
  369. * | ~~~js
  370. * | alert(1)
  371. * > | ~~~
  372. * ^
  373. * ```
  374. *
  375. * @type {State}
  376. */
  377. function beforeSequenceClose(code) {
  378. if (code === marker) {
  379. effects.enter(types.codeFencedFenceSequence)
  380. return sequenceClose(code)
  381. }
  382. return nok(code)
  383. }
  384. /**
  385. * In closing fence sequence.
  386. *
  387. * ```markdown
  388. * | ~~~js
  389. * | alert(1)
  390. * > | ~~~
  391. * ^
  392. * ```
  393. *
  394. * @type {State}
  395. */
  396. function sequenceClose(code) {
  397. if (code === marker) {
  398. size++
  399. effects.consume(code)
  400. return sequenceClose
  401. }
  402. if (size >= sizeOpen) {
  403. effects.exit(types.codeFencedFenceSequence)
  404. return markdownSpace(code)
  405. ? factorySpace(effects, sequenceCloseAfter, types.whitespace)(code)
  406. : sequenceCloseAfter(code)
  407. }
  408. return nok(code)
  409. }
  410. /**
  411. * After closing fence sequence, after optional whitespace.
  412. *
  413. * ```markdown
  414. * | ~~~js
  415. * | alert(1)
  416. * > | ~~~
  417. * ^
  418. * ```
  419. *
  420. * @type {State}
  421. */
  422. function sequenceCloseAfter(code) {
  423. if (code === codes.eof || markdownLineEnding(code)) {
  424. effects.exit(types.codeFencedFence)
  425. return ok(code)
  426. }
  427. return nok(code)
  428. }
  429. }
  430. }
  431. /**
  432. * @this {TokenizeContext}
  433. * @type {Tokenizer}
  434. */
  435. function tokenizeNonLazyContinuation(effects, ok, nok) {
  436. const self = this
  437. return start
  438. /**
  439. *
  440. *
  441. * @type {State}
  442. */
  443. function start(code) {
  444. if (code === codes.eof) {
  445. return nok(code)
  446. }
  447. assert(markdownLineEnding(code), 'expected eol')
  448. effects.enter(types.lineEnding)
  449. effects.consume(code)
  450. effects.exit(types.lineEnding)
  451. return lineStart
  452. }
  453. /**
  454. *
  455. *
  456. * @type {State}
  457. */
  458. function lineStart(code) {
  459. return self.parser.lazy[self.now().line] ? nok(code) : ok(code)
  460. }
  461. }