Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace links in status after translated #2385

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion composables/masto/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export async function translateText(text: string, from: string | null | undefine
error: '',
text: '',
})
const regex = /<a[^>]*>.*?<\/a>/g
try {
const response = await ($fetch as any)(config.public.translateApi, {
method: 'POST',
Expand All @@ -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
Expand Down
Loading