Skip to content

Commit

Permalink
fix: don't rend the HTML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrxvxx7 committed Oct 4, 2022
1 parent fac1ccc commit 5323ce0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions PolyglotSafariExtension/Sources/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ function translationHandler(message: ReceivedTranslation): void {

const args = {
sourceLanguage: message.sourceLanguage || null,
translation: message.translation.replace(/\n/g, "<br/>"),
transliteration: message.transliteration.replace(/\n/g, "<br/>"),
sourceTransliteration: message.sourceTransliteration.replace(
/\n/g,
"<br/>"
),
translation: convertHTMLToDisplayText(message.translation),
transliteration: convertHTMLToDisplayText(message.transliteration),
sourceTransliteration: convertHTMLToDisplayText(message.sourceTransliteration),
synonyms: message.synonyms
? message.synonyms.map((synonym) => ({
pos: synonym.pos,
Expand Down Expand Up @@ -203,4 +200,17 @@ function getSelectedText(): string | undefined {
}
}

function convertHTMLToDisplayText(htmlString: string): string {
let encodedString = htmlString.replace(/&/g, '&amp;');
encodedString = encodedString.replace(/</g, '&lt;');
encodedString = encodedString.replace(/>/g, '&gt;');
encodedString = encodedString.replace(/"/g, '&quot;');

return convertNewlineToHTML(encodedString)
}

function convertNewlineToHTML(text: string): string {
return text.replace(/\n/g, "<br/>")
}

init();

0 comments on commit 5323ce0

Please sign in to comment.