gray-matter.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Takes a string or object with `content` property, extracts
  3. * and parses front-matter from the string, then returns an object
  4. * with `data`, `content` and other [useful properties](#returned-object).
  5. *
  6. * ```js
  7. * var matter = require('gray-matter');
  8. * console.log(matter('---\ntitle: Home\n---\nOther stuff'));
  9. * //=> { data: { title: 'Home'}, content: 'Other stuff' }
  10. * ```
  11. * @param {Object|String} `input` String, or object with `content` string
  12. * @param {Object} `options`
  13. * @return {Object}
  14. * @api public
  15. */
  16. declare function matter<
  17. I extends matter.Input,
  18. O extends matter.GrayMatterOption<I, O>
  19. >(input: I | { content: I }, options?: O): matter.GrayMatterFile<I>
  20. declare namespace matter {
  21. type Input = string | Buffer
  22. interface GrayMatterOption<
  23. I extends Input,
  24. O extends GrayMatterOption<I, O>
  25. > {
  26. parser?: () => void
  27. eval?: boolean
  28. excerpt?: boolean | ((input: I, options: O) => string)
  29. excerpt_separator?: string
  30. engines?: {
  31. [index: string]:
  32. | ((input: string) => object)
  33. | { parse: (input: string) => object; stringify?: (data: object) => string }
  34. }
  35. language?: string
  36. delimiters?: string | [string, string]
  37. }
  38. interface GrayMatterFile<I extends Input> {
  39. data: { [key: string]: any }
  40. content: string
  41. excerpt?: string
  42. orig: Buffer | I
  43. language: string
  44. matter: string
  45. stringify(lang: string): string
  46. }
  47. /**
  48. * Stringify an object to YAML or the specified language, and
  49. * append it to the given string. By default, only YAML and JSON
  50. * can be stringified. See the [engines](#engines) section to learn
  51. * how to stringify other languages.
  52. *
  53. * ```js
  54. * console.log(matter.stringify('foo bar baz', {title: 'Home'}));
  55. * // results in:
  56. * // ---
  57. * // title: Home
  58. * // ---
  59. * // foo bar baz
  60. * ```
  61. * @param {String|Object} `file` The content string to append to stringified front-matter, or a file object with `file.content` string.
  62. * @param {Object} `data` Front matter to stringify.
  63. * @param {Object} `options` [Options](#options) to pass to gray-matter and [js-yaml].
  64. * @return {String} Returns a string created by wrapping stringified yaml with delimiters, and appending that to the given string.
  65. */
  66. export function stringify<O extends GrayMatterOption<string, O>>(
  67. file: string | { content: string },
  68. data: object,
  69. options?: GrayMatterOption<string, O>
  70. ): string
  71. /**
  72. * Synchronously read a file from the file system and parse
  73. * front matter. Returns the same object as the [main function](#matter).
  74. *
  75. * ```js
  76. * var file = matter.read('./content/blog-post.md');
  77. * ```
  78. * @param {String} `filepath` file path of the file to read.
  79. * @param {Object} `options` [Options](#options) to pass to gray-matter.
  80. * @return {Object} Returns [an object](#returned-object) with `data` and `content`
  81. */
  82. export function read<O extends GrayMatterOption<string, O>>(
  83. fp: string,
  84. options?: GrayMatterOption<string, O>
  85. ): matter.GrayMatterFile<string>
  86. /**
  87. * Returns true if the given `string` has front matter.
  88. * @param {String} `string`
  89. * @param {Object} `options`
  90. * @return {Boolean} True if front matter exists.
  91. */
  92. export function test<O extends matter.GrayMatterOption<string, O>>(
  93. str: string,
  94. options?: GrayMatterOption<string, O>
  95. ): boolean
  96. /**
  97. * Detect the language to use, if one is defined after the
  98. * first front-matter delimiter.
  99. * @param {String} `string`
  100. * @param {Object} `options`
  101. * @return {Object} Object with `raw` (actual language string), and `name`, the language with whitespace trimmed
  102. */
  103. export function language<O extends matter.GrayMatterOption<string, O>>(
  104. str: string,
  105. options?: GrayMatterOption<string, O>
  106. ): { name: string; raw: string }
  107. }
  108. export = matter