File tree 2 files changed +25
-4
lines changed
2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,27 @@ describe("Parsing Module", () => {
67
67
] ) ;
68
68
} ) ;
69
69
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
+
70
91
it ( "should handle empty arrays" , ( ) => {
71
92
expect ( parseJsonArrayFromText ( "```json\n[]\n```" ) ) . toEqual ( [ ] ) ;
72
93
expect ( parseJsonArrayFromText ( "[]" ) ) . toEqual ( null ) ;
Original file line number Diff line number Diff line change @@ -90,8 +90,8 @@ export function parseJsonArrayFromText(text: string) {
90
90
91
91
if ( jsonBlockMatch ) {
92
92
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 "' ) ;
95
95
jsonData = JSON . parse ( normalizedJson ) ;
96
96
} catch ( e ) {
97
97
console . error ( "Error parsing JSON:" , e ) ;
@@ -106,8 +106,8 @@ export function parseJsonArrayFromText(text: string) {
106
106
107
107
if ( arrayMatch ) {
108
108
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 "' ) ;
111
111
jsonData = JSON . parse ( normalizedJson ) ;
112
112
} catch ( e ) {
113
113
console . error ( "Text is not JSON" , text ) ;
You can’t perform that action at this time.
0 commit comments