Skip to content

Commit 35eaa5f

Browse files
authored
fix: no new line when translating twitter in translation only mode (#846)
1 parent 33acbfc commit 35eaa5f

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

.changeset/thick-squids-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@read-frog/extension": patch
3+
---
4+
5+
fix: no new line when translating twitter in translation only mode

src/entrypoints/host.content/style.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
margin: 8px 0 !important; /* 上下边距设置为8px,并强制覆盖其他样式 */
1414
color: inherit; /* 保持文本颜色与周围文本一致 */
1515
font-family: inherit;
16+
white-space: pre-wrap; /* 保留换行符 */
1617
}
1718

1819
.read-frog-translated-inline-content {
1920
display: inline;
2021
color: inherit;
2122
font-family: inherit;
2223
text-decoration: inherit;
24+
white-space: pre-wrap; /* 保留换行符 */
2325
}
2426

2527
/* Simplified Chinese - macOS > Windows > Cross-platform > Fallback */

src/utils/host/dom/traversal.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export function extractTextContent(node: TransNode, config: Config): string {
2323
return node.textContent ?? ''
2424
}
2525

26+
// Handle <br> elements as line breaks
27+
if (isHTMLElement(node) && node.tagName === 'BR') {
28+
return '\n'
29+
}
30+
2631
// We already don't walk and label the element which isDontWalkIntoElement
2732
// for the parent element we already walk and label, if we have a notranslate element inside this parent element,
2833
// we should extract the text content of the parent.

src/utils/host/translate/core/translation-modes.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function translateNodesBilingualMode(
7474
}
7575
}
7676

77-
const textContent = transNodes.map(node => extractTextContent(node, config)).join(' ').trim()
77+
const textContent = transNodes.map(node => extractTextContent(node, config)).join('').trim()
7878
if (!textContent || isNumericContent(textContent))
7979
return
8080

@@ -213,7 +213,7 @@ export async function translateNodeTranslationOnlyMode(
213213
}
214214
}
215215

216-
const innerTextContent = transNodes.map(node => extractTextContent(node, config)).join(' ')
216+
const innerTextContent = transNodes.map(node => extractTextContent(node, config)).join('')
217217
if (!innerTextContent.trim() || isNumericContent(innerTextContent))
218218
return
219219

@@ -223,7 +223,11 @@ export async function translateNodeTranslationOnlyMode(
223223

224224
let cleanedContent = content.replace(MARK_ATTRIBUTES_REGEX, '')
225225
cleanedContent = cleanedContent.replace(/<!--[\s\S]*?-->/g, ' ')
226-
cleanedContent = cleanedContent.replace(/\s+/g, ' ').trim()
226+
// Preserve newlines, only collapse horizontal whitespace (spaces/tabs)
227+
cleanedContent = cleanedContent.replace(/[^\S\n]+/g, ' ')
228+
// Trim spaces at start/end of each line, but preserve newlines
229+
cleanedContent = cleanedContent.split('\n').map(line => line.trim()).join('\n')
230+
cleanedContent = cleanedContent.trim()
227231

228232
return cleanedContent
229233
}
@@ -276,7 +280,8 @@ export async function translateNodeTranslationOnlyMode(
276280
return
277281
}
278282

279-
translatedWrapperNode.innerHTML = translatedText
283+
// Convert newlines to <br> for proper rendering in innerHTML
284+
translatedWrapperNode.innerHTML = translatedText.replace(/\n/g, '<br>')
280285

281286
// Batch final DOM mutations to reduce layout thrashing
282287
batchDOMOperation(() => {

0 commit comments

Comments
 (0)