state.d.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Create `state` from an mdast tree.
  3. *
  4. * @param {MdastNodes} tree
  5. * mdast node to transform.
  6. * @param {Options | null | undefined} [options]
  7. * Configuration (optional).
  8. * @returns {State}
  9. * `state` function.
  10. */
  11. export function createState(tree: MdastNodes, options?: Options | null | undefined): State;
  12. /**
  13. * Wrap `nodes` with line endings between each node.
  14. *
  15. * @template {HastRootContent} Type
  16. * Node type.
  17. * @param {Array<Type>} nodes
  18. * List of nodes to wrap.
  19. * @param {boolean | undefined} [loose=false]
  20. * Whether to add line endings at start and end (default: `false`).
  21. * @returns {Array<HastText | Type>}
  22. * Wrapped nodes.
  23. */
  24. export function wrap<Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | undefined): Array<HastText | Type>;
  25. /**
  26. * Handle a node.
  27. */
  28. export type Handler = (state: State, node: any, parent: MdastParents | undefined) => Array<HastElementContent> | HastElementContent | undefined;
  29. /**
  30. * Handle nodes.
  31. */
  32. export type Handlers = Partial<Record<MdastNodes["type"], Handler>>;
  33. /**
  34. * Configuration (optional).
  35. */
  36. export type Options = {
  37. /**
  38. * Whether to persist raw HTML in markdown in the hast tree (default:
  39. * `false`).
  40. */
  41. allowDangerousHtml?: boolean | null | undefined;
  42. /**
  43. * Prefix to use before the `id` property on footnotes to prevent them from
  44. * *clobbering* (default: `'user-content-'`).
  45. *
  46. * Pass `''` for trusted markdown and when you are careful with
  47. * polyfilling.
  48. * You could pass a different prefix.
  49. *
  50. * DOM clobbering is this:
  51. *
  52. * ```html
  53. * <p id="x"></p>
  54. * <script>alert(x) // `x` now refers to the `p#x` DOM element</script>
  55. * ```
  56. *
  57. * The above example shows that elements are made available by browsers, by
  58. * their ID, on the `window` object.
  59. * This is a security risk because you might be expecting some other variable
  60. * at that place.
  61. * It can also break polyfills.
  62. * Using a prefix solves these problems.
  63. */
  64. clobberPrefix?: string | null | undefined;
  65. /**
  66. * Corresponding virtual file representing the input document (optional).
  67. */
  68. file?: VFile | null | undefined;
  69. /**
  70. * Content of the backreference back to references (default: `defaultFootnoteBackContent`).
  71. *
  72. * The default value is:
  73. *
  74. * ```js
  75. * function defaultFootnoteBackContent(_, rereferenceIndex) {
  76. * const result = [{type: 'text', value: '↩'}]
  77. *
  78. * if (rereferenceIndex > 1) {
  79. * result.push({
  80. * type: 'element',
  81. * tagName: 'sup',
  82. * properties: {},
  83. * children: [{type: 'text', value: String(rereferenceIndex)}]
  84. * })
  85. * }
  86. *
  87. * return result
  88. * }
  89. * ```
  90. *
  91. * This content is used in the `a` element of each backreference (the `↩`
  92. * links).
  93. */
  94. footnoteBackContent?: FootnoteBackContentTemplate | string | null | undefined;
  95. /**
  96. * Label to describe the backreference back to references (default:
  97. * `defaultFootnoteBackLabel`).
  98. *
  99. * The default value is:
  100. *
  101. * ```js
  102. * function defaultFootnoteBackLabel(referenceIndex, rereferenceIndex) {
  103. * return (
  104. * 'Back to reference ' +
  105. * (referenceIndex + 1) +
  106. * (rereferenceIndex > 1 ? '-' + rereferenceIndex : '')
  107. * )
  108. * }
  109. * ```
  110. *
  111. * Change it when the markdown is not in English.
  112. *
  113. * This label is used in the `ariaLabel` property on each backreference
  114. * (the `↩` links).
  115. * It affects users of assistive technology.
  116. */
  117. footnoteBackLabel?: FootnoteBackLabelTemplate | string | null | undefined;
  118. /**
  119. * Textual label to use for the footnotes section (default: `'Footnotes'`).
  120. *
  121. * Change it when the markdown is not in English.
  122. *
  123. * This label is typically hidden visually (assuming a `sr-only` CSS class
  124. * is defined that does that) and so affects screen readers only.
  125. * If you do have such a class, but want to show this section to everyone,
  126. * pass different properties with the `footnoteLabelProperties` option.
  127. */
  128. footnoteLabel?: string | null | undefined;
  129. /**
  130. * Properties to use on the footnote label (default: `{className:
  131. * ['sr-only']}`).
  132. *
  133. * Change it to show the label and add other properties.
  134. *
  135. * This label is typically hidden visually (assuming an `sr-only` CSS class
  136. * is defined that does that) and so affects screen readers only.
  137. * If you do have such a class, but want to show this section to everyone,
  138. * pass an empty string.
  139. * You can also add different properties.
  140. *
  141. * > **Note**: `id: 'footnote-label'` is always added, because footnote
  142. * > calls use it with `aria-describedby` to provide an accessible label.
  143. */
  144. footnoteLabelProperties?: HastProperties | null | undefined;
  145. /**
  146. * HTML tag name to use for the footnote label element (default: `'h2'`).
  147. *
  148. * Change it to match your document structure.
  149. *
  150. * This label is typically hidden visually (assuming a `sr-only` CSS class
  151. * is defined that does that) and so affects screen readers only.
  152. * If you do have such a class, but want to show this section to everyone,
  153. * pass different properties with the `footnoteLabelProperties` option.
  154. */
  155. footnoteLabelTagName?: string | null | undefined;
  156. /**
  157. * Extra handlers for nodes (optional).
  158. */
  159. handlers?: Handlers | null | undefined;
  160. /**
  161. * List of custom mdast node types to pass through (keep) in hast (note that
  162. * the node itself is passed, but eventual children are transformed)
  163. * (optional).
  164. */
  165. passThrough?: Array<MdastNodes["type"]> | null | undefined;
  166. /**
  167. * Handler for all unknown nodes (optional).
  168. */
  169. unknownHandler?: Handler | null | undefined;
  170. };
  171. /**
  172. * Info passed around.
  173. */
  174. export type State = {
  175. /**
  176. * Transform the children of an mdast parent to hast.
  177. */
  178. all: (node: MdastNodes) => Array<HastElementContent>;
  179. /**
  180. * Honor the `data` of `from`, and generate an element instead of `node`.
  181. */
  182. applyData: <Type extends HastNodes>(from: MdastNodes, to: Type) => HastElement | Type;
  183. /**
  184. * Definitions by their identifier.
  185. */
  186. definitionById: Map<string, MdastDefinition>;
  187. /**
  188. * Footnote definitions by their identifier.
  189. */
  190. footnoteById: Map<string, MdastFootnoteDefinition>;
  191. /**
  192. * Counts for how often the same footnote was called.
  193. */
  194. footnoteCounts: Map<string, number>;
  195. /**
  196. * Identifiers of order when footnote calls first appear in tree order.
  197. */
  198. footnoteOrder: Array<string>;
  199. /**
  200. * Applied handlers.
  201. */
  202. handlers: Handlers;
  203. /**
  204. * Transform an mdast node to hast.
  205. */
  206. one: (node: MdastNodes, parent: MdastParents | undefined) => Array<HastElementContent> | HastElementContent | undefined;
  207. /**
  208. * Configuration.
  209. */
  210. options: Options;
  211. /**
  212. * Copy a node’s positional info.
  213. */
  214. patch: (from: MdastNodes, node: HastNodes) => undefined;
  215. /**
  216. * Wrap `nodes` with line endings between each node, adds initial/final line endings when `loose`.
  217. */
  218. wrap: <Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | undefined) => Array<HastText | Type>;
  219. };
  220. import type { Nodes as MdastNodes } from 'mdast';
  221. import type { RootContent as HastRootContent } from 'hast';
  222. import type { Text as HastText } from 'hast';
  223. import type { Parents as MdastParents } from 'mdast';
  224. import type { ElementContent as HastElementContent } from 'hast';
  225. import type { VFile } from 'vfile';
  226. import type { FootnoteBackContentTemplate } from './footer.js';
  227. import type { FootnoteBackLabelTemplate } from './footer.js';
  228. import type { Properties as HastProperties } from 'hast';
  229. import type { Nodes as HastNodes } from 'hast';
  230. import type { Element as HastElement } from 'hast';
  231. import type { Definition as MdastDefinition } from 'mdast';
  232. import type { FootnoteDefinition as MdastFootnoteDefinition } from 'mdast';
  233. //# sourceMappingURL=state.d.ts.map