Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition file #75

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Type definitions for next-mdx-remote
// Project: https://github.com/hashicorp/next-mdx-remote
// Definitions by: Steven Schmatz <https://github.com/stevenschmatz>

declare module 'next-mdx-remote/render-to-string' {
import * as React from 'react';

/**
* Runs the MDX renderer on the MDX string provided with the components and data provided.
*/
export default function renderToString(
/** Raw MDX contents as a string. */
source: string,
/** Optional parameters, such as components, plugins, and data. */
params?: {
/**
* A object mapping names to React components.
* The key used will be the name accessible to MDX.
*
* For example: `{ ComponentName: Component }` will be accessible in the MDX as `<ComponentName/>`.
*/
components?: {
[componentName: string]: React.FunctionComponent | React.Component;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was working on type definitions, I also found that I needed to specify this type in this way instead of just React.Element, but was confused about why. Do you have any further insight on this one?

};
/**
* An arbitrary object of data which will be supplied to the MDX.
*
* For example, in cases where you want to provide template variables to the MDX, like `my name is {name}`,
* you could provide scope as `{ name: "Some name" }`.
*/
scope?: object;
/**
* These options are passed to the MDX compiler.
* See [the MDX docs.](https://github.com/mdx-js/mdx/blob/master/packages/mdx/index.js).
*/
mdxOptions?: {
remarkPlugins?: any[];
rehypePlugins?: any[];
hastPlugins?: any[];
compilers?: any[];
filepath?: string;
};
}
): Promise<{
compiledSource: string;
renderedOutput: string;
scope?: Record<string, unknown>;
}>;
}

declare module 'next-mdx-remote/hydrate' {
import * as React from 'react';

/**
* Consumes the output of `renderToString` as well as the same components argument as `renderToString`.
* Its result can be rendered directly into your component.
*
* This function is intended to run on the **client-side**.
* It will initially render static content, and hydrate it when the browser isn't busy with higher priority tasks.
*
* For more information and examples, consult [the documentation](https://github.com/hashicorp/next-mdx-remote#apis).
*/
export default function hydrate(
/** Rendered MDX output. The direct output of `renderToString`. */
source: {
compiledSource: string;
renderedOutput: string;
scope?: Record<string, unknown>;
},
/**
* A map of names to React components.
* The key used will be the name accessible to MDX.
*
* For example: `{ ComponentName: Component }` will be accessible in the MDX as `<ComponentName/>`.
*/
params?: {
[componentName: string]: React.FunctionComponent | React.Component;
}
): React.ReactNode;
}