Skip to content

Commit 9cdc760

Browse files
committed
fix: single quote handle bug at parseJsonArray
1 parent 0f9441a commit 9cdc760

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Diff for: packages/core/__tests__/parsing.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ describe("Parsing Module", () => {
6767
]);
6868
});
6969

70+
it("should handle single quote", () => {
71+
let input = "```json\n['item1', 'item2', 'item3']\n```";
72+
expect(parseJsonArrayFromText(input)).toEqual([
73+
"item1",
74+
"item2",
75+
"item3",
76+
]);
77+
input = "```json\n[\"A's item\", \"B's item\", \"C's item\"]\n```";
78+
expect(parseJsonArrayFromText(input)).toEqual([
79+
"A's item",
80+
"B's item",
81+
"C's item",
82+
]);
83+
input = "[\"A's item\", \"B's item\", \"C's item\"]";
84+
expect(parseJsonArrayFromText(input)).toEqual([
85+
"A's item",
86+
"B's item",
87+
"C's item",
88+
]);
89+
});
90+
7091
it("should handle empty arrays", () => {
7192
expect(parseJsonArrayFromText("```json\n[]\n```")).toEqual([]);
7293
expect(parseJsonArrayFromText("[]")).toEqual(null);

Diff for: packages/core/src/parsing.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export function parseJsonArrayFromText(text: string) {
9090

9191
if (jsonBlockMatch) {
9292
try {
93-
// Replace single quotes with double quotes before parsing
94-
const normalizedJson = jsonBlockMatch[1].replace(/'/g, '"');
93+
// Only replace quotes that are actually being used for string delimitation
94+
const normalizedJson = jsonBlockMatch[1].replace(/(?<!\\)'([^']*)'(?=[,}\]])/g, '"$1"');
9595
jsonData = JSON.parse(normalizedJson);
9696
} catch (e) {
9797
console.error("Error parsing JSON:", e);
@@ -106,8 +106,8 @@ export function parseJsonArrayFromText(text: string) {
106106

107107
if (arrayMatch) {
108108
try {
109-
// Replace single quotes with double quotes before parsing
110-
const normalizedJson = arrayMatch[0].replace(/'/g, '"');
109+
// Only replace quotes that are actually being used for string delimitation
110+
const normalizedJson = arrayMatch[0].replace(/(?<!\\)'([^']*)'(?=[,}\]])/g, '"$1"');
111111
jsonData = JSON.parse(normalizedJson);
112112
} catch (e) {
113113
console.error("Text is not JSON", text);

0 commit comments

Comments
 (0)