版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

148 lines
4.4 KiB

  1. import yaml from 'js-yaml';
  2. type Input = string | Buffer;
  3. interface GrayMatterFile<I extends Input> {
  4. path: string;
  5. data: {
  6. [key: string]: any;
  7. };
  8. content: string;
  9. contents: any;
  10. excerpt?: string;
  11. orig: I;
  12. language: string;
  13. matter: string;
  14. isEmpty?: boolean;
  15. empty?: any;
  16. stringify(lang: string): string;
  17. }
  18. interface GrayMatterOption<I extends Input, O extends GrayMatterOption<I, O>> {
  19. parser?: <A extends any[], R>(...args: A) => R;
  20. eval?: boolean;
  21. excerpt?: boolean | ((input: GrayMatterFile<I>, options: O) => string);
  22. excerpt_separator?: string;
  23. sections?: boolean;
  24. section?: any;
  25. safeLoad?: boolean;
  26. engines?: {
  27. [index: string]: ((input: string) => object) | {
  28. parse: (input: string) => object;
  29. stringify?: (data: object) => string;
  30. };
  31. };
  32. language?: string;
  33. /** @deprecated use `language` instead */
  34. lang?: string;
  35. delimiters?: string | string[];
  36. /** @deprecated use `delimiters` instead */
  37. delims?: string | string[];
  38. }
  39. /**
  40. * Takes a string or object with `content` property, extracts
  41. * and parses front-matter from the string, then returns an object
  42. * with `data`, `content` and other [useful properties](#returned-object).
  43. *
  44. * ```js
  45. * var matter = require('gray-matter');
  46. * console.log(matter('---\ntitle: Home\n---\nOther stuff'));
  47. * //=> { data: { title: 'Home'}, content: 'Other stuff' }
  48. * ```
  49. */
  50. type GrayMatterFn = <I extends Input, O extends GrayMatterOption<I, O>>(input: I | {
  51. content: I;
  52. }, options?: O) => GrayMatterFile<I>;
  53. interface GrayMatterApi {
  54. /**
  55. * Detect the language to use, if on eis defined after the first
  56. * front-matter delimiter.
  57. */
  58. language<I extends Input, O extends GrayMatterOption<I, O>>(str: string, options?: GrayMatterOption<I, O>): {
  59. name: string;
  60. raw: string;
  61. [key: string]: any;
  62. };
  63. /**
  64. * Returns `true` if the given _string_ has front-matter.
  65. */
  66. test<I extends Input, O extends GrayMatterOption<I, O>>(str: string, options?: GrayMatterOption<I, O>): boolean;
  67. /**
  68. * Synchronously read a file from the file system and parse
  69. * front matter. Returns the same object as the [main function](#matter).
  70. *
  71. * ```js
  72. * var file = matter.read('./content/blog-post.md');
  73. * ```
  74. */
  75. read<I extends Input, O extends GrayMatterOption<I, O>>(fp: string, options?: GrayMatterOption<I, O>): GrayMatterFile<string>;
  76. /**
  77. * Stringify an object to YAML or the specified language, and
  78. * append it to the given string. By default, only YAML and JSON
  79. * can be stringified. See the [engines](#engines) section to learn
  80. * how to stringify other languages.
  81. *
  82. * ```js
  83. * console.log(matter.stringify('foo bar baz', {title: 'Home'}));
  84. * // results in:
  85. * // ---
  86. * // title: Home
  87. * // ---
  88. * // foo bar baz
  89. * ```
  90. */
  91. stringify<I extends Input, O extends GrayMatterOption<I, O>>(file: string | GrayMatterFile<I>, data?: object, options?: GrayMatterOption<I, O>): string;
  92. /**
  93. * Parses the gray-matter
  94. */
  95. parseMatter<I extends Input, O extends GrayMatterOption<I, O>>(file: string | GrayMatterFile<I>, options?: GrayMatterOption<I, O>): any;
  96. clearCache(): void;
  97. cache: Record<string, any>;
  98. }
  99. /**
  100. * Default engines
  101. */
  102. declare const engines: {
  103. yaml: {
  104. parse: typeof yaml.load;
  105. stringify: typeof yaml.dump;
  106. };
  107. json: {
  108. parse: (text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any;
  109. stringify: (obj: any, options: any) => string;
  110. };
  111. javascript(str: any, options: any, wrap: any): any;
  112. stringify(): never;
  113. };
  114. declare const utils: {
  115. define: (obj: any, key: any, val: any) => void;
  116. /**
  117. * Returns true if `val` is a buffer
  118. */
  119. isBuffer: (val: any) => boolean;
  120. /**
  121. * Returns true if `val` is an object
  122. */
  123. isObject: (val: any) => boolean;
  124. /**
  125. * Cast `input` to a buffer
  126. */
  127. toBuffer(input: any): any;
  128. /**
  129. * Cast `val` to a string.
  130. */
  131. toString(input: any): any;
  132. /**
  133. * Cast `val` to an array.
  134. */
  135. arrayify(val: any): any[];
  136. /**
  137. * Returns true if `str` starts with `substr`.
  138. */
  139. startsWith(str: string, substr: string, len?: number): boolean;
  140. };
  141. declare const matter: GrayMatterApi & GrayMatterFn;
  142. export { matter as default, engines, matter, utils };