@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
60 lines (58 loc) • 1.6 kB
JavaScript
import { deepTransformNode } from "../interpreter/getContent/deepTransform.mjs";
//#region src/deepTransformPlugins/getReplacedValuesContent.ts
const replaceValuesPlugin = (value) => ({
id: "replace-values-plugin",
canHandle: (node) => typeof node === "string" || typeof node === "number" || typeof node === "boolean",
transform: () => value
});
const skipTypedNodePlugin = {
id: "skip-typed-node-plugin",
canHandle: (node) => typeof node === "object" && typeof node?.nodeType === "string",
transform: (node, props, deepTransformNode$1) => {
const nodeType = node.nodeType;
const result = structuredClone(node[nodeType]);
if (typeof result !== "object" || result === null) {
const transformedResult = deepTransformNode$1(result, {
...props,
children: result,
keyPath: [...props.keyPath, {
type: nodeType,
key: nodeType
}]
});
return {
...node,
[nodeType]: transformedResult
};
}
for (const key in result) {
const childProps = {
...props,
children: result[key],
keyPath: [...props.keyPath, {
type: nodeType,
key
}]
};
result[key] = deepTransformNode$1(result[key], childProps);
}
return {
...node,
[nodeType]: result
};
}
};
const getReplacedValuesContent = (node, value, nodeProps) => {
const plugins = [
skipTypedNodePlugin,
replaceValuesPlugin(value),
...nodeProps.plugins ?? []
];
return deepTransformNode(JSON.parse(JSON.stringify(node)), {
...nodeProps,
plugins
});
};
//#endregion
export { getReplacedValuesContent };
//# sourceMappingURL=getReplacedValuesContent.mjs.map