index.d.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import type {Point, Position} from 'unist'
  2. // To do: next major: remove `void` from allowed return types.
  3. /**
  4. * @typeParam Context
  5. * Value used as `this`.
  6. * @this
  7. * The `warningContext` given to `parseEntities`
  8. * @param reason
  9. * Human readable reason for emitting a parse error.
  10. * @param point
  11. * Place where the error occurred.
  12. * @param code
  13. * Machine readable code the error.
  14. */
  15. export type WarningHandler<Context = undefined> = (
  16. this: Context,
  17. reason: string,
  18. point: Point,
  19. code: number
  20. ) => undefined | void
  21. /**
  22. * @typeParam Context
  23. * Value used as `this`.
  24. * @this
  25. * The `referenceContext` given to `parseEntities`
  26. * @param value
  27. * Decoded character reference.
  28. * @param position
  29. * Place where `value` starts and ends.
  30. * @param source
  31. * Raw source of character reference.
  32. */
  33. export type ReferenceHandler<Context = undefined> = (
  34. this: Context,
  35. value: string,
  36. position: Position,
  37. source: string
  38. ) => undefined | void
  39. /**
  40. * @typeParam Context
  41. * Value used as `this`.
  42. * @this
  43. * The `textContext` given to `parseEntities`.
  44. * @param value
  45. * String of content.
  46. * @param position
  47. * Place where `value` starts and ends.
  48. */
  49. export type TextHandler<Context = undefined> = (
  50. this: Context,
  51. value: string,
  52. position: Position
  53. ) => undefined | void
  54. /**
  55. * Configuration.
  56. *
  57. * @typeParam WarningContext
  58. * Value used as `this` in the `warning` handler.
  59. * @typeParam ReferenceContext
  60. * Value used as `this` in the `reference` handler.
  61. * @typeParam TextContext
  62. * Value used as `this` in the `text` handler.
  63. */
  64. export interface Options<
  65. WarningContext = undefined,
  66. ReferenceContext = undefined,
  67. TextContext = undefined
  68. > {
  69. /**
  70. * Additional character to accept.
  71. * This allows other characters, without error, when following an ampersand.
  72. *
  73. * @default ''
  74. */
  75. additional?: string | null | undefined
  76. /**
  77. * Whether to parse `value` as an attribute value.
  78. * This results in slightly different behavior.
  79. *
  80. * @default false
  81. */
  82. attribute?: boolean | null | undefined
  83. /**
  84. * Whether to allow nonterminated character references.
  85. * For example, `&copycat` for `©cat`.
  86. * This behavior is compliant to the spec but can lead to unexpected results.
  87. *
  88. * @default true
  89. */
  90. nonTerminated?: boolean | null | undefined
  91. /**
  92. * Starting `position` of `value` (`Point` or `Position`). Useful when dealing with values nested in some sort of syntax tree.
  93. */
  94. position?: Readonly<Position> | Readonly<Point> | null | undefined
  95. /**
  96. * Context used when calling `warning`.
  97. */
  98. warningContext?: WarningContext | null | undefined
  99. /**
  100. * Context used when calling `reference`.
  101. */
  102. referenceContext?: ReferenceContext | null | undefined
  103. /**
  104. * Context used when calling `text`.
  105. */
  106. textContext?: TextContext | null | undefined
  107. /**
  108. * Warning handler.
  109. */
  110. warning?: WarningHandler<WarningContext> | null | undefined
  111. /**
  112. * Reference handler.
  113. */
  114. reference?: ReferenceHandler<ReferenceContext> | null | undefined
  115. /**
  116. * Text handler.
  117. */
  118. text?: TextHandler<TextContext> | null | undefined
  119. }
  120. export {parseEntities} from './lib/index.js'