react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
105 lines (103 loc) • 3.59 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
let _intlayer_core_markdown = require("@intlayer/core/markdown");
let react = require("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: react.createElement,
cloneElement: react.cloneElement,
Fragment: react.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 (0, _intlayer_core_markdown.parseMarkdown)(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 (0, _intlayer_core_markdown.compile)(input, ctx, compilerOptions);
return (0, _intlayer_core_markdown.renderMarkdownAst)(input, ctx, compilerOptions);
};
//#endregion
exports.compileMarkdown = compileMarkdown;
exports.parseMarkdown = parseMarkdown;
//# sourceMappingURL=processor.cjs.map