@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
83 lines (81 loc) • 3.27 kB
JavaScript
'use client';
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import { createStarryNight } from '@wooorm/starry-night';
import { CodeContext } from "./CodeContext.js";
import { extensionMap, grammars } from "../pipeline/parseSource/grammars.js";
import { starryNightGutter } from "../pipeline/parseSource/addLineGutters.js";
// Import the heavy functions
import { loadFallbackCode } from "../CodeHighlighter/loadFallbackCode.js";
import { loadVariant } from "../CodeHighlighter/loadVariant.js";
import { parseCode } from "../CodeHighlighter/parseCode.js";
import { parseControlledCode } from "../CodeHighlighter/parseControlledCode.js";
import { applyTransforms, getAvailableTransforms } from "../CodeHighlighter/transformCode.js";
import { jsx as _jsx } from "react/jsx-runtime";
export function CodeProvider(_ref) {
var children = _ref.children,
loadCodeMeta = _ref.loadCodeMeta,
loadVariantMeta = _ref.loadVariantMeta,
loadSource = _ref.loadSource;
var _React$useState = React.useState(undefined),
_React$useState2 = _slicedToArray(_React$useState, 2),
parseSource = _React$useState2[0],
setParseSource = _React$useState2[1];
var sourceParser = React.useMemo(function () {
// Only initialize Starry Night in the browser, not during SSR
if (typeof window === 'undefined') {
return Promise.resolve(function () {
throw new Error('parseSource not available during SSR');
});
}
return createStarryNight(grammars).then(function (starryNight) {
var parseSourceFn = function parseSourceFn(source, fileName) {
var fileType = fileName.slice(fileName.lastIndexOf('.'));
if (!extensionMap[fileType]) {
// Return a basic HAST root node with the source text for unsupported file types
return {
type: 'root',
children: [{
type: 'text',
value: source
}]
};
}
var highlighted = starryNight.highlight(source, extensionMap[fileType]);
var sourceLines = source.split(/\r?\n|\r/);
starryNightGutter(highlighted, sourceLines); // mutates the tree to add line gutters
return highlighted;
};
return parseSourceFn;
});
}, []);
React.useEffect(function () {
// Update the sync version when available
sourceParser.then(function (parseSourceFn) {
return setParseSource(function () {
return parseSourceFn;
});
});
}, [sourceParser]);
var context = React.useMemo(function () {
return {
sourceParser: sourceParser,
parseSource: parseSource,
// Sync version when available
loadSource: loadSource,
loadVariantMeta: loadVariantMeta,
loadCodeMeta: loadCodeMeta,
// Provide the heavy functions
loadFallbackCode: loadFallbackCode,
loadVariant: loadVariant,
parseCode: parseCode,
parseControlledCode: parseControlledCode,
applyTransforms: applyTransforms,
getAvailableTransforms: getAvailableTransforms
};
}, [sourceParser, parseSource, loadSource, loadVariantMeta, loadCodeMeta]);
return /*#__PURE__*/_jsx(CodeContext.Provider, {
value: context,
children: children
});
}