Skip to content

Commit

Permalink
fix json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Nov 1, 2024
1 parent 418703d commit a113554
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ function writeTo(path: string, data: string) {
writeFileSync(path, data);
}

function extractJSON(text: string): string {
const match = text.match(/\s*```json\n([\s\S]+?)\n```\s*/);

if (match) {
return match[1];
}

return text;
}

export async function translate<T extends StructureType, F extends FormatterType>(
type: T['type'] | 'auto',
format: F['type'] | 'auto',
Expand Down Expand Up @@ -185,7 +195,7 @@ export async function translate<T extends StructureType, F extends FormatterType
text: JSON.stringify(chunk)
});

translated.push(JSON.parse(text));
translated.push(JSON.parse(extractJSON(text)));
}

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

0 comments on commit a113554

Please sign in to comment.