syntax.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /**
  2. * @typedef {import('micromark-util-types').Extension} Extension
  3. * @typedef {import('micromark-util-types').Resolver} Resolver
  4. * @typedef {import('micromark-util-types').Tokenizer} Tokenizer
  5. * @typedef {import('micromark-util-types').State} State
  6. * @typedef {import('micromark-util-types').Token} Token
  7. */
  8. /**
  9. * @typedef {'left'|'center'|'right'|'none'} Align
  10. */
  11. import { factorySpace } from 'micromark-factory-space';
  12. import {
  13. markdownLineEnding,
  14. markdownLineEndingOrSpace,
  15. markdownSpace,
  16. } from 'micromark-util-character';
  17. import { codes } from 'micromark-util-symbol/codes.js';
  18. import { constants } from 'micromark-util-symbol/constants.js';
  19. import { types } from 'micromark-util-symbol/types.js';
  20. import { ok as assert } from 'uvu/assert';
  21. /**
  22. * Syntax extension for micromark (passed in `extensions`).
  23. *
  24. * @type {Extension}
  25. */
  26. export const gfmTable = {
  27. flow: { null: { tokenize: tokenizeTable, resolve: resolveTable } },
  28. };
  29. const nextPrefixedOrBlank = {
  30. tokenize: tokenizeNextPrefixedOrBlank,
  31. partial: true,
  32. };
  33. /** @type {Resolver} */
  34. // eslint-disable-next-line complexity
  35. function resolveTable(events, context) {
  36. let index = -1;
  37. /** @type {boolean|undefined} */
  38. let inHead;
  39. /** @type {boolean|undefined} */
  40. let inDelimiterRow;
  41. /** @type {boolean|undefined} */
  42. let inRow;
  43. /** @type {number|undefined} */
  44. let contentStart;
  45. /** @type {number|undefined} */
  46. let contentEnd;
  47. /** @type {number|undefined} */
  48. let cellStart;
  49. /** @type {boolean|undefined} */
  50. let seenCellInRow;
  51. while (++index < events.length) {
  52. const token = events[index][1];
  53. if (inRow) {
  54. if (token.type === 'temporaryTableCellContent') {
  55. contentStart = contentStart || index;
  56. contentEnd = index;
  57. }
  58. if (
  59. // Combine separate content parts into one.
  60. (token.type === 'tableCellDivider' || token.type === 'tableRow')
  61. && contentEnd
  62. ) {
  63. assert(
  64. contentStart,
  65. 'expected `contentStart` to be defined if `contentEnd` is',
  66. );
  67. const content = {
  68. type: 'tableContent',
  69. start: events[contentStart][1].start,
  70. end: events[contentEnd][1].end,
  71. };
  72. /** @type {Token} */
  73. const text = {
  74. type: types.chunkText,
  75. start: content.start,
  76. end: content.end,
  77. // @ts-expect-error It’s fine.
  78. contentType: constants.contentTypeText,
  79. };
  80. assert(
  81. contentStart,
  82. 'expected `contentStart` to be defined if `contentEnd` is',
  83. );
  84. events.splice(
  85. contentStart,
  86. contentEnd - contentStart + 1,
  87. ['enter', content, context],
  88. ['enter', text, context],
  89. ['exit', text, context],
  90. ['exit', content, context],
  91. );
  92. index -= contentEnd - contentStart - 3;
  93. contentStart = undefined;
  94. contentEnd = undefined;
  95. }
  96. }
  97. if (
  98. events[index][0] === 'exit'
  99. && cellStart !== undefined
  100. && cellStart + (seenCellInRow ? 0 : 1) < index
  101. && (token.type === 'tableCellDivider'
  102. || (token.type === 'tableRow'
  103. && (cellStart + 3 < index
  104. || events[cellStart][1].type !== types.whitespace)))
  105. ) {
  106. const cell = {
  107. // eslint-disable-next-line no-nested-ternary
  108. type: inDelimiterRow
  109. ? 'tableDelimiter'
  110. : inHead
  111. ? 'tableHeader'
  112. : 'tableData',
  113. start: events[cellStart][1].start,
  114. end: events[index][1].end,
  115. };
  116. events.splice(index + (token.type === 'tableCellDivider' ? 1 : 0), 0, [
  117. 'exit',
  118. cell,
  119. context,
  120. ]);
  121. events.splice(cellStart, 0, ['enter', cell, context]);
  122. index += 2;
  123. cellStart = index + 1;
  124. seenCellInRow = true;
  125. }
  126. if (token.type === 'tableRow') {
  127. inRow = events[index][0] === 'enter';
  128. if (inRow) {
  129. cellStart = index + 1;
  130. seenCellInRow = false;
  131. }
  132. }
  133. if (token.type === 'tableDelimiterRow') {
  134. inDelimiterRow = events[index][0] === 'enter';
  135. if (inDelimiterRow) {
  136. cellStart = index + 1;
  137. seenCellInRow = false;
  138. }
  139. }
  140. if (token.type === 'tableHead') {
  141. inHead = events[index][0] === 'enter';
  142. }
  143. }
  144. return events;
  145. }
  146. /** @type {Tokenizer} */
  147. function tokenizeTable(effects, ok, nok) {
  148. // eslint-disable-next-line @typescript-eslint/no-this-alias
  149. const self = this;
  150. /** @type {Array<Align>} */
  151. const align = [];
  152. let tableHeaderCount = 0;
  153. /** @type {boolean|undefined} */
  154. let seenDelimiter;
  155. /** @type {boolean|undefined} */
  156. let hasDash;
  157. return start;
  158. /** @type {State} */
  159. function start(code) {
  160. // @ts-expect-error Custom.
  161. effects.enter('table')._align = align;
  162. effects.enter('tableHead');
  163. effects.enter('tableRow');
  164. // If we start with a pipe, we open a cell marker.
  165. if (code === codes.verticalBar) {
  166. return cellDividerHead(code);
  167. }
  168. tableHeaderCount++;
  169. effects.enter('temporaryTableCellContent');
  170. // Can’t be space or eols at the start of a construct, so we’re in a cell.
  171. assert(!markdownLineEndingOrSpace(code), 'expected non-space');
  172. return inCellContentHead(code);
  173. }
  174. /** @type {State} */
  175. function cellDividerHead(code) {
  176. assert(code === codes.verticalBar, 'expected `|`');
  177. effects.enter('tableCellDivider');
  178. effects.consume(code);
  179. effects.exit('tableCellDivider');
  180. seenDelimiter = true;
  181. return cellBreakHead;
  182. }
  183. /** @type {State} */
  184. function cellBreakHead(code) {
  185. if (code === codes.eof || markdownLineEnding(code)) {
  186. return atRowEndHead(code);
  187. }
  188. if (markdownSpace(code)) {
  189. effects.enter(types.whitespace);
  190. effects.consume(code);
  191. return inWhitespaceHead;
  192. }
  193. if (seenDelimiter) {
  194. seenDelimiter = undefined;
  195. tableHeaderCount++;
  196. }
  197. if (code === codes.verticalBar) {
  198. return cellDividerHead(code);
  199. }
  200. // Anything else is cell content.
  201. effects.enter('temporaryTableCellContent');
  202. return inCellContentHead(code);
  203. }
  204. /** @type {State} */
  205. function inWhitespaceHead(code) {
  206. if (markdownSpace(code)) {
  207. effects.consume(code);
  208. return inWhitespaceHead;
  209. }
  210. effects.exit(types.whitespace);
  211. return cellBreakHead(code);
  212. }
  213. /** @type {State} */
  214. function inCellContentHead(code) {
  215. // EOF, whitespace, pipe
  216. if (
  217. code === codes.eof
  218. || code === codes.verticalBar
  219. || markdownLineEndingOrSpace(code)
  220. ) {
  221. effects.exit('temporaryTableCellContent');
  222. return cellBreakHead(code);
  223. }
  224. effects.consume(code);
  225. return code === codes.backslash
  226. ? inCellContentEscapeHead
  227. : inCellContentHead;
  228. }
  229. /** @type {State} */
  230. function inCellContentEscapeHead(code) {
  231. if (code === codes.backslash || code === codes.verticalBar) {
  232. effects.consume(code);
  233. return inCellContentHead;
  234. }
  235. // Anything else.
  236. return inCellContentHead(code);
  237. }
  238. /** @type {State} */
  239. function atRowEndHead(code) {
  240. if (code === codes.eof) {
  241. return nok(code);
  242. }
  243. assert(markdownLineEnding(code), 'expected eol');
  244. effects.exit('tableRow');
  245. effects.exit('tableHead');
  246. const originalInterrupt = self.interrupt;
  247. self.interrupt = true;
  248. return effects.attempt(
  249. { tokenize: tokenizeRowEnd, partial: true },
  250. (code) => {
  251. self.interrupt = originalInterrupt;
  252. effects.enter('tableDelimiterRow');
  253. return atDelimiterRowBreak(code);
  254. },
  255. (code) => {
  256. self.interrupt = originalInterrupt;
  257. return nok(code);
  258. },
  259. )(code);
  260. }
  261. /** @type {State} */
  262. function atDelimiterRowBreak(code) {
  263. if (code === codes.eof || markdownLineEnding(code)) {
  264. return rowEndDelimiter(code);
  265. }
  266. if (markdownSpace(code)) {
  267. effects.enter(types.whitespace);
  268. effects.consume(code);
  269. return inWhitespaceDelimiter;
  270. }
  271. if (code === codes.dash) {
  272. effects.enter('tableDelimiterFiller');
  273. effects.consume(code);
  274. hasDash = true;
  275. align.push('none');
  276. return inFillerDelimiter;
  277. }
  278. if (code === codes.colon) {
  279. effects.enter('tableDelimiterAlignment');
  280. effects.consume(code);
  281. effects.exit('tableDelimiterAlignment');
  282. align.push('left');
  283. return afterLeftAlignment;
  284. }
  285. // If we start with a pipe, we open a cell marker.
  286. if (code === codes.verticalBar) {
  287. effects.enter('tableCellDivider');
  288. effects.consume(code);
  289. effects.exit('tableCellDivider');
  290. return atDelimiterRowBreak;
  291. }
  292. return nok(code);
  293. }
  294. /** @type {State} */
  295. function inWhitespaceDelimiter(code) {
  296. if (markdownSpace(code)) {
  297. effects.consume(code);
  298. return inWhitespaceDelimiter;
  299. }
  300. effects.exit(types.whitespace);
  301. return atDelimiterRowBreak(code);
  302. }
  303. /** @type {State} */
  304. function inFillerDelimiter(code) {
  305. if (code === codes.dash) {
  306. effects.consume(code);
  307. return inFillerDelimiter;
  308. }
  309. effects.exit('tableDelimiterFiller');
  310. if (code === codes.colon) {
  311. effects.enter('tableDelimiterAlignment');
  312. effects.consume(code);
  313. effects.exit('tableDelimiterAlignment');
  314. align[align.length - 1] = align[align.length - 1] === 'left' ? 'center' : 'right';
  315. return afterRightAlignment;
  316. }
  317. return atDelimiterRowBreak(code);
  318. }
  319. /** @type {State} */
  320. function afterLeftAlignment(code) {
  321. if (code === codes.dash) {
  322. effects.enter('tableDelimiterFiller');
  323. effects.consume(code);
  324. hasDash = true;
  325. return inFillerDelimiter;
  326. }
  327. // Anything else is not ok.
  328. return nok(code);
  329. }
  330. /** @type {State} */
  331. function afterRightAlignment(code) {
  332. if (code === codes.eof || markdownLineEnding(code)) {
  333. return rowEndDelimiter(code);
  334. }
  335. if (markdownSpace(code)) {
  336. effects.enter(types.whitespace);
  337. effects.consume(code);
  338. return inWhitespaceDelimiter;
  339. }
  340. // `|`
  341. if (code === codes.verticalBar) {
  342. effects.enter('tableCellDivider');
  343. effects.consume(code);
  344. effects.exit('tableCellDivider');
  345. return atDelimiterRowBreak;
  346. }
  347. return nok(code);
  348. }
  349. /** @type {State} */
  350. function rowEndDelimiter(code) {
  351. effects.exit('tableDelimiterRow');
  352. // Exit if there was no dash at all, or if the header cell count is not the
  353. // delimiter cell count.
  354. if (!hasDash || tableHeaderCount !== align.length) {
  355. return nok(code);
  356. }
  357. if (code === codes.eof) {
  358. return tableClose(code);
  359. }
  360. assert(markdownLineEnding(code), 'expected eol');
  361. return effects.check(
  362. nextPrefixedOrBlank,
  363. tableClose,
  364. effects.attempt(
  365. { tokenize: tokenizeRowEnd, partial: true },
  366. factorySpace(effects, bodyStart, types.linePrefix, constants.tabSize),
  367. tableClose,
  368. ),
  369. )(code);
  370. }
  371. /** @type {State} */
  372. function tableClose(code) {
  373. effects.exit('table');
  374. return ok(code);
  375. }
  376. /** @type {State} */
  377. function bodyStart(code) {
  378. effects.enter('tableBody');
  379. return rowStartBody(code);
  380. }
  381. /** @type {State} */
  382. function rowStartBody(code) {
  383. effects.enter('tableRow');
  384. // If we start with a pipe, we open a cell marker.
  385. if (code === codes.verticalBar) {
  386. return cellDividerBody(code);
  387. }
  388. effects.enter('temporaryTableCellContent');
  389. // Can’t be space or eols at the start of a construct, so we’re in a cell.
  390. return inCellContentBody(code);
  391. }
  392. /** @type {State} */
  393. function cellDividerBody(code) {
  394. assert(code === codes.verticalBar, 'expected `|`');
  395. effects.enter('tableCellDivider');
  396. effects.consume(code);
  397. effects.exit('tableCellDivider');
  398. return cellBreakBody;
  399. }
  400. /** @type {State} */
  401. function cellBreakBody(code) {
  402. if (code === codes.eof || markdownLineEnding(code)) {
  403. return atRowEndBody(code);
  404. }
  405. if (markdownSpace(code)) {
  406. effects.enter(types.whitespace);
  407. effects.consume(code);
  408. return inWhitespaceBody;
  409. }
  410. // `|`
  411. if (code === codes.verticalBar) {
  412. return cellDividerBody(code);
  413. }
  414. // Anything else is cell content.
  415. effects.enter('temporaryTableCellContent');
  416. return inCellContentBody(code);
  417. }
  418. /** @type {State} */
  419. function inWhitespaceBody(code) {
  420. if (markdownSpace(code)) {
  421. effects.consume(code);
  422. return inWhitespaceBody;
  423. }
  424. effects.exit(types.whitespace);
  425. return cellBreakBody(code);
  426. }
  427. /** @type {State} */
  428. function inCellContentBody(code) {
  429. // EOF, whitespace, pipe
  430. if (
  431. code === codes.eof
  432. || code === codes.verticalBar
  433. || markdownLineEndingOrSpace(code)
  434. ) {
  435. effects.exit('temporaryTableCellContent');
  436. return cellBreakBody(code);
  437. }
  438. effects.consume(code);
  439. return code === codes.backslash
  440. ? inCellContentEscapeBody
  441. : inCellContentBody;
  442. }
  443. /** @type {State} */
  444. function inCellContentEscapeBody(code) {
  445. if (code === codes.backslash || code === codes.verticalBar) {
  446. effects.consume(code);
  447. return inCellContentBody;
  448. }
  449. // Anything else.
  450. return inCellContentBody(code);
  451. }
  452. /** @type {State} */
  453. function atRowEndBody(code) {
  454. effects.exit('tableRow');
  455. if (code === codes.eof) {
  456. return tableBodyClose(code);
  457. }
  458. return effects.check(
  459. nextPrefixedOrBlank,
  460. tableBodyClose,
  461. effects.attempt(
  462. { tokenize: tokenizeRowEnd, partial: true },
  463. factorySpace(
  464. effects,
  465. rowStartBody,
  466. types.linePrefix,
  467. constants.tabSize,
  468. ),
  469. tableBodyClose,
  470. ),
  471. )(code);
  472. }
  473. /** @type {State} */
  474. function tableBodyClose(code) {
  475. effects.exit('tableBody');
  476. return tableClose(code);
  477. }
  478. /** @type {Tokenizer} */
  479. function tokenizeRowEnd(effects, ok, nok) {
  480. return start;
  481. /** @type {State} */
  482. function start(code) {
  483. assert(markdownLineEnding(code), 'expected eol');
  484. effects.enter(types.lineEnding);
  485. effects.consume(code);
  486. effects.exit(types.lineEnding);
  487. return factorySpace(effects, prefixed, types.linePrefix);
  488. }
  489. /** @type {State} */
  490. function prefixed(code) {
  491. // Blank or interrupting line.
  492. if (
  493. self.parser.lazy[self.now().line]
  494. || code === codes.eof
  495. || markdownLineEnding(code)
  496. ) {
  497. return nok(code);
  498. }
  499. const tail = self.events[self.events.length - 1];
  500. // Indented code can interrupt delimiter and body rows.
  501. if (
  502. !self.parser.constructs.disable.null.includes('codeIndented')
  503. && tail
  504. && tail[1].type === types.linePrefix
  505. && tail[2].sliceSerialize(tail[1], true).length >= constants.tabSize
  506. ) {
  507. return nok(code);
  508. }
  509. self._gfmTableDynamicInterruptHack = true;
  510. return effects.check(
  511. self.parser.constructs.flow,
  512. (code) => {
  513. self._gfmTableDynamicInterruptHack = false;
  514. return nok(code);
  515. },
  516. (code) => {
  517. self._gfmTableDynamicInterruptHack = false;
  518. return ok(code);
  519. },
  520. )(code);
  521. }
  522. }
  523. }
  524. /** @type {Tokenizer} */
  525. function tokenizeNextPrefixedOrBlank(effects, ok, nok) {
  526. let size = 0;
  527. return start;
  528. /** @type {State} */
  529. function start(code) {
  530. // This is a check, so we don’t care about tokens, but we open a bogus one
  531. // so we’re valid.
  532. effects.enter('check');
  533. // EOL.
  534. effects.consume(code);
  535. return whitespace;
  536. }
  537. /** @type {State} */
  538. function whitespace(code) {
  539. if (code === codes.virtualSpace || code === codes.space) {
  540. effects.consume(code);
  541. size++;
  542. return size === constants.tabSize ? ok : whitespace;
  543. }
  544. // EOF or whitespace
  545. if (code === codes.eof || markdownLineEndingOrSpace(code)) {
  546. return ok(code);
  547. }
  548. // Anything else.
  549. return nok(code);
  550. }
  551. }