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 Jul 8, 2021
1 parent 2c8b1cd commit 761d303
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import rehypeReact, { Options as RehypeReactOptions } from 'rehype-react';

type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export interface UseRemarkOptions {
export interface UseRemarkSyncOptions {
remarkParseOptions?: Partial<RemarkParseOptions>;
remarkToRehypeOptions?: RemarkRehypeOptions;
rehypeReactOptions?: PartialBy<
Expand All @@ -24,6 +24,31 @@ export interface UseRemarkOptions {
>;
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,
} as RehypeReactOptions<typeof createElement>)
.processSync(source).result as ReactElement;

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

Expand Down

0 comments on commit 761d303

Please sign in to comment.