index.d.ts 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. // Note: this file is authored manually, not generated from `index.js`.
  2. /**
  3. * A character code.
  4. *
  5. * This is often the same as what `String#charCodeAt()` yields but micromark
  6. * adds meaning to certain other values.
  7. *
  8. * `null` represents the end of the input stream (called eof).
  9. * Negative integers are used instead of certain sequences of characters (such
  10. * as line endings and tabs).
  11. */
  12. export type Code = number | null
  13. /**
  14. * A chunk is either a character code or a slice of a buffer in the form of a
  15. * string.
  16. *
  17. * Chunks are used because strings are more efficient storage that character
  18. * codes, but limited in what they can represent.
  19. */
  20. export type Chunk = Code | string
  21. /**
  22. * Enumeration of the content types.
  23. *
  24. * Technically `document` is also a content type, which includes containers
  25. * (lists, block quotes) and flow.
  26. * As `ContentType` is used on tokens to define the type of subcontent but
  27. * `document` is the highest level of content, so it’s not listed here.
  28. *
  29. * Containers in markdown come from the margin and include more constructs
  30. * on the lines that define them.
  31. * Take for example a block quote with a paragraph inside it (such as
  32. * `> asd`).
  33. *
  34. * `flow` represents the sections, such as headings, code, and content, which
  35. * is also parsed per line
  36. * An example is HTML, which has a certain starting condition (such as
  37. * `<script>` on its own line), then continues for a while, until an end
  38. * condition is found (such as `</style>`).
  39. * If that line with an end condition is never found, that flow goes until
  40. * the end.
  41. *
  42. * `content` is zero or more definitions, and then zero or one paragraph.
  43. * It’s a weird one, and needed to make certain edge cases around definitions
  44. * spec compliant.
  45. * Definitions are unlike other things in markdown, in that they behave like
  46. * `text` in that they can contain arbitrary line endings, but *have* to end
  47. * at a line ending.
  48. * If they end in something else, the whole definition instead is seen as a
  49. * paragraph.
  50. *
  51. * The content in markdown first needs to be parsed up to this level to
  52. * figure out which things are defined, for the whole document, before
  53. * continuing on with `text`, as whether a link or image reference forms or
  54. * not depends on whether it’s defined.
  55. * This unfortunately prevents a true streaming markdown to HTML compiler.
  56. *
  57. * `text` contains phrasing content such as attention (emphasis, strong),
  58. * media (links, images), and actual text.
  59. *
  60. * `string` is a limited `text` like content type which only allows character
  61. * references and character escapes.
  62. * It exists in things such as identifiers (media references, definitions),
  63. * titles, or URLs.
  64. */
  65. export type ContentType = 'content' | 'document' | 'flow' | 'string' | 'text'
  66. /**
  67. * A location in the document (`line`/`column`/`offset`) and chunk (`_index`,
  68. * `_bufferIndex`).
  69. *
  70. * `_bufferIndex` is `-1` when `_index` points to a code chunk and it’s a
  71. * non-negative integer when pointing to a string chunk.
  72. *
  73. * The interface for the location in the document comes from unist `Point`:
  74. * <https://github.com/syntax-tree/unist#point>
  75. */
  76. export type Point = {
  77. /**
  78. * Position in a string chunk (or `-1` when pointing to a numeric chunk).
  79. */
  80. _bufferIndex: number
  81. /**
  82. * Position in a list of chunks.
  83. */
  84. _index: number
  85. /**
  86. * 1-indexed column number.
  87. */
  88. column: number
  89. /**
  90. * 1-indexed line number.
  91. */
  92. line: number
  93. /**
  94. * 0-indexed position in the document.
  95. */
  96. offset: number
  97. }
  98. /**
  99. * A token: a span of chunks.
  100. *
  101. * Tokens are what the core of micromark produces: the built in HTML compiler
  102. * or other tools can turn them into different things.
  103. *
  104. * Tokens are essentially names attached to a slice of chunks, such as
  105. * `lineEndingBlank` for certain line endings, or `codeFenced` for a whole
  106. * fenced code.
  107. *
  108. * Sometimes, more info is attached to tokens, such as `_open` and `_close`
  109. * by `attention` (strong, emphasis) to signal whether the sequence can open
  110. * or close an attention run.
  111. *
  112. * Linked tokens are used because outer constructs are parsed first.
  113. * Take for example:
  114. *
  115. * ```markdown
  116. * > *a
  117. * b*.
  118. * ```
  119. *
  120. * 1. The block quote marker and the space after it is parsed first
  121. * 2. The rest of the line is a `chunkFlow` token
  122. * 3. The two spaces on the second line are a `linePrefix`
  123. * 4. The rest of the line is another `chunkFlow` token
  124. *
  125. * The two `chunkFlow` tokens are linked together.
  126. * The chunks they span are then passed through the flow tokenizer.
  127. */
  128. export interface Token {
  129. /**
  130. * Token type.
  131. */
  132. type: TokenType
  133. /**
  134. * Point where the token starts.
  135. */
  136. start: Point
  137. /**
  138. * Point where the token ends.
  139. */
  140. end: Point
  141. /**
  142. * The previous token in a list of linked tokens.
  143. */
  144. previous?: Token | undefined
  145. /**
  146. * The next token in a list of linked tokens.
  147. */
  148. next?: Token | undefined
  149. /**
  150. * Declares a token as having content of a certain type.
  151. */
  152. contentType?: ContentType | undefined
  153. /**
  154. * Connected tokenizer.
  155. *
  156. * Used when dealing with linked tokens.
  157. * A child tokenizer is needed to tokenize them, which is stored on those
  158. * tokens.
  159. */
  160. _tokenizer?: TokenizeContext | undefined
  161. /**
  162. * Field to help parse attention.
  163. *
  164. * Depending on the character before sequences (`**`), the sequence can open,
  165. * close, both, or none.
  166. */
  167. _open?: boolean | undefined
  168. /**
  169. * Field to help parse attention.
  170. *
  171. * Depending on the character before sequences (`**`), the sequence can open,
  172. * close, both, or none.
  173. */
  174. _close?: boolean | undefined
  175. /**
  176. * Field to help parse GFM task lists.
  177. *
  178. * This boolean is used internally to figure out if a token is in the first
  179. * content of a list item construct.
  180. */
  181. _isInFirstContentOfListItem?: boolean | undefined
  182. /**
  183. * Field to help parse containers.
  184. *
  185. * This boolean is used internally to figure out if a token is a container
  186. * token.
  187. */
  188. _container?: boolean | undefined
  189. /**
  190. * Field to help parse lists.
  191. *
  192. * This boolean is used internally to figure out if a list is loose or not.
  193. */
  194. _loose?: boolean | undefined
  195. /**
  196. * Field to help parse links.
  197. *
  198. * This boolean is used internally to figure out if a link opening
  199. * can’t be used (because links in links are incorrect).
  200. */
  201. _inactive?: boolean | undefined
  202. /**
  203. * Field to help parse links.
  204. *
  205. * This boolean is used internally to figure out if a link opening is
  206. * balanced: it’s not a link opening but has a balanced closing.
  207. */
  208. _balanced?: boolean | undefined
  209. }
  210. /**
  211. * The start or end of a token amongst other events.
  212. *
  213. * Tokens can “contain” other tokens, even though they are stored in a flat
  214. * list, through `enter`ing before them, and `exit`ing after them.
  215. */
  216. export type Event = ['enter' | 'exit', Token, TokenizeContext]
  217. /**
  218. * Open a token.
  219. *
  220. * @param type
  221. * Token type.
  222. * @param fields
  223. * Extra fields.
  224. * @returns {Token}
  225. * Token.
  226. */
  227. export type Enter = (
  228. type: TokenType,
  229. fields?: Omit<Partial<Token>, 'type'> | undefined
  230. ) => Token
  231. /**
  232. * Close a token.
  233. *
  234. * @param type
  235. * Token type.
  236. * @returns
  237. * Token.
  238. */
  239. export type Exit = (type: TokenType) => Token
  240. /**
  241. * Deal with the character and move to the next.
  242. *
  243. * @param code
  244. * Current code.
  245. */
  246. export type Consume = (code: Code) => undefined
  247. /**
  248. * Attempt deals with several values, and tries to parse according to those
  249. * values.
  250. *
  251. * If a value resulted in `ok`, it worked, the tokens that were made are used,
  252. * and `ok` is switched to.
  253. * If the result is `nok`, the attempt failed, so we revert to the original
  254. * state, and `nok` is used.
  255. *
  256. * @param construct
  257. * Construct(s) to try.
  258. * @param ok
  259. * State to move to when successful.
  260. * @param nok
  261. * State to move to when unsuccessful.
  262. * @returns
  263. * Next state.
  264. */
  265. export type Attempt = (
  266. construct: Array<Construct> | Construct | ConstructRecord,
  267. ok: State,
  268. nok?: State | undefined
  269. ) => State
  270. /**
  271. * A context object to transition the state machine.
  272. */
  273. export type Effects = {
  274. /**
  275. * Start a new token.
  276. */
  277. enter: Enter
  278. /**
  279. * End a started token.
  280. */
  281. exit: Exit
  282. /**
  283. * Deal with the character and move to the next.
  284. */
  285. consume: Consume
  286. /**
  287. * Try to tokenize a construct.
  288. */
  289. attempt: Attempt
  290. /**
  291. * Interrupt is used for stuff right after a line of content.
  292. */
  293. interrupt: Attempt
  294. /**
  295. * Attempt, then revert.
  296. */
  297. check: Attempt
  298. }
  299. /**
  300. * The main unit in the state machine: a function that gets a character code
  301. * and has certain effects.
  302. *
  303. * A state function should return another function: the next
  304. * state-as-a-function to go to.
  305. *
  306. * But there is one case where they return `undefined`: for the eof character
  307. * code (at the end of a value).
  308. * The reason being: well, there isn’t any state that makes sense, so
  309. * `undefined` works well.
  310. * Practically that has also helped: if for some reason it was a mistake, then
  311. * an exception is throw because there is no next function, meaning it
  312. * surfaces early.
  313. *
  314. * @param code
  315. * Current code.
  316. * @returns
  317. * Next state.
  318. */
  319. export type State = (code: Code) => State | undefined
  320. /**
  321. * A resolver handles and cleans events coming from `tokenize`.
  322. *
  323. * @param events
  324. * List of events.
  325. * @param context
  326. * Tokenize context.
  327. * @returns
  328. * The given, modified, events.
  329. */
  330. export type Resolver = (
  331. events: Array<Event>,
  332. context: TokenizeContext
  333. ) => Array<Event>
  334. /**
  335. * A tokenize function sets up a state machine to handle character codes streaming in.
  336. *
  337. * @param this
  338. * Tokenize context.
  339. * @param effects
  340. * Effects.
  341. * @param ok
  342. * State to go to when successful.
  343. * @param nok
  344. * State to go to when unsuccessful.
  345. * @returns
  346. * First state.
  347. */
  348. export type Tokenizer = (
  349. this: TokenizeContext,
  350. effects: Effects,
  351. ok: State,
  352. nok: State
  353. ) => State
  354. /**
  355. * Like a tokenizer, but without `ok` or `nok`.
  356. *
  357. * @param this
  358. * Tokenize context.
  359. * @param effects
  360. * Effects.
  361. * @returns
  362. * First state.
  363. */
  364. export type Initializer = (this: TokenizeContext, effects: Effects) => State
  365. /**
  366. * Like a tokenizer, but without `ok` or `nok`, and returning `undefined`.
  367. *
  368. * This is the final hook when a container must be closed.
  369. *
  370. * @param this
  371. * Tokenize context.
  372. * @param effects
  373. * Effects.
  374. * @returns
  375. * Nothing.
  376. */
  377. export type Exiter = (this: TokenizeContext, effects: Effects) => undefined
  378. /**
  379. * Guard whether `code` can come before the construct.
  380. *
  381. * In certain cases a construct can hook into many potential start characters.
  382. * Instead of setting up an attempt to parse that construct for most
  383. * characters, this is a speedy way to reduce that.
  384. *
  385. * @param this
  386. * Tokenize context.
  387. * @param code
  388. * Previous code.
  389. * @returns
  390. * Whether `code` is allowed before.
  391. */
  392. export type Previous = (this: TokenizeContext, code: Code) => boolean
  393. /**
  394. * An object describing how to parse a markdown construct.
  395. */
  396. export type Construct = {
  397. /**
  398. * Set up a state machine to handle character codes streaming in.
  399. */
  400. tokenize: Tokenizer
  401. /**
  402. * Guard whether the previous character can come before the construct.
  403. */
  404. previous?: Previous | undefined
  405. /**
  406. * For containers, a continuation construct.
  407. */
  408. continuation?: Construct | undefined
  409. /**
  410. * For containers, a final hook.
  411. */
  412. exit?: Exiter | undefined
  413. /**
  414. * Name of the construct, used to toggle constructs off.
  415. *
  416. * Named constructs must not be `partial`.
  417. */
  418. name?: string | undefined
  419. /**
  420. * Whether this construct represents a partial construct.
  421. *
  422. * Partial constructs must not have a `name`.
  423. */
  424. partial?: boolean | undefined
  425. /**
  426. * Resolve the events parsed by `tokenize`.
  427. *
  428. * For example, if we’re currently parsing a link title and this construct
  429. * parses character references, then `resolve` is called with the events
  430. * ranging from the start to the end of a character reference each time one is
  431. * found.
  432. */
  433. resolve?: Resolver | undefined
  434. /**
  435. * Resolve the events from the start of the content (which includes other
  436. * constructs) to the last one parsed by `tokenize`.
  437. *
  438. * For example, if we’re currently parsing a link title and this construct
  439. * parses character references, then `resolveTo` is called with the events
  440. * ranging from the start of the link title to the end of a character
  441. * reference each time one is found.
  442. */
  443. resolveTo?: Resolver | undefined
  444. /**
  445. * Resolve all events when the content is complete, from the start to the end.
  446. * Only used if `tokenize` is successful once in the content.
  447. *
  448. * For example, if we’re currently parsing a link title and this construct
  449. * parses character references, then `resolveAll` is called *if* at least one
  450. * character reference is found, ranging from the start to the end of the link
  451. * title to the end.
  452. */
  453. resolveAll?: Resolver | undefined
  454. /**
  455. * Concrete constructs cannot be interrupted by more containers.
  456. *
  457. * For example, when parsing the document (containers, such as block quotes
  458. * and lists) and this construct is parsing fenced code:
  459. *
  460. * ````markdown
  461. * > ```js
  462. * > - list?
  463. * ````
  464. *
  465. * …then `- list?` cannot form if this fenced code construct is concrete.
  466. *
  467. * An example of a construct that is not concrete is a GFM table:
  468. *
  469. * ````markdown
  470. * | a |
  471. * | - |
  472. * > | b |
  473. * ````
  474. *
  475. * …`b` is not part of the table.
  476. */
  477. concrete?: boolean | undefined
  478. /**
  479. * Whether the construct, when in a `ConstructRecord`, precedes over existing
  480. * constructs for the same character code when merged.
  481. *
  482. * The default is that new constructs precede over existing ones.
  483. */
  484. add?: 'after' | 'before' | undefined
  485. }
  486. /**
  487. * Like a construct, but `tokenize` does not accept `ok` or `nok`.
  488. */
  489. export type InitialConstruct = Omit<Construct, 'tokenize'> & {
  490. tokenize: Initializer
  491. }
  492. /**
  493. * Several constructs, mapped from their initial codes.
  494. */
  495. export type ConstructRecord = Record<
  496. string,
  497. Array<Construct> | Construct | undefined
  498. >
  499. /**
  500. * State shared between container calls.
  501. */
  502. export interface ContainerState {
  503. /**
  504. * Special field to close the current flow (or containers).
  505. */
  506. _closeFlow?: boolean | undefined
  507. /**
  508. * Used by block quotes.
  509. */
  510. open?: boolean | undefined
  511. /**
  512. * Current marker, used by lists.
  513. */
  514. marker?: Code | undefined
  515. /**
  516. * Current token type, used by lists.
  517. */
  518. type?: TokenType | undefined
  519. /**
  520. * Current size, used by lists.
  521. */
  522. size?: number | undefined
  523. /**
  524. * Whether there first line is blank, used by lists.
  525. */
  526. initialBlankLine?: boolean | undefined
  527. /**
  528. * Whether there are further blank lines, used by lists.
  529. */
  530. furtherBlankLines?: boolean | undefined
  531. }
  532. /**
  533. * A context object that helps w/ tokenizing markdown constructs.
  534. */
  535. export interface TokenizeContext {
  536. /**
  537. * The previous code.
  538. */
  539. previous: Code
  540. /**
  541. * Current code.
  542. */
  543. code: Code
  544. /**
  545. * Whether we’re currently interrupting.
  546. *
  547. * Take for example:
  548. *
  549. * ```markdown
  550. * a
  551. * # b
  552. * ```
  553. *
  554. * At 2:1, we’re “interrupting”.
  555. */
  556. interrupt?: boolean | undefined
  557. /**
  558. * The current construct.
  559. *
  560. * Constructs that are not `partial` are set here.
  561. */
  562. currentConstruct?: Construct | undefined
  563. /**
  564. * share state set when parsing containers.
  565. *
  566. * Containers are parsed in separate phases: their first line (`tokenize`),
  567. * continued lines (`continuation.tokenize`), and finally `exit`.
  568. * This record can be used to store some information between these hooks.
  569. */
  570. containerState?: ContainerState | undefined
  571. /**
  572. * Current list of events.
  573. */
  574. events: Array<Event>
  575. /**
  576. * The relevant parsing context.
  577. */
  578. parser: ParseContext
  579. /**
  580. * Get the chunks that span a token (or location).
  581. *
  582. * @param token
  583. * Start/end in stream.
  584. * @returns
  585. * List of chunks.
  586. */
  587. sliceStream: (token: Pick<Token, 'end' | 'start'>) => Array<Chunk>
  588. /**
  589. * Get the source text that spans a token (or location).
  590. *
  591. * @param token
  592. * Start/end in stream.
  593. * @param expandTabs
  594. * Whether to expand tabs.
  595. * @returns
  596. * Serialized chunks.
  597. */
  598. sliceSerialize: (
  599. token: Pick<Token, 'end' | 'start'>,
  600. expandTabs?: boolean | undefined
  601. ) => string
  602. /**
  603. * Get the current place.
  604. *
  605. * @returns
  606. * Current point.
  607. */
  608. now: () => Point
  609. /**
  610. * Define a skip
  611. *
  612. * As containers (block quotes, lists), “nibble” a prefix from the margins,
  613. * where a line starts after that prefix is defined here.
  614. * When the tokenizers moves after consuming a line ending corresponding to
  615. * the line number in the given point, the tokenizer shifts past the prefix
  616. * based on the column in the shifted point.
  617. *
  618. * @param point
  619. * Skip.
  620. * @returns
  621. * Nothing.
  622. */
  623. defineSkip: (point: Point) => undefined
  624. /**
  625. * Write a slice of chunks.
  626. *
  627. * The eof code (`null`) can be used to signal the end of the stream.
  628. *
  629. * @param slice
  630. * Chunks.
  631. * @returns
  632. * Events.
  633. */
  634. write: (slice: Array<Chunk>) => Array<Event>
  635. /**
  636. * Internal boolean shared with `micromark-extension-gfm-task-list-item` to
  637. * signal whether the tokenizer is tokenizing the first content of a list item
  638. * construct.
  639. */
  640. _gfmTasklistFirstContentOfListItem?: boolean | undefined
  641. // To do: next major: remove `_gfmTableDynamicInterruptHack` (no longer
  642. // needed in micromark-extension-gfm-table@1.0.6).
  643. /**
  644. * Internal boolean shared with `micromark-extension-gfm-table` whose body
  645. * rows are not affected by normal interruption rules.
  646. * “Normal” rules are, for example, that an empty list item can’t interrupt:
  647. *
  648. * ````markdown
  649. * a
  650. * *
  651. * ````
  652. *
  653. * The above is one paragraph.
  654. * These rules don’t apply to table body rows:
  655. *
  656. * ````markdown
  657. * | a |
  658. * | - |
  659. * *
  660. * ````
  661. *
  662. * The above list interrupts the table.
  663. */
  664. _gfmTableDynamicInterruptHack?: boolean
  665. }
  666. /**
  667. * Encodings supported by `TextEncoder`.
  668. *
  669. * Arbitrary encodings can be supported depending on how the engine is built.
  670. * So any string *could* be valid.
  671. * But you probably want `utf-8`.
  672. */
  673. export type Encoding =
  674. // Encodings supported in Node by default or when built with the small-icu option.
  675. // Does not include aliases.
  676. | 'utf-8' // Always supported in Node.
  677. | 'utf-16le' // Always supported in Node.
  678. | 'utf-16be' // Not supported when ICU is disabled.
  679. // Everything else (depends on browser, or full ICU data).
  680. | (string & {})
  681. /**
  682. * Contents of the file.
  683. *
  684. * Can either be text, or a `Uint8Array` like structure.
  685. */
  686. export type Value = Uint8Array | string
  687. /**
  688. * A syntax extension changes how markdown is tokenized.
  689. *
  690. * See: <https://github.com/micromark/micromark#syntaxextension>
  691. */
  692. export interface Extension {
  693. document?: ConstructRecord | undefined
  694. contentInitial?: ConstructRecord | undefined
  695. flowInitial?: ConstructRecord | undefined
  696. flow?: ConstructRecord | undefined
  697. string?: ConstructRecord | undefined
  698. text?: ConstructRecord | undefined
  699. disable?: {null?: Array<string> | undefined} | undefined
  700. insideSpan?:
  701. | {null?: Array<Pick<Construct, 'resolveAll'>> | undefined}
  702. | undefined
  703. attentionMarkers?: {null?: Array<Code> | undefined} | undefined
  704. }
  705. /**
  706. * A filtered, combined, extension.
  707. */
  708. export type NormalizedExtension = {
  709. [Key in keyof Extension]: Exclude<Extension[Key], undefined>
  710. }
  711. /**
  712. * A full, filtereed, normalized, extension.
  713. */
  714. export type FullNormalizedExtension = {
  715. [Key in keyof Extension]-?: Exclude<Extension[Key], undefined>
  716. }
  717. /**
  718. * Create a context.
  719. *
  720. * @param from
  721. * Where to create from.
  722. * @returns
  723. * Context.
  724. */
  725. export type Create = (
  726. from?: Omit<Point, '_bufferIndex' | '_index'> | undefined
  727. ) => TokenizeContext
  728. /**
  729. * Config defining how to parse.
  730. */
  731. export interface ParseOptions {
  732. /**
  733. * Array of syntax extensions (default: `[]`).
  734. */
  735. extensions?: Array<Extension> | null | undefined
  736. }
  737. /**
  738. * A context object that helps w/ parsing markdown.
  739. */
  740. export interface ParseContext {
  741. /**
  742. * All constructs.
  743. */
  744. constructs: FullNormalizedExtension
  745. /**
  746. * Create a content tokenizer.
  747. */
  748. content: Create
  749. /**
  750. * Create a document tokenizer.
  751. */
  752. document: Create
  753. /**
  754. * Create a flow tokenizer.
  755. */
  756. flow: Create
  757. /**
  758. * Create a string tokenizer.
  759. */
  760. string: Create
  761. /**
  762. * Create a text tokenizer.
  763. */
  764. text: Create
  765. /**
  766. * List of defined identifiers.
  767. */
  768. defined: Array<string>
  769. /**
  770. * Map of line numbers to whether they are lazy (as opposed to the line before
  771. * them).
  772. * Take for example:
  773. *
  774. * ```markdown
  775. * > a
  776. * b
  777. * ```
  778. *
  779. * L1 here is not lazy, L2 is.
  780. */
  781. lazy: Record<number, boolean>
  782. }
  783. /**
  784. * HTML compiler context.
  785. */
  786. export type CompileContext = {
  787. /**
  788. * Configuration passed by the user.
  789. */
  790. options: CompileOptions
  791. /**
  792. * Set data into the key-value store.
  793. *
  794. * @param key
  795. * Key.
  796. * @param value
  797. * Value.
  798. * @returns
  799. * Nothing.
  800. */
  801. setData: <Key extends keyof CompileData>(
  802. key: Key,
  803. value?: CompileData[Key]
  804. ) => undefined
  805. /**
  806. * Get data from the key-value store.
  807. *
  808. * @param key
  809. * Key.
  810. * @returns
  811. * Value at `key` in compile data.
  812. */
  813. getData: <Key extends keyof CompileData>(key: Key) => CompileData[Key]
  814. /**
  815. * Output an extra line ending if the previous value wasn’t EOF/EOL.
  816. *
  817. * @returns
  818. * Nothing.
  819. */
  820. lineEndingIfNeeded: () => undefined
  821. /**
  822. * Make a value safe for injection in HTML (except w/ `ignoreEncode`).
  823. *
  824. * @param value
  825. * Raw value.
  826. * @returns
  827. * Safe value.
  828. */
  829. encode: (value: string) => string
  830. /**
  831. * Capture some of the output data.
  832. *
  833. * @returns
  834. * Nothing.
  835. */
  836. buffer: () => undefined
  837. /**
  838. * Stop capturing and access the output data.
  839. *
  840. * @returns
  841. * Captured data.
  842. */
  843. resume: () => string
  844. /**
  845. * Output raw data.
  846. *
  847. * @param value
  848. * Raw value.
  849. * @returns
  850. * Nothing.
  851. */
  852. raw: (value: string) => undefined
  853. /**
  854. * Output (parts of) HTML tags.
  855. *
  856. * @param value
  857. * Raw value.
  858. * @returns
  859. * Nothing.
  860. */
  861. tag: (value: string) => undefined
  862. /**
  863. * Get the string value of a token.
  864. *
  865. * @param token
  866. * Start/end in stream.
  867. * @param expandTabs
  868. * Whether to expand tabs.
  869. * @returns
  870. * Serialized chunks.
  871. */
  872. sliceSerialize: TokenizeContext['sliceSerialize']
  873. }
  874. /**
  875. * Serialize micromark events as HTML.
  876. */
  877. export type Compile = (events: Array<Event>) => string
  878. /**
  879. * Handle one token.
  880. *
  881. * @param token
  882. * Token.
  883. * @returns
  884. * Nothing.
  885. */
  886. export type Handle = (this: CompileContext, token: Token) => undefined
  887. /**
  888. * Handle the whole document.
  889. *
  890. * @returns
  891. * Nothing.
  892. */
  893. export type DocumentHandle = (
  894. this: Omit<CompileContext, 'sliceSerialize'>
  895. ) => undefined
  896. /**
  897. * Token types mapping to handles.
  898. */
  899. export type Handles = {
  900. /**
  901. * Token handle.
  902. */
  903. [Key in TokenType]?: Handle
  904. } & {
  905. /**
  906. * Document handle.
  907. */
  908. null?: DocumentHandle
  909. }
  910. /**
  911. * Normalized extenion.
  912. */
  913. export interface HtmlExtension {
  914. enter?: Handles | undefined
  915. exit?: Handles | undefined
  916. }
  917. /**
  918. * An HTML extension changes how markdown tokens are serialized.
  919. */
  920. export type NormalizedHtmlExtension = {
  921. [Key in keyof HtmlExtension]-?: Exclude<HtmlExtension[Key], undefined>
  922. }
  923. /**
  924. * Definition.
  925. */
  926. export type Definition = {
  927. /**
  928. * Destination.
  929. */
  930. destination?: string | undefined
  931. /**
  932. * Title.
  933. */
  934. title?: string | undefined
  935. }
  936. /**
  937. * State tracked to compile events as HTML.
  938. */
  939. export interface CompileData {
  940. /**
  941. * Whether the last emitted value was a tag.
  942. */
  943. lastWasTag?: boolean | undefined
  944. /**
  945. * Whether the first list item is expected, used by lists.
  946. */
  947. expectFirstItem?: boolean | undefined
  948. /**
  949. * Whether to slurp the next line ending (resets itself on the next line
  950. * ending).
  951. */
  952. slurpOneLineEnding?: boolean | undefined
  953. /**
  954. * Whether to slurp all future line endings (has to be unset manually).
  955. */
  956. slurpAllLineEndings?: boolean | undefined
  957. /**
  958. * Whether we’re in fenced code, used by code (fenced).
  959. */
  960. fencedCodeInside?: boolean | undefined
  961. /**
  962. * Number of fences that were seen, used by code (fenced).
  963. */
  964. fencesCount?: number | undefined
  965. /**
  966. * Whether we’ve seen code data, used by code (fenced, indented).
  967. */
  968. flowCodeSeenData?: boolean | undefined
  969. /**
  970. * Ignore encoding unsafe characters, used for example for URLs which are
  971. * first percent encoded, or by HTML when supporting it.
  972. */
  973. ignoreEncode?: boolean | undefined
  974. /**
  975. * Current heading rank, used by heading (atx, setext).
  976. */
  977. headingRank?: number | undefined
  978. /**
  979. * Whether we’re in code data, used by code (text).
  980. */
  981. inCodeText?: boolean | undefined
  982. /**
  983. * Current character reference kind.
  984. */
  985. characterReferenceType?: string | undefined
  986. /**
  987. * Stack of containers, whether they’re tight or not.
  988. */
  989. tightStack: Array<boolean>
  990. /**
  991. * Collected definitions.
  992. */
  993. definitions: Record<string, Definition>
  994. }
  995. /**
  996. * Type of line ending in markdown.
  997. */
  998. export type LineEnding = '\r' | '\n' | '\r\n'
  999. /**
  1000. * Compile options.
  1001. */
  1002. export interface CompileOptions {
  1003. /**
  1004. * Whether to allow (dangerous) HTML (`boolean`, default: `false`).
  1005. *
  1006. * The default is `false`, which still parses the HTML according to
  1007. * `CommonMark` but shows the HTML as text instead of as elements.
  1008. *
  1009. * Pass `true` for trusted content to get actual HTML elements.
  1010. */
  1011. allowDangerousHtml?: boolean | null | undefined
  1012. /**
  1013. * Whether to allow dangerous protocols in links and images (`boolean`,
  1014. * default: `false`).
  1015. *
  1016. * The default is `false`, which drops URLs in links and images that use
  1017. * dangerous protocols.
  1018. *
  1019. * Pass `true` for trusted content to support all protocols.
  1020. *
  1021. * URLs that have no protocol (which means it’s relative to the current page,
  1022. * such as `./some/page.html`) and URLs that have a safe protocol (for
  1023. * images: `http`, `https`; for links: `http`, `https`, `irc`, `ircs`,
  1024. * `mailto`, `xmpp`), are safe.
  1025. * All other URLs are dangerous and dropped.
  1026. */
  1027. allowDangerousProtocol?: boolean | null | undefined
  1028. /**
  1029. * Default line ending to use when compiling to HTML, for line endings not in
  1030. * `value`.
  1031. *
  1032. * Generally, `micromark` copies line endings (`\r`, `\n`, `\r\n`) in the
  1033. * markdown document over to the compiled HTML.
  1034. * In some cases, such as `> a`, CommonMark requires that extra line endings
  1035. * are added: `<blockquote>\n<p>a</p>\n</blockquote>`.
  1036. *
  1037. * To create that line ending, the document is checked for the first line
  1038. * ending that is used.
  1039. * If there is no line ending, `defaultLineEnding` is used.
  1040. * If that isn’t configured, `\n` is used.
  1041. */
  1042. defaultLineEnding?: LineEnding | null | undefined
  1043. /**
  1044. * Array of HTML extensions (default: `[]`).
  1045. */
  1046. htmlExtensions?: Array<HtmlExtension> | null | undefined
  1047. }
  1048. /**
  1049. * Configuration.
  1050. */
  1051. export type Options = ParseOptions & CompileOptions
  1052. /**
  1053. * Enum of allowed token types.
  1054. */
  1055. export type TokenType = keyof TokenTypeMap
  1056. // Note: when changing the next interface, you likely also have to change
  1057. // `micromark-util-symbol`.
  1058. /**
  1059. * Map of allowed token types.
  1060. */
  1061. export interface TokenTypeMap {
  1062. // Note: these are compiled away.
  1063. attentionSequence: 'attentionSequence' // To do: remove.
  1064. space: 'space' // To do: remove.
  1065. data: 'data'
  1066. whitespace: 'whitespace'
  1067. lineEnding: 'lineEnding'
  1068. lineEndingBlank: 'lineEndingBlank'
  1069. linePrefix: 'linePrefix'
  1070. lineSuffix: 'lineSuffix'
  1071. atxHeading: 'atxHeading'
  1072. atxHeadingSequence: 'atxHeadingSequence'
  1073. atxHeadingText: 'atxHeadingText'
  1074. autolink: 'autolink'
  1075. autolinkEmail: 'autolinkEmail'
  1076. autolinkMarker: 'autolinkMarker'
  1077. autolinkProtocol: 'autolinkProtocol'
  1078. characterEscape: 'characterEscape'
  1079. characterEscapeValue: 'characterEscapeValue'
  1080. characterReference: 'characterReference'
  1081. characterReferenceMarker: 'characterReferenceMarker'
  1082. characterReferenceMarkerNumeric: 'characterReferenceMarkerNumeric'
  1083. characterReferenceMarkerHexadecimal: 'characterReferenceMarkerHexadecimal'
  1084. characterReferenceValue: 'characterReferenceValue'
  1085. codeFenced: 'codeFenced'
  1086. codeFencedFence: 'codeFencedFence'
  1087. codeFencedFenceSequence: 'codeFencedFenceSequence'
  1088. codeFencedFenceInfo: 'codeFencedFenceInfo'
  1089. codeFencedFenceMeta: 'codeFencedFenceMeta'
  1090. codeFlowValue: 'codeFlowValue'
  1091. codeIndented: 'codeIndented'
  1092. codeText: 'codeText'
  1093. codeTextData: 'codeTextData'
  1094. codeTextPadding: 'codeTextPadding'
  1095. codeTextSequence: 'codeTextSequence'
  1096. content: 'content'
  1097. definition: 'definition'
  1098. definitionDestination: 'definitionDestination'
  1099. definitionDestinationLiteral: 'definitionDestinationLiteral'
  1100. definitionDestinationLiteralMarker: 'definitionDestinationLiteralMarker'
  1101. definitionDestinationRaw: 'definitionDestinationRaw'
  1102. definitionDestinationString: 'definitionDestinationString'
  1103. definitionLabel: 'definitionLabel'
  1104. definitionLabelMarker: 'definitionLabelMarker'
  1105. definitionLabelString: 'definitionLabelString'
  1106. definitionMarker: 'definitionMarker'
  1107. definitionTitle: 'definitionTitle'
  1108. definitionTitleMarker: 'definitionTitleMarker'
  1109. definitionTitleString: 'definitionTitleString'
  1110. emphasis: 'emphasis'
  1111. emphasisSequence: 'emphasisSequence'
  1112. emphasisText: 'emphasisText'
  1113. escapeMarker: 'escapeMarker'
  1114. hardBreakEscape: 'hardBreakEscape'
  1115. hardBreakTrailing: 'hardBreakTrailing'
  1116. htmlFlow: 'htmlFlow'
  1117. htmlFlowData: 'htmlFlowData'
  1118. htmlText: 'htmlText'
  1119. htmlTextData: 'htmlTextData'
  1120. image: 'image'
  1121. label: 'label'
  1122. labelText: 'labelText'
  1123. labelLink: 'labelLink'
  1124. labelImage: 'labelImage'
  1125. labelMarker: 'labelMarker'
  1126. labelImageMarker: 'labelImageMarker'
  1127. labelEnd: 'labelEnd'
  1128. link: 'link'
  1129. paragraph: 'paragraph'
  1130. reference: 'reference'
  1131. referenceMarker: 'referenceMarker'
  1132. referenceString: 'referenceString'
  1133. resource: 'resource'
  1134. resourceDestination: 'resourceDestination'
  1135. resourceDestinationLiteral: 'resourceDestinationLiteral'
  1136. resourceDestinationLiteralMarker: 'resourceDestinationLiteralMarker'
  1137. resourceDestinationRaw: 'resourceDestinationRaw'
  1138. resourceDestinationString: 'resourceDestinationString'
  1139. resourceMarker: 'resourceMarker'
  1140. resourceTitle: 'resourceTitle'
  1141. resourceTitleMarker: 'resourceTitleMarker'
  1142. resourceTitleString: 'resourceTitleString'
  1143. setextHeading: 'setextHeading'
  1144. setextHeadingText: 'setextHeadingText'
  1145. setextHeadingLine: 'setextHeadingLine'
  1146. setextHeadingLineSequence: 'setextHeadingLineSequence'
  1147. strong: 'strong'
  1148. strongSequence: 'strongSequence'
  1149. strongText: 'strongText'
  1150. thematicBreak: 'thematicBreak'
  1151. thematicBreakSequence: 'thematicBreakSequence'
  1152. blockQuote: 'blockQuote'
  1153. blockQuotePrefix: 'blockQuotePrefix'
  1154. blockQuoteMarker: 'blockQuoteMarker'
  1155. blockQuotePrefixWhitespace: 'blockQuotePrefixWhitespace'
  1156. listOrdered: 'listOrdered'
  1157. listUnordered: 'listUnordered'
  1158. listItemIndent: 'listItemIndent'
  1159. listItemMarker: 'listItemMarker'
  1160. listItemPrefix: 'listItemPrefix'
  1161. listItemPrefixWhitespace: 'listItemPrefixWhitespace'
  1162. listItemValue: 'listItemValue'
  1163. chunkDocument: 'chunkDocument'
  1164. chunkContent: 'chunkContent'
  1165. chunkFlow: 'chunkFlow'
  1166. chunkText: 'chunkText'
  1167. chunkString: 'chunkString'
  1168. }