@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
134 lines (132 loc) • 6.33 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/**
* Pure function to parse code variants and their extraFiles.
* Converts string sources to HAST nodes and handles hastJson parsing.
*/
export function parseCode(code, parseSource) {
var parsed = {};
for (var _i = 0, _Object$entries = Object.entries(code); _i < _Object$entries.length; _i++) {
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
variant = _Object$entries$_i[0],
variantCode = _Object$entries$_i[1];
if (typeof variantCode === 'string') {
// Already parsed/highlighted
parsed[variant] = variantCode;
} else if (variantCode && _typeof(variantCode) === 'object') {
// Parse if source is available and not already parsed, and we have a filename to determine the file type
if (variantCode.source && typeof variantCode.source === 'string' && variantCode.fileName) {
try {
var hastRoot = parseSource(variantCode.source, variantCode.fileName);
// Also parse extraFiles if they contain sources that need parsing
var parsedExtraFiles = variantCode.extraFiles ? Object.fromEntries(Object.entries(variantCode.extraFiles).map(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
fileName = _ref2[0],
fileContent = _ref2[1];
if (typeof fileContent === 'string') {
return [fileName, fileContent]; // Keep string as-is
}
if (fileContent && _typeof(fileContent) === 'object' && fileContent.source) {
if (typeof fileContent.source === 'string') {
// Parse string source in extraFile
try {
var parsedHastRoot = parseSource(fileContent.source, fileName);
return [fileName, _objectSpread(_objectSpread({}, fileContent), {}, {
source: parsedHastRoot
})];
} catch (error) {
return [fileName, fileContent]; // Keep original if parsing fails
}
}
}
return [fileName, fileContent]; // Keep as-is for other cases
})) : undefined;
parsed[variant] = _objectSpread(_objectSpread({}, variantCode), {}, {
source: hastRoot,
extraFiles: parsedExtraFiles
});
} catch (error) {
// Keep original if parsing fails
parsed[variant] = variantCode;
}
} else if (variantCode.source && typeof variantCode.source === 'string' && !variantCode.fileName) {
// Return a basic HAST root node with the source text for unsupported file types
// This indicates that the source has at least passed through the parsing pipeline
parsed[variant] = _objectSpread(_objectSpread({}, variantCode), {}, {
source: {
type: 'root',
children: [{
type: 'text',
value: variantCode.source
}]
}
});
} else if (variantCode.source && _typeof(variantCode.source) === 'object' && 'hastJson' in variantCode.source) {
try {
// Parse hastJson to HAST nodes
var _hastRoot = JSON.parse(variantCode.source.hastJson);
// Also parse extraFiles if they contain sources that need parsing
var _parsedExtraFiles = variantCode.extraFiles ? Object.fromEntries(Object.entries(variantCode.extraFiles).map(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
fileName = _ref4[0],
fileContent = _ref4[1];
if (typeof fileContent === 'string') {
return [fileName, fileContent]; // Keep string as-is
}
if (fileContent && _typeof(fileContent) === 'object' && fileContent.source) {
if (typeof fileContent.source === 'string') {
// Parse string source in extraFile
try {
var parsedHastRoot = parseSource(fileContent.source, fileName);
return [fileName, _objectSpread(_objectSpread({}, fileContent), {}, {
source: parsedHastRoot
})];
} catch (error) {
return [fileName, fileContent]; // Keep original if parsing fails
}
}
}
return [fileName, fileContent]; // Keep as-is for other cases
})) : undefined;
parsed[variant] = _objectSpread(_objectSpread({}, variantCode), {}, {
source: _hastRoot,
extraFiles: _parsedExtraFiles
});
} catch (error) {
// Keep original if parsing fails
console.error("Failed to parse hastJson for variant ".concat(variant, ":"), error);
parsed[variant] = variantCode;
}
} else {
// Already parsed or no source to parse - but still check extraFiles
var _parsedExtraFiles2 = variantCode.extraFiles ? Object.fromEntries(Object.entries(variantCode.extraFiles).map(function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
fileName = _ref6[0],
fileContent = _ref6[1];
if (typeof fileContent === 'string') {
return [fileName, fileContent]; // Keep string as-is
}
if (fileContent && _typeof(fileContent) === 'object' && fileContent.source) {
if (typeof fileContent.source === 'string') {
// Parse string source in extraFile
try {
var parsedHastRoot = parseSource(fileContent.source, fileName);
return [fileName, _objectSpread(_objectSpread({}, fileContent), {}, {
source: parsedHastRoot
})];
} catch (error) {
return [fileName, fileContent]; // Keep original if parsing fails
}
}
}
return [fileName, fileContent]; // Keep as-is for other cases
})) : undefined;
parsed[variant] = _objectSpread(_objectSpread({}, variantCode), {}, {
extraFiles: _parsedExtraFiles2
});
}
}
}
return parsed;
}