Skip to content

Commit

Permalink
optimize_nodesToString (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
VIKTORVAV99 authored Jul 15, 2024
1 parent 92bc147 commit 2f7e67a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,30 @@ export const nodesToString = (children, i18nOptions) => {
// expected e.g. lorem
stringNode += `${child}`;
} else if (isValidElement(child)) {
const childPropsCount = Object.keys(child.props).length;
const shouldKeepChild = keepArray.indexOf(child.type) > -1;
const childChildren = child.props.children;
const { props, type } = child;
const childPropsCount = Object.keys(props).length;
const shouldKeepChild = keepArray.indexOf(type) > -1;
const childChildren = props.children;

if (!childChildren && shouldKeepChild && childPropsCount === 0) {
if (!childChildren && shouldKeepChild && !childPropsCount) {
// actual e.g. lorem <br/> ipsum
// expected e.g. lorem <br/> ipsum
stringNode += `<${child.type}/>`;
} else if (!childChildren && (!shouldKeepChild || childPropsCount !== 0)) {
stringNode += `<${type}/>`;
} else if (
(!childChildren && (!shouldKeepChild || childPropsCount)) ||
props.i18nIsDynamicList
) {
// actual e.g. lorem <hr className="test" /> ipsum
// expected e.g. lorem <0></0> ipsum
stringNode += `<${childIndex}></${childIndex}>`;
} else if (child.props.i18nIsDynamicList) {
// or
// we got a dynamic list like
// e.g. <ul i18nIsDynamicList>{['a', 'b'].map(item => ( <li key={item}>{item}</li> ))}</ul>
// expected e.g. "<0></0>", not e.g. "<0><0>a</0><1>b</1></0>"
stringNode += `<${childIndex}></${childIndex}>`;
} else if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) {
// actual e.g. dolor <strong>bold</strong> amet
// expected e.g. dolor <strong>bold</strong> amet
stringNode += `<${child.type}>${childChildren}</${child.type}>`;
stringNode += `<${type}>${childChildren}</${type}>`;
} else {
// regular case mapping the inner children
const content = nodesToString(childChildren, i18nOptions);
Expand Down

0 comments on commit 2f7e67a

Please sign in to comment.