@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
99 lines (98 loc) • 3.18 kB
JavaScript
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
import { toText } from 'hast-util-to-text';
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
import { decompressHast } from "./hastCompression.mjs";
import { fallbackToText } from "./fallbackFormat.mjs";
/**
* Resolve the DEFLATE dictionary text for a `hastCompressed` payload from a
* compact `fallback`. The loader compresses each file with its own fallback
* text as the dictionary (see `loadIsomorphicCodeVariant`), so the same
* fallback must be supplied here to decode it. Returns `undefined` when no
* fallback is given (payloads compressed without a dictionary).
*/
function fallbackDictionary(fallback) {
return fallback ? fallbackToText(fallback) : undefined;
}
export function hastToJsx(hast, components) {
return toJsxRuntime(hast, {
Fragment,
jsx,
jsxs,
components
});
}
export function hastOrJsonToJsx(hastOrJson, components, fallback) {
let hast;
if ('hastJson' in hastOrJson) {
try {
hast = JSON.parse(hastOrJson.hastJson);
} catch (error) {
throw new Error(`Failed to parse hastJson: ${error instanceof Error ? error.message : String(error)}`);
}
} else if ('hastCompressed' in hastOrJson) {
try {
hast = JSON.parse(decompressHast(hastOrJson.hastCompressed, fallbackDictionary(fallback)));
} catch (error) {
throw new Error(`Failed to parse hastCompressed: ${error instanceof Error ? error.message : String(error)}`);
}
} else {
hast = hastOrJson;
}
return toJsxRuntime(hast, {
Fragment,
jsx,
jsxs,
components
});
}
export function stringOrHastToString(source, fallback) {
if (typeof source === 'string') {
return source;
}
let hast;
if ('hastJson' in source) {
try {
hast = JSON.parse(source.hastJson);
} catch (error) {
throw new Error(`Failed to parse hastJson: ${error instanceof Error ? error.message : String(error)}`);
}
} else if ('hastCompressed' in source) {
try {
hast = JSON.parse(decompressHast(source.hastCompressed, fallbackDictionary(fallback)));
} catch (error) {
throw new Error(`Failed to parse hastCompressed: ${error instanceof Error ? error.message : String(error)}`);
}
} else {
hast = source;
}
return toText(hast, {
whitespace: 'pre'
});
}
export function stringOrHastToJsx(source, highlighted, components, fallback) {
if (typeof source === 'string') {
return source;
}
let hast;
if ('hastJson' in source) {
try {
hast = JSON.parse(source.hastJson);
} catch (error) {
throw new Error(`Failed to parse hastJson: ${error instanceof Error ? error.message : String(error)}`);
}
} else if ('hastCompressed' in source) {
try {
hast = JSON.parse(decompressHast(source.hastCompressed, fallbackDictionary(fallback)));
} catch (error) {
throw new Error(`Failed to parse hastCompressed: ${error instanceof Error ? error.message : String(error)}`);
}
} else {
hast = source;
}
if (highlighted && typeof hast === 'object') {
return hastToJsx(hast, components);
}
return toText(hast, {
whitespace: 'pre'
});
}