Skip to content

Commit bfcb424

Browse files
authored
fix(compiler): replace elements with dot in name (#1011)
1 parent 728f0c3 commit bfcb424

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

.changeset/spotty-cobras-greet.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@lingo.dev/_compiler": patch
3+
"@lingo.dev/_react": patch
4+
"lingo.dev": patch
5+
---
6+
7+
replace elements with dot in name

packages/compiler/src/utils/jsx-content.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ describe("JSX Content Utils", () => {
6060

6161
it("should handle whitespaces between elements", () => {
6262
const path = getJSXElementPath(
63-
"<div>\n Hello <strong>crazy</strong> world!</div>",
63+
"<div>\n Hello <strong>crazy</strong> world! <Icons.Rocket /></div>",
6464
);
6565
const content = extractJsxContent(path);
6666
expect(content).toBe(
67-
"Hello <element:strong>crazy</element:strong> world!",
67+
"Hello <element:strong>crazy</element:strong> world! <element:Icons.Rocket></element:Icons.Rocket>",
6868
);
6969
});
7070

packages/react/src/core/component.spec.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe("LingoComponent", () => {
99
entries: {
1010
greeting: "Hello {user.profile.name} you have {count} messages",
1111
welcome:
12-
"Welcome <element:a>incredible <element:span>fantastic <element:em>wonderful <element:strong>amazing</element:strong></element:em></element:span> user</element:a>",
12+
"Welcome <element:a>incredible <element:span>fantastic <element:em>wonderful <element:strong>amazing</element:strong></element:em></element:span> user</element:a> <element:Icons.Rocket></element:Icons.Rocket>",
1313
complex:
1414
"<element:a>Hello {user.profile.name}, welcome to <element:span>wonderful <element:strong><element:em>{placeholder}</element:em> nested</element:strong></element:span> world</element:a> of the <element:u>universe number {count}</element:u>",
1515
},
@@ -49,6 +49,10 @@ describe("LingoComponent", () => {
4949
});
5050

5151
it("replaces element placeholders", () => {
52+
const Icons = {
53+
Rocket: () => <span>🚀</span>,
54+
};
55+
5256
const { container } = render(
5357
<LingoComponent
5458
$dictionary={dictionary}
@@ -60,11 +64,12 @@ describe("LingoComponent", () => {
6064
({ children }: any) => <span>{children}</span>,
6165
({ children }: any) => <em>{children}</em>,
6266
({ children }: any) => <strong className="red">{children}</strong>,
67+
({ children }: any) => <Icons.Rocket />,
6368
]}
6469
/>,
6570
);
6671
expect(container.innerHTML).toBe(
67-
'<div>Welcome <a href="#">incredible <span>fantastic <em>wonderful <strong class="red">amazing</strong></em></span> user</a></div>',
72+
'<div>Welcome <a href="#">incredible <span>fantastic <em>wonderful <strong class="red">amazing</strong></em></span> user</a> <span>🚀</span></div>',
6873
);
6974
});
7075

packages/react/src/core/component.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,15 @@ function replaceElements(
134134
elements?: Array<FunctionComponent>,
135135
elementIndex: { current: number } = { current: 0 },
136136
): ReactNode[] {
137+
const ELEMENT_PATTERN = /<element:([\w.]+)>(.*?)<\/element:\1>/gs;
138+
137139
if (_.isEmpty(elements)) {
138140
return nodes.map((node) => {
139141
if (typeof node !== "string") return node;
140142

141-
return node.replace(
142-
/<element:(\w+)>(.*?)<\/element:\1>/gs,
143-
(match, elementName, content) => {
144-
return content;
145-
},
146-
);
143+
return node.replace(ELEMENT_PATTERN, (match, elementName, content) => {
144+
return content;
145+
});
147146
});
148147
}
149148

@@ -153,7 +152,7 @@ function replaceElements(
153152

154153
const segments: ReactNode[] = [];
155154
let lastIndex = 0;
156-
const ELEMENT_PATTERN = /<element:(\w+)>(.*?)<\/element:\1>/gs;
155+
157156
let match;
158157

159158
while ((match = ELEMENT_PATTERN.exec(node)) !== null) {

0 commit comments

Comments
 (0)