index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /**
  2. * @import {
  3. * Break,
  4. * Blockquote,
  5. * Code,
  6. * Definition,
  7. * Emphasis,
  8. * Heading,
  9. * Html,
  10. * Image,
  11. * InlineCode,
  12. * Link,
  13. * ListItem,
  14. * List,
  15. * Nodes,
  16. * Paragraph,
  17. * PhrasingContent,
  18. * ReferenceType,
  19. * Root,
  20. * Strong,
  21. * Text,
  22. * ThematicBreak
  23. * } from 'mdast'
  24. * @import {
  25. * Encoding,
  26. * Event,
  27. * Token,
  28. * Value
  29. * } from 'micromark-util-types'
  30. * @import {Point} from 'unist'
  31. * @import {
  32. * CompileContext,
  33. * CompileData,
  34. * Config,
  35. * Extension,
  36. * Handle,
  37. * OnEnterError,
  38. * Options
  39. * } from './types.js'
  40. */
  41. import {ok as assert} from 'devlop'
  42. import {toString} from 'mdast-util-to-string'
  43. import {parse, postprocess, preprocess} from 'micromark'
  44. import {decodeNumericCharacterReference} from 'micromark-util-decode-numeric-character-reference'
  45. import {decodeString} from 'micromark-util-decode-string'
  46. import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
  47. import {codes, constants, types} from 'micromark-util-symbol'
  48. import {decodeNamedCharacterReference} from 'decode-named-character-reference'
  49. import {stringifyPosition} from 'unist-util-stringify-position'
  50. const own = {}.hasOwnProperty
  51. /**
  52. * Turn markdown into a syntax tree.
  53. *
  54. * @overload
  55. * @param {Value} value
  56. * @param {Encoding | null | undefined} [encoding]
  57. * @param {Options | null | undefined} [options]
  58. * @returns {Root}
  59. *
  60. * @overload
  61. * @param {Value} value
  62. * @param {Options | null | undefined} [options]
  63. * @returns {Root}
  64. *
  65. * @param {Value} value
  66. * Markdown to parse.
  67. * @param {Encoding | Options | null | undefined} [encoding]
  68. * Character encoding for when `value` is `Buffer`.
  69. * @param {Options | null | undefined} [options]
  70. * Configuration.
  71. * @returns {Root}
  72. * mdast tree.
  73. */
  74. export function fromMarkdown(value, encoding, options) {
  75. if (encoding && typeof encoding === 'object') {
  76. options = encoding
  77. encoding = undefined
  78. }
  79. return compiler(options)(
  80. postprocess(
  81. parse(options)
  82. .document()
  83. .write(preprocess()(value, encoding, true))
  84. )
  85. )
  86. }
  87. /**
  88. * Note this compiler only understand complete buffering, not streaming.
  89. *
  90. * @param {Options | null | undefined} [options]
  91. */
  92. function compiler(options) {
  93. /** @type {Config} */
  94. const config = {
  95. transforms: [],
  96. canContainEols: ['emphasis', 'fragment', 'heading', 'paragraph', 'strong'],
  97. enter: {
  98. autolink: opener(link),
  99. autolinkProtocol: onenterdata,
  100. autolinkEmail: onenterdata,
  101. atxHeading: opener(heading),
  102. blockQuote: opener(blockQuote),
  103. characterEscape: onenterdata,
  104. characterReference: onenterdata,
  105. codeFenced: opener(codeFlow),
  106. codeFencedFenceInfo: buffer,
  107. codeFencedFenceMeta: buffer,
  108. codeIndented: opener(codeFlow, buffer),
  109. codeText: opener(codeText, buffer),
  110. codeTextData: onenterdata,
  111. data: onenterdata,
  112. codeFlowValue: onenterdata,
  113. definition: opener(definition),
  114. definitionDestinationString: buffer,
  115. definitionLabelString: buffer,
  116. definitionTitleString: buffer,
  117. emphasis: opener(emphasis),
  118. hardBreakEscape: opener(hardBreak),
  119. hardBreakTrailing: opener(hardBreak),
  120. htmlFlow: opener(html, buffer),
  121. htmlFlowData: onenterdata,
  122. htmlText: opener(html, buffer),
  123. htmlTextData: onenterdata,
  124. image: opener(image),
  125. label: buffer,
  126. link: opener(link),
  127. listItem: opener(listItem),
  128. listItemValue: onenterlistitemvalue,
  129. listOrdered: opener(list, onenterlistordered),
  130. listUnordered: opener(list),
  131. paragraph: opener(paragraph),
  132. reference: onenterreference,
  133. referenceString: buffer,
  134. resourceDestinationString: buffer,
  135. resourceTitleString: buffer,
  136. setextHeading: opener(heading),
  137. strong: opener(strong),
  138. thematicBreak: opener(thematicBreak)
  139. },
  140. exit: {
  141. atxHeading: closer(),
  142. atxHeadingSequence: onexitatxheadingsequence,
  143. autolink: closer(),
  144. autolinkEmail: onexitautolinkemail,
  145. autolinkProtocol: onexitautolinkprotocol,
  146. blockQuote: closer(),
  147. characterEscapeValue: onexitdata,
  148. characterReferenceMarkerHexadecimal: onexitcharacterreferencemarker,
  149. characterReferenceMarkerNumeric: onexitcharacterreferencemarker,
  150. characterReferenceValue: onexitcharacterreferencevalue,
  151. characterReference: onexitcharacterreference,
  152. codeFenced: closer(onexitcodefenced),
  153. codeFencedFence: onexitcodefencedfence,
  154. codeFencedFenceInfo: onexitcodefencedfenceinfo,
  155. codeFencedFenceMeta: onexitcodefencedfencemeta,
  156. codeFlowValue: onexitdata,
  157. codeIndented: closer(onexitcodeindented),
  158. codeText: closer(onexitcodetext),
  159. codeTextData: onexitdata,
  160. data: onexitdata,
  161. definition: closer(),
  162. definitionDestinationString: onexitdefinitiondestinationstring,
  163. definitionLabelString: onexitdefinitionlabelstring,
  164. definitionTitleString: onexitdefinitiontitlestring,
  165. emphasis: closer(),
  166. hardBreakEscape: closer(onexithardbreak),
  167. hardBreakTrailing: closer(onexithardbreak),
  168. htmlFlow: closer(onexithtmlflow),
  169. htmlFlowData: onexitdata,
  170. htmlText: closer(onexithtmltext),
  171. htmlTextData: onexitdata,
  172. image: closer(onexitimage),
  173. label: onexitlabel,
  174. labelText: onexitlabeltext,
  175. lineEnding: onexitlineending,
  176. link: closer(onexitlink),
  177. listItem: closer(),
  178. listOrdered: closer(),
  179. listUnordered: closer(),
  180. paragraph: closer(),
  181. referenceString: onexitreferencestring,
  182. resourceDestinationString: onexitresourcedestinationstring,
  183. resourceTitleString: onexitresourcetitlestring,
  184. resource: onexitresource,
  185. setextHeading: closer(onexitsetextheading),
  186. setextHeadingLineSequence: onexitsetextheadinglinesequence,
  187. setextHeadingText: onexitsetextheadingtext,
  188. strong: closer(),
  189. thematicBreak: closer()
  190. }
  191. }
  192. configure(config, (options || {}).mdastExtensions || [])
  193. /** @type {CompileData} */
  194. const data = {}
  195. return compile
  196. /**
  197. * Turn micromark events into an mdast tree.
  198. *
  199. * @param {Array<Event>} events
  200. * Events.
  201. * @returns {Root}
  202. * mdast tree.
  203. */
  204. function compile(events) {
  205. /** @type {Root} */
  206. let tree = {type: 'root', children: []}
  207. /** @type {Omit<CompileContext, 'sliceSerialize'>} */
  208. const context = {
  209. stack: [tree],
  210. tokenStack: [],
  211. config,
  212. enter,
  213. exit,
  214. buffer,
  215. resume,
  216. data
  217. }
  218. /** @type {Array<number>} */
  219. const listStack = []
  220. let index = -1
  221. while (++index < events.length) {
  222. // We preprocess lists to add `listItem` tokens, and to infer whether
  223. // items the list itself are spread out.
  224. if (
  225. events[index][1].type === types.listOrdered ||
  226. events[index][1].type === types.listUnordered
  227. ) {
  228. if (events[index][0] === 'enter') {
  229. listStack.push(index)
  230. } else {
  231. const tail = listStack.pop()
  232. assert(typeof tail === 'number', 'expected list to be open')
  233. index = prepareList(events, tail, index)
  234. }
  235. }
  236. }
  237. index = -1
  238. while (++index < events.length) {
  239. const handler = config[events[index][0]]
  240. if (own.call(handler, events[index][1].type)) {
  241. handler[events[index][1].type].call(
  242. Object.assign(
  243. {sliceSerialize: events[index][2].sliceSerialize},
  244. context
  245. ),
  246. events[index][1]
  247. )
  248. }
  249. }
  250. // Handle tokens still being open.
  251. if (context.tokenStack.length > 0) {
  252. const tail = context.tokenStack[context.tokenStack.length - 1]
  253. const handler = tail[1] || defaultOnError
  254. handler.call(context, undefined, tail[0])
  255. }
  256. // Figure out `root` position.
  257. tree.position = {
  258. start: point(
  259. events.length > 0 ? events[0][1].start : {line: 1, column: 1, offset: 0}
  260. ),
  261. end: point(
  262. events.length > 0
  263. ? events[events.length - 2][1].end
  264. : {line: 1, column: 1, offset: 0}
  265. )
  266. }
  267. // Call transforms.
  268. index = -1
  269. while (++index < config.transforms.length) {
  270. tree = config.transforms[index](tree) || tree
  271. }
  272. return tree
  273. }
  274. /**
  275. * @param {Array<Event>} events
  276. * @param {number} start
  277. * @param {number} length
  278. * @returns {number}
  279. */
  280. function prepareList(events, start, length) {
  281. let index = start - 1
  282. let containerBalance = -1
  283. let listSpread = false
  284. /** @type {Token | undefined} */
  285. let listItem
  286. /** @type {number | undefined} */
  287. let lineIndex
  288. /** @type {number | undefined} */
  289. let firstBlankLineIndex
  290. /** @type {boolean | undefined} */
  291. let atMarker
  292. while (++index <= length) {
  293. const event = events[index]
  294. switch (event[1].type) {
  295. case types.listUnordered:
  296. case types.listOrdered:
  297. case types.blockQuote: {
  298. if (event[0] === 'enter') {
  299. containerBalance++
  300. } else {
  301. containerBalance--
  302. }
  303. atMarker = undefined
  304. break
  305. }
  306. case types.lineEndingBlank: {
  307. if (event[0] === 'enter') {
  308. if (
  309. listItem &&
  310. !atMarker &&
  311. !containerBalance &&
  312. !firstBlankLineIndex
  313. ) {
  314. firstBlankLineIndex = index
  315. }
  316. atMarker = undefined
  317. }
  318. break
  319. }
  320. case types.linePrefix:
  321. case types.listItemValue:
  322. case types.listItemMarker:
  323. case types.listItemPrefix:
  324. case types.listItemPrefixWhitespace: {
  325. // Empty.
  326. break
  327. }
  328. default: {
  329. atMarker = undefined
  330. }
  331. }
  332. if (
  333. (!containerBalance &&
  334. event[0] === 'enter' &&
  335. event[1].type === types.listItemPrefix) ||
  336. (containerBalance === -1 &&
  337. event[0] === 'exit' &&
  338. (event[1].type === types.listUnordered ||
  339. event[1].type === types.listOrdered))
  340. ) {
  341. if (listItem) {
  342. let tailIndex = index
  343. lineIndex = undefined
  344. while (tailIndex--) {
  345. const tailEvent = events[tailIndex]
  346. if (
  347. tailEvent[1].type === types.lineEnding ||
  348. tailEvent[1].type === types.lineEndingBlank
  349. ) {
  350. if (tailEvent[0] === 'exit') continue
  351. if (lineIndex) {
  352. events[lineIndex][1].type = types.lineEndingBlank
  353. listSpread = true
  354. }
  355. tailEvent[1].type = types.lineEnding
  356. lineIndex = tailIndex
  357. } else if (
  358. tailEvent[1].type === types.linePrefix ||
  359. tailEvent[1].type === types.blockQuotePrefix ||
  360. tailEvent[1].type === types.blockQuotePrefixWhitespace ||
  361. tailEvent[1].type === types.blockQuoteMarker ||
  362. tailEvent[1].type === types.listItemIndent
  363. ) {
  364. // Empty
  365. } else {
  366. break
  367. }
  368. }
  369. if (
  370. firstBlankLineIndex &&
  371. (!lineIndex || firstBlankLineIndex < lineIndex)
  372. ) {
  373. listItem._spread = true
  374. }
  375. // Fix position.
  376. listItem.end = Object.assign(
  377. {},
  378. lineIndex ? events[lineIndex][1].start : event[1].end
  379. )
  380. events.splice(lineIndex || index, 0, ['exit', listItem, event[2]])
  381. index++
  382. length++
  383. }
  384. // Create a new list item.
  385. if (event[1].type === types.listItemPrefix) {
  386. /** @type {Token} */
  387. const item = {
  388. type: 'listItem',
  389. _spread: false,
  390. start: Object.assign({}, event[1].start),
  391. // @ts-expect-error: we’ll add `end` in a second.
  392. end: undefined
  393. }
  394. listItem = item
  395. events.splice(index, 0, ['enter', item, event[2]])
  396. index++
  397. length++
  398. firstBlankLineIndex = undefined
  399. atMarker = true
  400. }
  401. }
  402. }
  403. events[start][1]._spread = listSpread
  404. return length
  405. }
  406. /**
  407. * Create an opener handle.
  408. *
  409. * @param {(token: Token) => Nodes} create
  410. * Create a node.
  411. * @param {Handle | undefined} [and]
  412. * Optional function to also run.
  413. * @returns {Handle}
  414. * Handle.
  415. */
  416. function opener(create, and) {
  417. return open
  418. /**
  419. * @this {CompileContext}
  420. * @param {Token} token
  421. * @returns {undefined}
  422. */
  423. function open(token) {
  424. enter.call(this, create(token), token)
  425. if (and) and.call(this, token)
  426. }
  427. }
  428. /**
  429. * @type {CompileContext['buffer']}
  430. */
  431. function buffer() {
  432. this.stack.push({type: 'fragment', children: []})
  433. }
  434. /**
  435. * @type {CompileContext['enter']}
  436. */
  437. function enter(node, token, errorHandler) {
  438. const parent = this.stack[this.stack.length - 1]
  439. assert(parent, 'expected `parent`')
  440. assert('children' in parent, 'expected `parent`')
  441. /** @type {Array<Nodes>} */
  442. const siblings = parent.children
  443. siblings.push(node)
  444. this.stack.push(node)
  445. this.tokenStack.push([token, errorHandler || undefined])
  446. node.position = {
  447. start: point(token.start),
  448. // @ts-expect-error: `end` will be patched later.
  449. end: undefined
  450. }
  451. }
  452. /**
  453. * Create a closer handle.
  454. *
  455. * @param {Handle | undefined} [and]
  456. * Optional function to also run.
  457. * @returns {Handle}
  458. * Handle.
  459. */
  460. function closer(and) {
  461. return close
  462. /**
  463. * @this {CompileContext}
  464. * @param {Token} token
  465. * @returns {undefined}
  466. */
  467. function close(token) {
  468. if (and) and.call(this, token)
  469. exit.call(this, token)
  470. }
  471. }
  472. /**
  473. * @type {CompileContext['exit']}
  474. */
  475. function exit(token, onExitError) {
  476. const node = this.stack.pop()
  477. assert(node, 'expected `node`')
  478. const open = this.tokenStack.pop()
  479. if (!open) {
  480. throw new Error(
  481. 'Cannot close `' +
  482. token.type +
  483. '` (' +
  484. stringifyPosition({start: token.start, end: token.end}) +
  485. '): it’s not open'
  486. )
  487. } else if (open[0].type !== token.type) {
  488. if (onExitError) {
  489. onExitError.call(this, token, open[0])
  490. } else {
  491. const handler = open[1] || defaultOnError
  492. handler.call(this, token, open[0])
  493. }
  494. }
  495. assert(node.type !== 'fragment', 'unexpected fragment `exit`ed')
  496. assert(node.position, 'expected `position` to be defined')
  497. node.position.end = point(token.end)
  498. }
  499. /**
  500. * @type {CompileContext['resume']}
  501. */
  502. function resume() {
  503. return toString(this.stack.pop())
  504. }
  505. //
  506. // Handlers.
  507. //
  508. /**
  509. * @this {CompileContext}
  510. * @type {Handle}
  511. */
  512. function onenterlistordered() {
  513. this.data.expectingFirstListItemValue = true
  514. }
  515. /**
  516. * @this {CompileContext}
  517. * @type {Handle}
  518. */
  519. function onenterlistitemvalue(token) {
  520. if (this.data.expectingFirstListItemValue) {
  521. const ancestor = this.stack[this.stack.length - 2]
  522. assert(ancestor, 'expected nodes on stack')
  523. assert(ancestor.type === 'list', 'expected list on stack')
  524. ancestor.start = Number.parseInt(
  525. this.sliceSerialize(token),
  526. constants.numericBaseDecimal
  527. )
  528. this.data.expectingFirstListItemValue = undefined
  529. }
  530. }
  531. /**
  532. * @this {CompileContext}
  533. * @type {Handle}
  534. */
  535. function onexitcodefencedfenceinfo() {
  536. const data = this.resume()
  537. const node = this.stack[this.stack.length - 1]
  538. assert(node, 'expected node on stack')
  539. assert(node.type === 'code', 'expected code on stack')
  540. node.lang = data
  541. }
  542. /**
  543. * @this {CompileContext}
  544. * @type {Handle}
  545. */
  546. function onexitcodefencedfencemeta() {
  547. const data = this.resume()
  548. const node = this.stack[this.stack.length - 1]
  549. assert(node, 'expected node on stack')
  550. assert(node.type === 'code', 'expected code on stack')
  551. node.meta = data
  552. }
  553. /**
  554. * @this {CompileContext}
  555. * @type {Handle}
  556. */
  557. function onexitcodefencedfence() {
  558. // Exit if this is the closing fence.
  559. if (this.data.flowCodeInside) return
  560. this.buffer()
  561. this.data.flowCodeInside = true
  562. }
  563. /**
  564. * @this {CompileContext}
  565. * @type {Handle}
  566. */
  567. function onexitcodefenced() {
  568. const data = this.resume()
  569. const node = this.stack[this.stack.length - 1]
  570. assert(node, 'expected node on stack')
  571. assert(node.type === 'code', 'expected code on stack')
  572. node.value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
  573. this.data.flowCodeInside = undefined
  574. }
  575. /**
  576. * @this {CompileContext}
  577. * @type {Handle}
  578. */
  579. function onexitcodeindented() {
  580. const data = this.resume()
  581. const node = this.stack[this.stack.length - 1]
  582. assert(node, 'expected node on stack')
  583. assert(node.type === 'code', 'expected code on stack')
  584. node.value = data.replace(/(\r?\n|\r)$/g, '')
  585. }
  586. /**
  587. * @this {CompileContext}
  588. * @type {Handle}
  589. */
  590. function onexitdefinitionlabelstring(token) {
  591. const label = this.resume()
  592. const node = this.stack[this.stack.length - 1]
  593. assert(node, 'expected node on stack')
  594. assert(node.type === 'definition', 'expected definition on stack')
  595. node.label = label
  596. node.identifier = normalizeIdentifier(
  597. this.sliceSerialize(token)
  598. ).toLowerCase()
  599. }
  600. /**
  601. * @this {CompileContext}
  602. * @type {Handle}
  603. */
  604. function onexitdefinitiontitlestring() {
  605. const data = this.resume()
  606. const node = this.stack[this.stack.length - 1]
  607. assert(node, 'expected node on stack')
  608. assert(node.type === 'definition', 'expected definition on stack')
  609. node.title = data
  610. }
  611. /**
  612. * @this {CompileContext}
  613. * @type {Handle}
  614. */
  615. function onexitdefinitiondestinationstring() {
  616. const data = this.resume()
  617. const node = this.stack[this.stack.length - 1]
  618. assert(node, 'expected node on stack')
  619. assert(node.type === 'definition', 'expected definition on stack')
  620. node.url = data
  621. }
  622. /**
  623. * @this {CompileContext}
  624. * @type {Handle}
  625. */
  626. function onexitatxheadingsequence(token) {
  627. const node = this.stack[this.stack.length - 1]
  628. assert(node, 'expected node on stack')
  629. assert(node.type === 'heading', 'expected heading on stack')
  630. if (!node.depth) {
  631. const depth = this.sliceSerialize(token).length
  632. assert(
  633. depth === 1 ||
  634. depth === 2 ||
  635. depth === 3 ||
  636. depth === 4 ||
  637. depth === 5 ||
  638. depth === 6,
  639. 'expected `depth` between `1` and `6`'
  640. )
  641. node.depth = depth
  642. }
  643. }
  644. /**
  645. * @this {CompileContext}
  646. * @type {Handle}
  647. */
  648. function onexitsetextheadingtext() {
  649. this.data.setextHeadingSlurpLineEnding = true
  650. }
  651. /**
  652. * @this {CompileContext}
  653. * @type {Handle}
  654. */
  655. function onexitsetextheadinglinesequence(token) {
  656. const node = this.stack[this.stack.length - 1]
  657. assert(node, 'expected node on stack')
  658. assert(node.type === 'heading', 'expected heading on stack')
  659. node.depth =
  660. this.sliceSerialize(token).codePointAt(0) === codes.equalsTo ? 1 : 2
  661. }
  662. /**
  663. * @this {CompileContext}
  664. * @type {Handle}
  665. */
  666. function onexitsetextheading() {
  667. this.data.setextHeadingSlurpLineEnding = undefined
  668. }
  669. /**
  670. * @this {CompileContext}
  671. * @type {Handle}
  672. */
  673. function onenterdata(token) {
  674. const node = this.stack[this.stack.length - 1]
  675. assert(node, 'expected node on stack')
  676. assert('children' in node, 'expected parent on stack')
  677. /** @type {Array<Nodes>} */
  678. const siblings = node.children
  679. let tail = siblings[siblings.length - 1]
  680. if (!tail || tail.type !== 'text') {
  681. // Add a new text node.
  682. tail = text()
  683. tail.position = {
  684. start: point(token.start),
  685. // @ts-expect-error: we’ll add `end` later.
  686. end: undefined
  687. }
  688. siblings.push(tail)
  689. }
  690. this.stack.push(tail)
  691. }
  692. /**
  693. * @this {CompileContext}
  694. * @type {Handle}
  695. */
  696. function onexitdata(token) {
  697. const tail = this.stack.pop()
  698. assert(tail, 'expected a `node` to be on the stack')
  699. assert('value' in tail, 'expected a `literal` to be on the stack')
  700. assert(tail.position, 'expected `node` to have an open position')
  701. tail.value += this.sliceSerialize(token)
  702. tail.position.end = point(token.end)
  703. }
  704. /**
  705. * @this {CompileContext}
  706. * @type {Handle}
  707. */
  708. function onexitlineending(token) {
  709. const context = this.stack[this.stack.length - 1]
  710. assert(context, 'expected `node`')
  711. // If we’re at a hard break, include the line ending in there.
  712. if (this.data.atHardBreak) {
  713. assert('children' in context, 'expected `parent`')
  714. const tail = context.children[context.children.length - 1]
  715. assert(tail.position, 'expected tail to have a starting position')
  716. tail.position.end = point(token.end)
  717. this.data.atHardBreak = undefined
  718. return
  719. }
  720. if (
  721. !this.data.setextHeadingSlurpLineEnding &&
  722. config.canContainEols.includes(context.type)
  723. ) {
  724. onenterdata.call(this, token)
  725. onexitdata.call(this, token)
  726. }
  727. }
  728. /**
  729. * @this {CompileContext}
  730. * @type {Handle}
  731. */
  732. function onexithardbreak() {
  733. this.data.atHardBreak = true
  734. }
  735. /**
  736. * @this {CompileContext}
  737. * @type {Handle}
  738. */
  739. function onexithtmlflow() {
  740. const data = this.resume()
  741. const node = this.stack[this.stack.length - 1]
  742. assert(node, 'expected node on stack')
  743. assert(node.type === 'html', 'expected html on stack')
  744. node.value = data
  745. }
  746. /**
  747. * @this {CompileContext}
  748. * @type {Handle}
  749. */
  750. function onexithtmltext() {
  751. const data = this.resume()
  752. const node = this.stack[this.stack.length - 1]
  753. assert(node, 'expected node on stack')
  754. assert(node.type === 'html', 'expected html on stack')
  755. node.value = data
  756. }
  757. /**
  758. * @this {CompileContext}
  759. * @type {Handle}
  760. */
  761. function onexitcodetext() {
  762. const data = this.resume()
  763. const node = this.stack[this.stack.length - 1]
  764. assert(node, 'expected node on stack')
  765. assert(node.type === 'inlineCode', 'expected inline code on stack')
  766. node.value = data
  767. }
  768. /**
  769. * @this {CompileContext}
  770. * @type {Handle}
  771. */
  772. function onexitlink() {
  773. const node = this.stack[this.stack.length - 1]
  774. assert(node, 'expected node on stack')
  775. assert(node.type === 'link', 'expected link on stack')
  776. // Note: there are also `identifier` and `label` fields on this link node!
  777. // These are used / cleaned here.
  778. // To do: clean.
  779. if (this.data.inReference) {
  780. /** @type {ReferenceType} */
  781. const referenceType = this.data.referenceType || 'shortcut'
  782. node.type += 'Reference'
  783. // @ts-expect-error: mutate.
  784. node.referenceType = referenceType
  785. // @ts-expect-error: mutate.
  786. delete node.url
  787. delete node.title
  788. } else {
  789. // @ts-expect-error: mutate.
  790. delete node.identifier
  791. // @ts-expect-error: mutate.
  792. delete node.label
  793. }
  794. this.data.referenceType = undefined
  795. }
  796. /**
  797. * @this {CompileContext}
  798. * @type {Handle}
  799. */
  800. function onexitimage() {
  801. const node = this.stack[this.stack.length - 1]
  802. assert(node, 'expected node on stack')
  803. assert(node.type === 'image', 'expected image on stack')
  804. // Note: there are also `identifier` and `label` fields on this link node!
  805. // These are used / cleaned here.
  806. // To do: clean.
  807. if (this.data.inReference) {
  808. /** @type {ReferenceType} */
  809. const referenceType = this.data.referenceType || 'shortcut'
  810. node.type += 'Reference'
  811. // @ts-expect-error: mutate.
  812. node.referenceType = referenceType
  813. // @ts-expect-error: mutate.
  814. delete node.url
  815. delete node.title
  816. } else {
  817. // @ts-expect-error: mutate.
  818. delete node.identifier
  819. // @ts-expect-error: mutate.
  820. delete node.label
  821. }
  822. this.data.referenceType = undefined
  823. }
  824. /**
  825. * @this {CompileContext}
  826. * @type {Handle}
  827. */
  828. function onexitlabeltext(token) {
  829. const string = this.sliceSerialize(token)
  830. const ancestor = this.stack[this.stack.length - 2]
  831. assert(ancestor, 'expected ancestor on stack')
  832. assert(
  833. ancestor.type === 'image' || ancestor.type === 'link',
  834. 'expected image or link on stack'
  835. )
  836. // @ts-expect-error: stash this on the node, as it might become a reference
  837. // later.
  838. ancestor.label = decodeString(string)
  839. // @ts-expect-error: same as above.
  840. ancestor.identifier = normalizeIdentifier(string).toLowerCase()
  841. }
  842. /**
  843. * @this {CompileContext}
  844. * @type {Handle}
  845. */
  846. function onexitlabel() {
  847. const fragment = this.stack[this.stack.length - 1]
  848. assert(fragment, 'expected node on stack')
  849. assert(fragment.type === 'fragment', 'expected fragment on stack')
  850. const value = this.resume()
  851. const node = this.stack[this.stack.length - 1]
  852. assert(node, 'expected node on stack')
  853. assert(
  854. node.type === 'image' || node.type === 'link',
  855. 'expected image or link on stack'
  856. )
  857. // Assume a reference.
  858. this.data.inReference = true
  859. if (node.type === 'link') {
  860. /** @type {Array<PhrasingContent>} */
  861. const children = fragment.children
  862. node.children = children
  863. } else {
  864. node.alt = value
  865. }
  866. }
  867. /**
  868. * @this {CompileContext}
  869. * @type {Handle}
  870. */
  871. function onexitresourcedestinationstring() {
  872. const data = this.resume()
  873. const node = this.stack[this.stack.length - 1]
  874. assert(node, 'expected node on stack')
  875. assert(
  876. node.type === 'image' || node.type === 'link',
  877. 'expected image or link on stack'
  878. )
  879. node.url = data
  880. }
  881. /**
  882. * @this {CompileContext}
  883. * @type {Handle}
  884. */
  885. function onexitresourcetitlestring() {
  886. const data = this.resume()
  887. const node = this.stack[this.stack.length - 1]
  888. assert(node, 'expected node on stack')
  889. assert(
  890. node.type === 'image' || node.type === 'link',
  891. 'expected image or link on stack'
  892. )
  893. node.title = data
  894. }
  895. /**
  896. * @this {CompileContext}
  897. * @type {Handle}
  898. */
  899. function onexitresource() {
  900. this.data.inReference = undefined
  901. }
  902. /**
  903. * @this {CompileContext}
  904. * @type {Handle}
  905. */
  906. function onenterreference() {
  907. this.data.referenceType = 'collapsed'
  908. }
  909. /**
  910. * @this {CompileContext}
  911. * @type {Handle}
  912. */
  913. function onexitreferencestring(token) {
  914. const label = this.resume()
  915. const node = this.stack[this.stack.length - 1]
  916. assert(node, 'expected node on stack')
  917. assert(
  918. node.type === 'image' || node.type === 'link',
  919. 'expected image reference or link reference on stack'
  920. )
  921. // @ts-expect-error: stash this on the node, as it might become a reference
  922. // later.
  923. node.label = label
  924. // @ts-expect-error: same as above.
  925. node.identifier = normalizeIdentifier(
  926. this.sliceSerialize(token)
  927. ).toLowerCase()
  928. this.data.referenceType = 'full'
  929. }
  930. /**
  931. * @this {CompileContext}
  932. * @type {Handle}
  933. */
  934. function onexitcharacterreferencemarker(token) {
  935. assert(
  936. token.type === 'characterReferenceMarkerNumeric' ||
  937. token.type === 'characterReferenceMarkerHexadecimal'
  938. )
  939. this.data.characterReferenceType = token.type
  940. }
  941. /**
  942. * @this {CompileContext}
  943. * @type {Handle}
  944. */
  945. function onexitcharacterreferencevalue(token) {
  946. const data = this.sliceSerialize(token)
  947. const type = this.data.characterReferenceType
  948. /** @type {string} */
  949. let value
  950. if (type) {
  951. value = decodeNumericCharacterReference(
  952. data,
  953. type === types.characterReferenceMarkerNumeric
  954. ? constants.numericBaseDecimal
  955. : constants.numericBaseHexadecimal
  956. )
  957. this.data.characterReferenceType = undefined
  958. } else {
  959. const result = decodeNamedCharacterReference(data)
  960. assert(result !== false, 'expected reference to decode')
  961. value = result
  962. }
  963. const tail = this.stack[this.stack.length - 1]
  964. assert(tail, 'expected `node`')
  965. assert('value' in tail, 'expected `node.value`')
  966. tail.value += value
  967. }
  968. /**
  969. * @this {CompileContext}
  970. * @type {Handle}
  971. */
  972. function onexitcharacterreference(token) {
  973. const tail = this.stack.pop()
  974. assert(tail, 'expected `node`')
  975. assert(tail.position, 'expected `node.position`')
  976. tail.position.end = point(token.end)
  977. }
  978. /**
  979. * @this {CompileContext}
  980. * @type {Handle}
  981. */
  982. function onexitautolinkprotocol(token) {
  983. onexitdata.call(this, token)
  984. const node = this.stack[this.stack.length - 1]
  985. assert(node, 'expected node on stack')
  986. assert(node.type === 'link', 'expected link on stack')
  987. node.url = this.sliceSerialize(token)
  988. }
  989. /**
  990. * @this {CompileContext}
  991. * @type {Handle}
  992. */
  993. function onexitautolinkemail(token) {
  994. onexitdata.call(this, token)
  995. const node = this.stack[this.stack.length - 1]
  996. assert(node, 'expected node on stack')
  997. assert(node.type === 'link', 'expected link on stack')
  998. node.url = 'mailto:' + this.sliceSerialize(token)
  999. }
  1000. //
  1001. // Creaters.
  1002. //
  1003. /** @returns {Blockquote} */
  1004. function blockQuote() {
  1005. return {type: 'blockquote', children: []}
  1006. }
  1007. /** @returns {Code} */
  1008. function codeFlow() {
  1009. return {type: 'code', lang: null, meta: null, value: ''}
  1010. }
  1011. /** @returns {InlineCode} */
  1012. function codeText() {
  1013. return {type: 'inlineCode', value: ''}
  1014. }
  1015. /** @returns {Definition} */
  1016. function definition() {
  1017. return {
  1018. type: 'definition',
  1019. identifier: '',
  1020. label: null,
  1021. title: null,
  1022. url: ''
  1023. }
  1024. }
  1025. /** @returns {Emphasis} */
  1026. function emphasis() {
  1027. return {type: 'emphasis', children: []}
  1028. }
  1029. /** @returns {Heading} */
  1030. function heading() {
  1031. return {
  1032. type: 'heading',
  1033. // @ts-expect-error `depth` will be set later.
  1034. depth: 0,
  1035. children: []
  1036. }
  1037. }
  1038. /** @returns {Break} */
  1039. function hardBreak() {
  1040. return {type: 'break'}
  1041. }
  1042. /** @returns {Html} */
  1043. function html() {
  1044. return {type: 'html', value: ''}
  1045. }
  1046. /** @returns {Image} */
  1047. function image() {
  1048. return {type: 'image', title: null, url: '', alt: null}
  1049. }
  1050. /** @returns {Link} */
  1051. function link() {
  1052. return {type: 'link', title: null, url: '', children: []}
  1053. }
  1054. /**
  1055. * @param {Token} token
  1056. * @returns {List}
  1057. */
  1058. function list(token) {
  1059. return {
  1060. type: 'list',
  1061. ordered: token.type === 'listOrdered',
  1062. start: null,
  1063. spread: token._spread,
  1064. children: []
  1065. }
  1066. }
  1067. /**
  1068. * @param {Token} token
  1069. * @returns {ListItem}
  1070. */
  1071. function listItem(token) {
  1072. return {
  1073. type: 'listItem',
  1074. spread: token._spread,
  1075. checked: null,
  1076. children: []
  1077. }
  1078. }
  1079. /** @returns {Paragraph} */
  1080. function paragraph() {
  1081. return {type: 'paragraph', children: []}
  1082. }
  1083. /** @returns {Strong} */
  1084. function strong() {
  1085. return {type: 'strong', children: []}
  1086. }
  1087. /** @returns {Text} */
  1088. function text() {
  1089. return {type: 'text', value: ''}
  1090. }
  1091. /** @returns {ThematicBreak} */
  1092. function thematicBreak() {
  1093. return {type: 'thematicBreak'}
  1094. }
  1095. }
  1096. /**
  1097. * Copy a point-like value.
  1098. *
  1099. * @param {Point} d
  1100. * Point-like value.
  1101. * @returns {Point}
  1102. * unist point.
  1103. */
  1104. function point(d) {
  1105. return {line: d.line, column: d.column, offset: d.offset}
  1106. }
  1107. /**
  1108. * @param {Config} combined
  1109. * @param {Array<Array<Extension> | Extension>} extensions
  1110. * @returns {undefined}
  1111. */
  1112. function configure(combined, extensions) {
  1113. let index = -1
  1114. while (++index < extensions.length) {
  1115. const value = extensions[index]
  1116. if (Array.isArray(value)) {
  1117. configure(combined, value)
  1118. } else {
  1119. extension(combined, value)
  1120. }
  1121. }
  1122. }
  1123. /**
  1124. * @param {Config} combined
  1125. * @param {Extension} extension
  1126. * @returns {undefined}
  1127. */
  1128. function extension(combined, extension) {
  1129. /** @type {keyof Extension} */
  1130. let key
  1131. for (key in extension) {
  1132. if (own.call(extension, key)) {
  1133. switch (key) {
  1134. case 'canContainEols': {
  1135. const right = extension[key]
  1136. if (right) {
  1137. combined[key].push(...right)
  1138. }
  1139. break
  1140. }
  1141. case 'transforms': {
  1142. const right = extension[key]
  1143. if (right) {
  1144. combined[key].push(...right)
  1145. }
  1146. break
  1147. }
  1148. case 'enter':
  1149. case 'exit': {
  1150. const right = extension[key]
  1151. if (right) {
  1152. Object.assign(combined[key], right)
  1153. }
  1154. break
  1155. }
  1156. // No default
  1157. }
  1158. }
  1159. }
  1160. }
  1161. /** @type {OnEnterError} */
  1162. function defaultOnError(left, right) {
  1163. if (left) {
  1164. throw new Error(
  1165. 'Cannot close `' +
  1166. left.type +
  1167. '` (' +
  1168. stringifyPosition({start: left.start, end: left.end}) +
  1169. '): a different token (`' +
  1170. right.type +
  1171. '`, ' +
  1172. stringifyPosition({start: right.start, end: right.end}) +
  1173. ') is open'
  1174. )
  1175. } else {
  1176. throw new Error(
  1177. 'Cannot close document, a token (`' +
  1178. right.type +
  1179. '`, ' +
  1180. stringifyPosition({start: right.start, end: right.end}) +
  1181. ') is still open'
  1182. )
  1183. }
  1184. }