From e9f274f3042030649962fff20656ad842e4ad20c Mon Sep 17 00:00:00 2001 From: Sma11X <540351143@qq.com> Date: Fri, 13 Oct 2023 15:12:48 +0800 Subject: [PATCH] feat: replace links in status after translated (#2385) Co-authored-by: patak --- composables/masto/translate.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/composables/masto/translate.ts b/composables/masto/translate.ts index af788b4c..969388aa 100644 --- a/composables/masto/translate.ts +++ b/composables/masto/translate.ts @@ -65,6 +65,7 @@ export async function translateText(text: string, from: string | null | undefine error: '', text: '', }) + const regex = /]*>.*?<\/a>/g try { const response = await ($fetch as any)(config.public.translateApi, { method: 'POST', @@ -77,7 +78,11 @@ export async function translateText(text: string, from: string | null | undefine }, }) as TranslationResponse status.success = true - status.text = response.translatedText + // replace the translated links with the original + status.text = response.translatedText.replace(regex, (match) => { + const tagLink = regex.exec(text) + return tagLink ? tagLink[0] : match + }) } catch (err) { // TODO: improve type