Skip to content

Commit

Permalink
feat: add use remark sync hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMurphy committed Jan 21, 2021
1 parent 642866f commit 0e7ca8b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,33 @@ import { Options as RemarkRehypeOptions } from 'mdast-util-to-hast';
import remarkToRehype from 'remark-rehype';
import rehypeReact, { Options as RehypeReactOptions } from 'rehype-react';

export interface UseRemarkOptions {
export interface UseRemarkSyncOptions {
remarkParseOptions?: Partial<RemarkParseOptions>;
remarkToRehypeOptions?: RemarkRehypeOptions;
rehypeReactOptions?: Partial<RehypeReactOptions<typeof createElement>>;
remarkPlugins?: PluggableList;
rehypePlugins?: PluggableList;
}

export const useRemarkSync = (
source: string,
{
remarkParseOptions,
remarkToRehypeOptions,
rehypeReactOptions,
remarkPlugins = [],
rehypePlugins = [],
}: UseRemarkOptions = {}
): ReactElement =>
unified()
.use(remarkParse, remarkParseOptions)
.use(remarkPlugins)
.use(remarkToRehype, remarkToRehypeOptions)
.use(rehypePlugins)
.use(rehypeReact, { createElement, Fragment, ...rehypeReactOptions })
.processSync(source).result as ReactElement;

export interface UseRemarkOptions extends UseRemarkSyncOptions {
onError?: (err: Error) => void;
}

Expand Down

0 comments on commit 0e7ca8b

Please sign in to comment.