UNPKG

react-intlayer

Version:

Easily internationalize i18n your React applications with type-safe multilingual content management.

1 lines 7.66 kB
{"version":3,"file":"processor.cjs","names":["createElement"],"sources":["../../../src/markdown/processor.tsx"],"sourcesContent":["/**\n * it's a fork\n * [markdown-to-jsx v7.7.14](https://github.com/quantizor/markdown-to-jsx) from quantizor\n * [simple-markdown v0.2.2](https://github.com/Khan/simple-markdown) from Khan Academy.\n */\n\nimport {\n compile as coreCompile,\n parseMarkdown as coreParseMarkdown,\n renderMarkdownAst as coreRenderMarkdownAst,\n type MarkdownContext,\n type MarkdownOptions,\n type MarkdownRuntime,\n type ParsedMarkdown,\n type RenderRuleHook,\n} from '@intlayer/core/markdown';\nimport {\n cloneElement,\n createElement,\n Fragment,\n type JSX,\n type ReactNode,\n} from 'react';\nimport type { HTMLComponents } from '../html/HTMLComponentTypes';\n\ntype HTMLTags = keyof JSX.IntrinsicElements;\n\n/**\n * Refined MarkdownRendererOptions type.\n */\nexport type MarkdownRendererOptions = Partial<{\n /**\n * Ultimate control over the output of all rendered JSX.\n */\n createElement: (\n tag: Parameters<typeof createElement>[0],\n props: JSX.IntrinsicAttributes,\n ...children: ReactNode[]\n ) => ReactNode;\n\n /**\n * The library automatically generates an anchor tag for bare URLs included in the markdown\n * document, but this behavior can be disabled if desired.\n */\n disableAutoLink: boolean;\n\n /**\n * Disable the compiler's best-effort transcription of provided raw HTML\n * into JSX-equivalent.\n */\n disableParsingRawHTML: boolean;\n\n /**\n * Forces the compiler to have space between hash sign and the header text.\n */\n enforceAtxHeadings: boolean;\n\n /**\n * Forces the compiler to always output content with a block-level wrapper.\n */\n forceBlock: boolean;\n\n /**\n * Forces the compiler to always output content with an inline wrapper.\n */\n forceInline: boolean;\n\n /**\n * Forces the compiler to wrap results, even if there is only a single child.\n */\n forceWrapper: boolean;\n\n /**\n * Supply additional HTML entity: unicode replacement mappings.\n */\n namedCodesToUnicode: {\n [key: string]: string;\n };\n\n /**\n * Selectively control the output of particular HTML tags.\n */\n components: HTMLComponents;\n\n /**\n * Allows for full control over rendering of particular rules.\n */\n renderRule: RenderRuleHook;\n\n /**\n * Override the built-in sanitizer function for URLs.\n */\n sanitizer: (value: string, tag: HTMLTags, attribute: string) => string | null;\n\n /**\n * Override normalization of non-URI-safe characters for anchor linking.\n */\n slugify: (input: string) => string;\n\n /**\n * Declare the type of the wrapper to be used when there are multiple children.\n */\n wrapper: any | null;\n\n /**\n * Whether to preserve frontmatter in the markdown content.\n */\n preserveFrontmatter: boolean;\n\n /**\n * Whether to use the GitHub Tag Filter.\n */\n tagfilter: boolean;\n}>;\n\n/**\n * Default React runtime for markdown rendering.\n */\nconst DEFAULT_RUNTIME: MarkdownRuntime = {\n createElement: createElement as any,\n cloneElement,\n Fragment,\n normalizeProps: (_tag, props) => props,\n};\n\n/**\n * Intermediate AST produced by `parseMarkdown`.\n * Pass this to `compileMarkdown` or `<MarkdownRenderer>` to skip re-parsing\n * when the same content is rendered multiple times.\n */\nexport type { ParsedMarkdown };\n\n/**\n * **Step 1 of 2 — parse only.**\n * Converts a raw markdown string into a `ParsedMarkdown` AST without rendering\n * any React elements. Use this when you need to:\n * - Cache the parsed result and render it several times with different options.\n * - Inspect or transform the AST before rendering.\n * - Defer the render step to a later point in the pipeline.\n *\n * @param markdown - The markdown source string.\n * @param options - Options that affect parsing (sanitizer, slugify, …).\n * @returns A `ParsedMarkdown` AST ready to be passed to `compileMarkdown`.\n *\n * @example\n * ```tsx\n * const ast = parseMarkdown('# Hello **world**');\n * const element = compileMarkdown(ast, { forceBlock: true });\n * ```\n */\nexport const parseMarkdown = (\n markdown: string = '',\n options: MarkdownRendererOptions = {}\n): ParsedMarkdown => {\n const {\n disableAutoLink,\n disableParsingRawHTML,\n enforceAtxHeadings,\n forceBlock,\n forceInline,\n forceWrapper,\n namedCodesToUnicode,\n components,\n sanitizer,\n slugify,\n wrapper,\n preserveFrontmatter,\n tagfilter,\n } = options;\n\n const ctx: MarkdownContext<HTMLComponents> = {\n runtime: DEFAULT_RUNTIME,\n components,\n namedCodesToUnicode,\n sanitizer: sanitizer as any,\n slugify,\n };\n\n const compilerOptions: MarkdownOptions = {\n disableAutoLink,\n disableParsingRawHTML,\n enforceAtxHeadings,\n forceBlock,\n forceInline,\n forceWrapper,\n wrapper,\n preserveFrontmatter,\n tagfilter,\n };\n\n return coreParseMarkdown(markdown, ctx, compilerOptions);\n};\n\n/**\n * **Steps 1 + 2 — parse and render in one shot.**\n * Accepts a raw markdown string or a pre-parsed `ParsedMarkdown` AST and\n * returns a React element. Use `parseMarkdown` first when you need to reuse\n * the same AST with different options.\n *\n * @param input - Markdown string or pre-parsed AST.\n * @param options - Rendering options (custom components, sanitizer, slugify, …).\n * @returns A React JSX element representing the rendered markdown.\n *\n * @example\n * ```tsx\n * const element = compileMarkdown('# Hello **world**', { forceBlock: true });\n * ```\n */\nexport const compileMarkdown = (\n input: string | ParsedMarkdown = '',\n options: MarkdownRendererOptions = {}\n): JSX.Element => {\n const {\n createElement: customCreateElement,\n disableAutoLink,\n disableParsingRawHTML,\n enforceAtxHeadings,\n forceBlock,\n forceInline,\n forceWrapper,\n namedCodesToUnicode,\n components,\n renderRule,\n sanitizer,\n slugify,\n wrapper,\n preserveFrontmatter,\n tagfilter,\n } = options;\n\n const runtime = customCreateElement\n ? { ...DEFAULT_RUNTIME, createElement: customCreateElement as any }\n : DEFAULT_RUNTIME;\n\n const ctx: MarkdownContext<HTMLComponents> = {\n runtime,\n components,\n namedCodesToUnicode,\n sanitizer: sanitizer as any,\n slugify,\n };\n\n const compilerOptions: MarkdownOptions = {\n disableAutoLink,\n disableParsingRawHTML,\n enforceAtxHeadings,\n forceBlock,\n forceInline,\n forceWrapper,\n renderRule,\n wrapper,\n preserveFrontmatter,\n tagfilter,\n };\n\n if (typeof input === 'string') {\n return coreCompile(input, ctx, compilerOptions) as JSX.Element;\n }\n\n return coreRenderMarkdownAst(input, ctx, compilerOptions) as JSX.Element;\n};\n"],"mappings":";;;;;;;;;;;;;;AAsHA,MAAM,kBAAmC;CACvC,eAAeA;CACf;CACA;CACA,iBAAiB,MAAM,UAAU;CAClC;;;;;;;;;;;;;;;;;;;AA2BD,MAAa,iBACX,WAAmB,IACnB,UAAmC,EAAE,KAClB;CACnB,MAAM,EACJ,iBACA,uBACA,oBACA,YACA,aACA,cACA,qBACA,YACA,WACA,SACA,SACA,qBACA,cACE;AAsBJ,mDAAyB,UAAU;EAnBjC,SAAS;EACT;EACA;EACW;EACX;EAeoC,EAAE;EAXtC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAGqD,CAAC;;;;;;;;;;;;;;;;;AAkB1D,MAAa,mBACX,QAAiC,IACjC,UAAmC,EAAE,KACrB;CAChB,MAAM,EACJ,eAAe,qBACf,iBACA,uBACA,oBACA,YACA,aACA,cACA,qBACA,YACA,YACA,WACA,SACA,SACA,qBACA,cACE;CAMJ,MAAM,MAAuC;EAC3C,SALc,sBACZ;GAAE,GAAG;GAAiB,eAAe;GAA4B,GACjE;EAIF;EACA;EACW;EACX;EACD;CAED,MAAM,kBAAmC;EACvC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD;AAED,KAAI,OAAO,UAAU,SACnB,6CAAmB,OAAO,KAAK,gBAAgB;AAGjD,uDAA6B,OAAO,KAAK,gBAAgB"}