'use client'; import { createContext, type FC, type HTMLAttributes, type PropsWithChildren, type ReactNode, useContext, } from 'react'; import type { HTMLComponents } from '../html/HTMLComponentTypes'; import { compileMarkdown, type MarkdownRendererOptions, type ParsedMarkdown, } from './processor'; export type MarkdownProviderOptions = { /** Forces the compiler to always output content with a block-level wrapper. */ forceBlock?: boolean; /** Forces the compiler to always output content with an inline wrapper. */ forceInline?: boolean; /** Whether to preserve frontmatter in the markdown content. */ preserveFrontmatter?: boolean; /** Whether to use the GitHub Tag Filter for security. */ tagfilter?: boolean; }; export type MarkdownContextValue = { components?: HTMLComponents<'permissive', {}>; renderMarkdown: ( markdown: string | ParsedMarkdown, options?: MarkdownProviderOptions, components?: HTMLComponents<'permissive', {}>, wrapper?: FC> ) => ReactNode | Promise; }; export type MarkdownProviderProps = PropsWithChildren< MarkdownProviderOptions & { components?: HTMLComponents<'permissive', {}>; wrapper?: FC>; renderMarkdown?: ( markdown: string | ParsedMarkdown, options?: MarkdownProviderOptions, components?: HTMLComponents<'permissive', {}>, wrapper?: FC> ) => ReactNode | Promise; } >; const MarkdownContext = createContext( undefined ); /** * Returns the raw `MarkdownProvider` context value — the active * `renderMarkdown` function plus any provider-level component overrides. * Returns `undefined` when called outside a ``. * Prefer `useMarkdownRenderer` for most use cases; use this hook only when * you need direct access to the context object itself. */ export const useMarkdownContext = () => useContext(MarkdownContext); const mergeOptions = ( baseOptions: MarkdownRendererOptions, options: MarkdownProviderOptions = {}, components: HTMLComponents<'permissive', {}> = {}, wrapper?: FC> ): MarkdownRendererOptions => { return { ...baseOptions, ...options, forceBlock: options.forceBlock ?? baseOptions.forceBlock, forceInline: options.forceInline ?? baseOptions.forceInline, preserveFrontmatter: options.preserveFrontmatter ?? baseOptions.preserveFrontmatter, tagfilter: options.tagfilter ?? baseOptions.tagfilter, wrapper: wrapper || baseOptions.wrapper, forceWrapper: !!(wrapper || baseOptions.wrapper), components: { ...baseOptions.components, ...components }, }; }; /** * Provider for the MarkdownRenderer component. * * It will provide the `renderMarkdown` function to the context, which can be used to render markdown. * * ```tsx * const content = useIntlayer('app'); * * return ( *
* {content.markdown} // Will be rendered with the components and options provided to the MarkdownProvider *
* ); * ``` * * @example * ```tsx * * * {markdownContent} * * * ``` */ export const MarkdownProvider: FC = ({ children, components, wrapper, forceBlock, forceInline, preserveFrontmatter, tagfilter, renderMarkdown: customRenderFn, }) => { const baseOptions: MarkdownRendererOptions = { components, forceBlock, forceInline, wrapper, forceWrapper: !!wrapper, preserveFrontmatter, tagfilter, }; // Standard internal renderer const defaultRenderMarkdown = ( markdown: string | ParsedMarkdown, options?: MarkdownProviderOptions, components?: HTMLComponents<'permissive', {}>, wrapper?: FC> ) => { const mergedOptions = mergeOptions( baseOptions, options, components, wrapper ); return compileMarkdown(markdown, mergedOptions); }; // Wrapper for user-provided custom renderer // Note: We wrap in a clean Provider to prevent infinite recursion const customRenderMarkdownWrapper = ( markdown: string | ParsedMarkdown, options?: MarkdownProviderOptions, components?: HTMLComponents<'permissive', {}>, wrapper?: FC> ) => ( {customRenderFn?.(markdown, options, components, wrapper) as ReactNode} ); return ( {children} ); };