@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
87 lines (85 loc) • 3.35 kB
JavaScript
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/**
* Pure function to parse controlled code and convert it to regular Code format.
* Handles the conversion from ControlledCode (string|null sources) to Code (HAST nodes).
*/
export function parseControlledCode(controlledCode, parseSource) {
var parsed = {};
for (var _i = 0, _Object$entries = Object.entries(controlledCode); _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 (variantCode === null) {
// Explicitly deleted - skip this variant
continue;
}
if (variantCode && _typeof(variantCode) === 'object') {
var mainSource = void 0;
// Convert null to empty string, then parse
var sourceToProcess = variantCode.source === null ? '' : variantCode.source;
if (typeof sourceToProcess === 'string' && variantCode.fileName) {
try {
mainSource = parseSource(sourceToProcess, variantCode.fileName);
} catch (error) {
// Keep original string if parsing fails
mainSource = sourceToProcess;
}
} else if (typeof sourceToProcess === '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
var source = {
type: 'root',
children: [{
type: 'text',
value: sourceToProcess
}]
};
mainSource = source;
} else {
// Handle undefined or other non-string values
mainSource = sourceToProcess;
}
// Parse extraFiles if present
var extraFiles = void 0;
if (variantCode.extraFiles) {
var parsedExtraFiles = {};
for (var _i2 = 0, _Object$entries2 = Object.entries(variantCode.extraFiles); _i2 < _Object$entries2.length; _i2++) {
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
fileName = _Object$entries2$_i[0],
fileData = _Object$entries2$_i[1];
// Convert null to empty string, then parse
var fileSourceToProcess = fileData.source === null ? '' : fileData.source;
if (typeof fileSourceToProcess === 'string') {
// Parse string sources
try {
var parsedSource = parseSource(fileSourceToProcess, fileName);
parsedExtraFiles[fileName] = {
source: parsedSource
};
} catch (error) {
// Keep original if parsing fails
parsedExtraFiles[fileName] = {
source: fileSourceToProcess
};
}
} else {
// Keep other values as-is
parsedExtraFiles[fileName] = {
source: fileSourceToProcess
};
}
}
extraFiles = parsedExtraFiles;
}
parsed[variant] = {
fileName: variantCode.fileName,
url: variantCode.url,
source: mainSource,
extraFiles: extraFiles,
filesOrder: variantCode.filesOrder
};
}
}
return parsed;
}