UNPKG

react-intlayer

Version:

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

102 lines (100 loc) 3.34 kB
import { compile, parseMarkdown as parseMarkdown$1, renderMarkdownAst } from "@intlayer/core/markdown"; import { Fragment, cloneElement, createElement } from "react"; //#region src/markdown/processor.tsx /** * it's a fork * [markdown-to-jsx v7.7.14](https://github.com/quantizor/markdown-to-jsx) from quantizor * [simple-markdown v0.2.2](https://github.com/Khan/simple-markdown) from Khan Academy. */ /** * Default React runtime for markdown rendering. */ const DEFAULT_RUNTIME = { createElement, cloneElement, Fragment, normalizeProps: (_tag, props) => props }; /** * **Step 1 of 2 — parse only.** * Converts a raw markdown string into a `ParsedMarkdown` AST without rendering * any React elements. Use this when you need to: * - Cache the parsed result and render it several times with different options. * - Inspect or transform the AST before rendering. * - Defer the render step to a later point in the pipeline. * * @param markdown - The markdown source string. * @param options - Options that affect parsing (sanitizer, slugify, …). * @returns A `ParsedMarkdown` AST ready to be passed to `compileMarkdown`. * * @example * ```tsx * const ast = parseMarkdown('# Hello **world**'); * const element = compileMarkdown(ast, { forceBlock: true }); * ``` */ const parseMarkdown = (markdown = "", options = {}) => { const { disableAutoLink, disableParsingRawHTML, enforceAtxHeadings, forceBlock, forceInline, forceWrapper, namedCodesToUnicode, components, sanitizer, slugify, wrapper, preserveFrontmatter, tagfilter } = options; return parseMarkdown$1(markdown, { runtime: DEFAULT_RUNTIME, components, namedCodesToUnicode, sanitizer, slugify }, { disableAutoLink, disableParsingRawHTML, enforceAtxHeadings, forceBlock, forceInline, forceWrapper, wrapper, preserveFrontmatter, tagfilter }); }; /** * **Steps 1 + 2 — parse and render in one shot.** * Accepts a raw markdown string or a pre-parsed `ParsedMarkdown` AST and * returns a React element. Use `parseMarkdown` first when you need to reuse * the same AST with different options. * * @param input - Markdown string or pre-parsed AST. * @param options - Rendering options (custom components, sanitizer, slugify, …). * @returns A React JSX element representing the rendered markdown. * * @example * ```tsx * const element = compileMarkdown('# Hello **world**', { forceBlock: true }); * ``` */ const compileMarkdown = (input = "", options = {}) => { const { createElement: customCreateElement, disableAutoLink, disableParsingRawHTML, enforceAtxHeadings, forceBlock, forceInline, forceWrapper, namedCodesToUnicode, components, renderRule, sanitizer, slugify, wrapper, preserveFrontmatter, tagfilter } = options; const ctx = { runtime: customCreateElement ? { ...DEFAULT_RUNTIME, createElement: customCreateElement } : DEFAULT_RUNTIME, components, namedCodesToUnicode, sanitizer, slugify }; const compilerOptions = { disableAutoLink, disableParsingRawHTML, enforceAtxHeadings, forceBlock, forceInline, forceWrapper, renderRule, wrapper, preserveFrontmatter, tagfilter }; if (typeof input === "string") return compile(input, ctx, compilerOptions); return renderMarkdownAst(input, ctx, compilerOptions); }; //#endregion export { compileMarkdown, parseMarkdown }; //# sourceMappingURL=processor.mjs.map