Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  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.  
  21. declare namespace matter {
  22. type Input = string | Buffer
  23. interface GrayMatterOption<
  24. I extends Input,
  25. O extends GrayMatterOption<I, O>
  26. > {
  27. parser?: () => void
  28. eval?: boolean
  29. excerpt?: boolean | ((input: I, options: O) => string)
  30. excerpt_separator?: string
  31. engines?: {
  32. [index: string]:
  33. | ((input: string) => object)
  34. | { parse: (input: string) => object; stringify?: (data: object) => string }
  35. }
  36. language?: string
  37. delimiters?: string | [string, string]
  38. }
  39. interface GrayMatterFile<I extends Input> {
  40. data: { [key: string]: any }
  41. content: string
  42. excerpt?: string
  43. orig: Buffer | I
  44. language: string
  45. matter: string
  46. stringify(lang: string): string
  47. }
  48.  
  49. /**
  50. * Stringify an object to YAML or the specified language, and
  51. * append it to the given string. By default, only YAML and JSON
  52. * can be stringified. See the [engines](#engines) section to learn
  53. * how to stringify other languages.
  54. *
  55. * ```js
  56. * console.log(matter.stringify('foo bar baz', {title: 'Home'}));
  57. * // results in:
  58. * // ---
  59. * // title: Home
  60. * // ---
  61. * // foo bar baz
  62. * ```
  63. * @param {String|Object} `file` The content string to append to stringified front-matter, or a file object with `file.content` string.
  64. * @param {Object} `data` Front matter to stringify.
  65. * @param {Object} `options` [Options](#options) to pass to gray-matter and [js-yaml].
  66. * @return {String} Returns a string created by wrapping stringified yaml with delimiters, and appending that to the given string.
  67. */
  68. export function stringify<O extends GrayMatterOption<string, O>>(
  69. file: string | { content: string },
  70. data: object,
  71. options?: GrayMatterOption<string, O>
  72. ): string
  73.  
  74. /**
  75. * Synchronously read a file from the file system and parse
  76. * front matter. Returns the same object as the [main function](#matter).
  77. *
  78. * ```js
  79. * var file = matter.read('./content/blog-post.md');
  80. * ```
  81. * @param {String} `filepath` file path of the file to read.
  82. * @param {Object} `options` [Options](#options) to pass to gray-matter.
  83. * @return {Object} Returns [an object](#returned-object) with `data` and `content`
  84. */
  85. export function read<O extends GrayMatterOption<string, O>>(
  86. fp: string,
  87. options?: GrayMatterOption<string, O>
  88. ): matter.GrayMatterFile<string>
  89.  
  90. /**
  91. * Returns true if the given `string` has front matter.
  92. * @param {String} `string`
  93. * @param {Object} `options`
  94. * @return {Boolean} True if front matter exists.
  95. */
  96. export function test<O extends matter.GrayMatterOption<string, O>>(
  97. str: string,
  98. options?: GrayMatterOption<string, O>
  99. ): boolean
  100.  
  101. /**
  102. * Detect the language to use, if one is defined after the
  103. * first front-matter delimiter.
  104. * @param {String} `string`
  105. * @param {Object} `options`
  106. * @return {Object} Object with `raw` (actual language string), and `name`, the language with whitespace trimmed
  107. */
  108. export function language<O extends matter.GrayMatterOption<string, O>>(
  109. str: string,
  110. options?: GrayMatterOption<string, O>
  111. ): { name: string; raw: string }
  112. }
  113.  
  114. export = matter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement