index.d.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Get the plain-text value of a node.
  3. *
  4. * ###### Algorithm
  5. *
  6. * * if `tree` is a comment, returns its `value`
  7. * * if `tree` is a text, applies normal whitespace collapsing to its
  8. * `value`, as defined by the CSS Text spec
  9. * * if `tree` is a root or element, applies an algorithm similar to the
  10. * `innerText` getter as defined by HTML
  11. *
  12. * ###### Notes
  13. *
  14. * > 👉 **Note**: the algorithm acts as if `tree` is being rendered, and as if
  15. * > we’re a CSS-supporting user agent, with scripting enabled.
  16. *
  17. * * if `tree` is an element that is not displayed (such as a `head`), we’ll
  18. * still use the `innerText` algorithm instead of switching to `textContent`
  19. * * if descendants of `tree` are elements that are not displayed, they are
  20. * ignored
  21. * * CSS is not considered, except for the default user agent style sheet
  22. * * a line feed is collapsed instead of ignored in cases where Fullwidth, Wide,
  23. * or Halfwidth East Asian Width characters are used, the same goes for a case
  24. * with Chinese, Japanese, or Yi writing systems
  25. * * replaced elements (such as `audio`) are treated like non-replaced elements
  26. *
  27. * @param {Nodes} tree
  28. * Tree to turn into text.
  29. * @param {Readonly<Options> | null | undefined} [options]
  30. * Configuration (optional).
  31. * @returns {string}
  32. * Serialized `tree`.
  33. */
  34. export function toText(tree: Nodes, options?: Readonly<Options> | null | undefined): string;
  35. export type Comment = import('hast').Comment;
  36. export type Element = import('hast').Element;
  37. export type Nodes = import('hast').Nodes;
  38. export type Parents = import('hast').Parents;
  39. export type Text = import('hast').Text;
  40. export type TestFunction = import('hast-util-is-element').TestFunction;
  41. /**
  42. * Valid and useful whitespace values (from CSS).
  43. */
  44. export type Whitespace = 'normal' | 'nowrap' | 'pre' | 'pre-wrap';
  45. /**
  46. * Specific break:
  47. *
  48. * * `0` — space
  49. * * `1` — line ending
  50. * * `2` — blank line
  51. */
  52. export type BreakNumber = 0 | 1 | 2;
  53. /**
  54. * Forced break.
  55. */
  56. export type BreakForce = '\n';
  57. /**
  58. * Whether there was a break.
  59. */
  60. export type BreakValue = boolean;
  61. /**
  62. * Any value for a break before.
  63. */
  64. export type BreakBefore = BreakNumber | BreakValue | undefined;
  65. /**
  66. * Any value for a break after.
  67. */
  68. export type BreakAfter = BreakForce | BreakNumber | BreakValue | undefined;
  69. /**
  70. * Info on current collection.
  71. */
  72. export type CollectionInfo = {
  73. /**
  74. * Whether there was a break after.
  75. */
  76. breakAfter: BreakAfter;
  77. /**
  78. * Whether there was a break before.
  79. */
  80. breakBefore: BreakBefore;
  81. /**
  82. * Current whitespace setting.
  83. */
  84. whitespace: Whitespace;
  85. };
  86. /**
  87. * Configuration.
  88. */
  89. export type Options = {
  90. /**
  91. * Initial CSS whitespace setting to use (default: `'normal'`).
  92. */
  93. whitespace?: Whitespace | null | undefined;
  94. };