index.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. export function fromMarkdown(
  2. value: Value,
  3. encoding?: Encoding | null | undefined,
  4. options?: Options | null | undefined
  5. ): Root
  6. export function fromMarkdown(
  7. value: Value,
  8. options?: Options | null | undefined
  9. ): Root
  10. export type Break = import('mdast').Break
  11. export type Blockquote = import('mdast').Blockquote
  12. export type Code = import('mdast').Code
  13. export type Definition = import('mdast').Definition
  14. export type Emphasis = import('mdast').Emphasis
  15. export type Heading = import('mdast').Heading
  16. export type Html = import('mdast').Html
  17. export type Image = import('mdast').Image
  18. export type InlineCode = import('mdast').InlineCode
  19. export type Link = import('mdast').Link
  20. export type List = import('mdast').List
  21. export type ListItem = import('mdast').ListItem
  22. export type Nodes = import('mdast').Nodes
  23. export type Paragraph = import('mdast').Paragraph
  24. export type Parent = import('mdast').Parent
  25. export type PhrasingContent = import('mdast').PhrasingContent
  26. export type ReferenceType = import('mdast').ReferenceType
  27. export type Root = import('mdast').Root
  28. export type Strong = import('mdast').Strong
  29. export type Text = import('mdast').Text
  30. export type ThematicBreak = import('mdast').ThematicBreak
  31. export type Encoding = import('micromark-util-types').Encoding
  32. export type Event = import('micromark-util-types').Event
  33. export type ParseOptions = import('micromark-util-types').ParseOptions
  34. export type Token = import('micromark-util-types').Token
  35. export type TokenizeContext = import('micromark-util-types').TokenizeContext
  36. export type Value = import('micromark-util-types').Value
  37. export type Point = import('unist').Point
  38. export type CompileData = import('../index.js').CompileData
  39. export type Fragment = Omit<Parent, 'children' | 'type'> & {
  40. type: 'fragment'
  41. children: Array<PhrasingContent>
  42. }
  43. /**
  44. * Extra transform, to change the AST afterwards.
  45. */
  46. export type Transform = (tree: Root) => Root | null | undefined | void
  47. /**
  48. * Handle a token.
  49. */
  50. export type Handle = (this: CompileContext, token: Token) => undefined | void
  51. /**
  52. * Token types mapping to handles
  53. */
  54. export type Handles = Record<string, Handle>
  55. /**
  56. * Handle the case where the `right` token is open, but it is closed (by the
  57. * `left` token) or because we reached the end of the document.
  58. */
  59. export type OnEnterError = (
  60. this: Omit<CompileContext, 'sliceSerialize'>,
  61. left: Token | undefined,
  62. right: Token
  63. ) => undefined
  64. /**
  65. * Handle the case where the `right` token is open but it is closed by
  66. * exiting the `left` token.
  67. */
  68. export type OnExitError = (
  69. this: Omit<CompileContext, 'sliceSerialize'>,
  70. left: Token,
  71. right: Token
  72. ) => undefined
  73. /**
  74. * Open token on the stack, with an optional error handler for when
  75. * that token isn’t closed properly.
  76. */
  77. export type TokenTuple = [Token, OnEnterError | undefined]
  78. /**
  79. * Configuration.
  80. *
  81. * We have our defaults, but extensions will add more.
  82. */
  83. export type Config = {
  84. /**
  85. * Token types where line endings are used.
  86. */
  87. canContainEols: Array<string>
  88. /**
  89. * Opening handles.
  90. */
  91. enter: Handles
  92. /**
  93. * Closing handles.
  94. */
  95. exit: Handles
  96. /**
  97. * Tree transforms.
  98. */
  99. transforms: Array<Transform>
  100. }
  101. /**
  102. * Change how markdown tokens from micromark are turned into mdast.
  103. */
  104. export type Extension = Partial<Config>
  105. /**
  106. * mdast compiler context.
  107. */
  108. export type CompileContext = {
  109. /**
  110. * Stack of nodes.
  111. */
  112. stack: Array<Fragment | Nodes>
  113. /**
  114. * Stack of tokens.
  115. */
  116. tokenStack: Array<TokenTuple>
  117. /**
  118. * Capture some of the output data.
  119. */
  120. buffer: (this: CompileContext) => undefined
  121. /**
  122. * Stop capturing and access the output data.
  123. */
  124. resume: (this: CompileContext) => string
  125. /**
  126. * Enter a node.
  127. */
  128. enter: (
  129. this: CompileContext,
  130. node: Nodes,
  131. token: Token,
  132. onError?: OnEnterError
  133. ) => undefined
  134. /**
  135. * Exit a node.
  136. */
  137. exit: (this: CompileContext, token: Token, onError?: OnExitError) => undefined
  138. /**
  139. * Get the string value of a token.
  140. */
  141. sliceSerialize: TokenizeContext['sliceSerialize']
  142. /**
  143. * Configuration.
  144. */
  145. config: Config
  146. /**
  147. * Info passed around; key/value store.
  148. */
  149. data: CompileData
  150. }
  151. /**
  152. * Configuration for how to build mdast.
  153. */
  154. export type FromMarkdownOptions = {
  155. /**
  156. * Extensions for this utility to change how tokens are turned into a tree.
  157. */
  158. mdastExtensions?: Array<Extension | Array<Extension>> | null | undefined
  159. }
  160. /**
  161. * Configuration.
  162. */
  163. export type Options = ParseOptions & FromMarkdownOptions