Skip to content

Commit a113554

Browse files
committed
fix json parser
1 parent 418703d commit a113554

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ function writeTo(path: string, data: string) {
106106
writeFileSync(path, data);
107107
}
108108

109+
function extractJSON(text: string): string {
110+
const match = text.match(/\s*```json\n([\s\S]+?)\n```\s*/);
111+
112+
if (match) {
113+
return match[1];
114+
}
115+
116+
return text;
117+
}
118+
109119
export async function translate<T extends StructureType, F extends FormatterType>(
110120
type: T['type'] | 'auto',
111121
format: F['type'] | 'auto',
@@ -185,7 +195,7 @@ export async function translate<T extends StructureType, F extends FormatterType
185195
text: JSON.stringify(chunk)
186196
});
187197

188-
translated.push(JSON.parse(text));
198+
translated.push(JSON.parse(extractJSON(text)));
189199
}
190200

191201
spinner.succeed(`Translated ${chunks.length} chunks`);

0 commit comments

Comments
 (0)