Open fireflysemantics opened 4 years ago
import * as matter from 'gray-matter';
only works if you then call it like
matter.default('---\ntitle: Front Matter\n---\nThis is content.');
Or import like you suggested import matter from 'gray-matter'
When
matter.default('---\ntitle: Front Matter\n---\nThis is content.');
it says
Property 'default' does not exist on type 'typeof matter'.ts(2339)
For being abandoned the package works well enough. I just had this issue when importing it as there are no types declarations for it.
I created gray-matter.d.ts
in the root of my React project with the following content.
Please take into account that I created it based on the documentation and my use case (React + TS), this might not suffice in all cases.
declare module "gray-matter" {
namespace grayMatter {
interface GrayMatterFile<T = any> {
content: string;
data: {
[key: string]: any;
};
excerpt: string;
isEmpty: boolean;
orig: Uint8Array;
}
}
function grayMatter<T = any>(
str: string,
options?: {
excerpt?: boolean | function;
excerpt_separator?: string;
engines?: any;
language?: string;
delimiters?: string | string[];
}
): grayMatter.GrayMatterFile<T>;
export = grayMatter;
}
Then I just import matter from "gray-matter";
and all the declared options and types should be available.
I don't think this line in the README makes sense ...
At least VS Code red lines it with the error:
This works:
import * as matter from 'gray-matter';
Actually nix that. VSCode does not complain but it does not work. This works:
Here's a stackblitz
https://stackblitz.com/edit/gray-matter
Outside of Stackblitz we also need to set this compiler options in
tsconfig.lib
:Last thing - for Angular 9 this needs to be added to polyfills.js:
Related SO: https://stackoverflow.com/questions/60772266/getting-buffer-is-not-defined-when-using-gray-matter-in-angular/60772329#60772329