Skip to content

Commit 154e2d0

Browse files
committed
fix: parser uses match to return matched content only
1 parent 66eb36d commit 154e2d0

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/client.js

+29-18
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,39 @@ async function send(conversation, action) {
7474
}
7575

7676
function parseContent(content, valueType) {
77-
let parsedContent;
77+
let regex;
78+
79+
// Define the regular expression based on the valueType
80+
switch (valueType) {
81+
case 'json':
82+
regex = /```json\n([\s\S]*?)\n```/;
83+
break;
84+
case 'javascript':
85+
regex = /```javascript\n([\s\S]*?)\n```/;
86+
break;
87+
case 'html':
88+
regex = /```html\n([\s\S]*?)\n```/;
89+
break;
90+
case 'css':
91+
regex = /```css\n([\s\S]*?)\n```/;
92+
break;
93+
default:
94+
break;
95+
}
96+
97+
// Extract the relevant section using the regular expression
98+
if (regex) {
99+
const match = content.match(regex);
100+
if (match && match[1]) {
101+
content = match[1].trim(); // Extracted content between the code block markers
102+
}
103+
}
78104

79105
if (valueType === 'json') {
80-
content = content.replace(/```json\n|\n```/g, '');
81-
parsedContent = JSON.parse(content);
82-
} else if (valueType === 'javascript') {
83-
content = content.replace(/```javascript\n|\n```/g, '');
84-
parsedContent = content;
85-
} else if (valueType === 'html') {
86-
content = content.replace(/```html\n|\n```/g, '');
87-
parsedContent = content;
88-
} else if (valueType === 'css') {
89-
content = content.replace(/```css\n|\n```/g, '');
90-
parsedContent = content;
91-
} else if (valueType === 'markdown') {
92-
parsedContent = content;
93-
} else {
94-
// Default case to handle any other types or plain text
95-
parsedContent = content;
106+
content = JSON.parse(content);
96107
}
97108

98-
return parsedContent;
109+
return content;
99110
}
100111

101112
// Function to extract the object from the generated code

0 commit comments

Comments
 (0)