-
Notifications
You must be signed in to change notification settings - Fork 278
GitHub 网站 Pull Requests 和 Issue 段落级而非容器级翻译粒度问题修复 #189
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
Open
T1mn
wants to merge
2
commits into
Bistutu:main
Choose a base branch
from
T1mn:tanggf/fix_comment_paragraphs_translation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
🧙 Sourcery has finished reviewing your pull request! Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 2 issues, and left some high level feedback:
- The new
findTranslatableParentno longer callsgrabNode, which may bypass existing logic for deciding whether a node is translatable/should be skipped; consider preserving thegrabNode-based check while still enforcing a block-level boundary. - The selector
div.comment-body td.comment-bodylooks suspicious for GitHub, asdiv.comment-bodyandtd.comment-bodyare typically siblings/alternatives rather than nested; consider using separate or comma-separated selectors so both cases are matched reliably. - The
blockElementsSet infindTranslatableParentis recreated on every call; moving it to a module-level constant would avoid repeated allocations and make it easier to reuse or extend.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `findTranslatableParent` no longer calls `grabNode`, which may bypass existing logic for deciding whether a node is translatable/should be skipped; consider preserving the `grabNode`-based check while still enforcing a block-level boundary.
- The selector `div.comment-body td.comment-body` looks suspicious for GitHub, as `div.comment-body` and `td.comment-body` are typically siblings/alternatives rather than nested; consider using separate or comma-separated selectors so both cases are matched reliably.
- The `blockElements` Set in `findTranslatableParent` is recreated on every call; moving it to a module-level constant would avoid repeated allocations and make it easier to reuse or extend.
## Individual Comments
### Comment 1
<location> `entrypoints/main/dom.ts:359-361` </location>
<code_context>
+
+ let current = node;
+ // 这些是应该作为翻译单元的块级元素
+ const blockElements = new Set([
+ 'p', 'li', 'div', 'section', 'article', 'blockquote',
+ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd'
+ ]);
+
</code_context>
<issue_to_address>
**suggestion (performance):** Consider moving the `blockElements` Set to a shared constant to avoid re-allocating it on every call.
Because this set is constant, repeatedly creating it in `findTranslatableParent` adds unnecessary overhead if the function is called often. Defining it once at module scope avoids the repeated allocation and makes it available for reuse elsewhere.
Suggested implementation:
```typescript
const BLOCK_LEVEL_TRANSLATION_ELEMENTS = new Set([
'p',
'li',
'div',
'section',
'article',
'blockquote',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'dd',
]);
function findTranslatableParent(node: any): any {
```
```typescript
let current = node;
// 这些是应该作为翻译单元的块级元素
```
To fully apply this refactor, update all usages of the old local `blockElements` variable within `findTranslatableParent` (and anywhere else in this module) to use the new shared constant instead, for example:
- Replace `blockElements.has(tag)` with `BLOCK_LEVEL_TRANSLATION_ELEMENTS.has(tag)`.
Search for `blockElements` in `entrypoints/main/dom.ts` and swap each occurrence accordingly.
</issue_to_address>
### Comment 2
<location> `entrypoints/main/dom.ts:352-357` </location>
<code_context>
function findTranslatableParent(node: any): any {
// 1. 递归调用 grabNode 查找父节点是否可翻译
// 2. 若父节点不可翻译,则返回当前节点
- const parentResult = grabNode(node.parentNode);
- return parentResult || node;
+ // 3. 但要在块级元素处停止,不要无限向上查找
+
+ let current = node;
</code_context>
<issue_to_address>
**suggestion:** The inline steps in the comment still reference the old `grabNode` behavior and may now be misleading.
These steps still describe the old recursive `grabNode` behavior with a fallback to the current node, but the code now simply walks up to the nearest block-level ancestor. Please update the comment to match the new traversal logic or simplify it to a brief, accurate description.
```suggestion
function findTranslatableParent(node: any): any {
// 从当前节点开始向上查找,返回最近的块级父元素;若不存在块级父元素,则使用原始节点作为翻译单元
let current = node;
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
修复:GitHub Pull Requests 和 Issue 段落级而非容器级翻译粒度问题
问题描述
在 GitHub 的 PR 或 Issue 评论中悬停翻译时,整个评论容器会被当作一个翻译单元合并在一起,而不是让各个段落单独翻译。
现象
div.comment-body容器被翻译成一个 span效果对比
修复前(整个容器被当成一个单元):
修复后(各段落独立翻译):
根本原因
entrypoints/main/compat.ts中 GitHub 的适配代码把整个div.comment-body硬性返回作为翻译单元:这样就阻止了通用的节点选择逻辑去处理
<p>、<li>这些单个块元素,导致翻译范围过大。解决方案
修改 1:
entrypoints/main/compat.ts删除对
div.comment-body的强制返回,让通用逻辑去处理各个段落:效果:
修改 2:
entrypoints/main/dom.ts优化
findTranslatableParent()函数,加入明确的块元素边界检查:优点:
测试情况
GitHub 测试
✅ PR 评论页面:各段落独立翻译
✅ Issue 评论页面:列表项和段落都能正确处理
✅ 内联代码注释:没有问题
✅ 多层嵌套段落:各自翻译
✅ 混合内容(文本+代码):粒度合适
其他网站
✅ Twitter/X:没问题(有自己的适配)
✅ Reddit:没问题(有自己的适配)
✅ Medium:没问题(有自己的适配)
✅ 普通网站:翻译粒度更好
✅ Stack Overflow:没问题(有自己的适配)
修改统计
兼容性
✅ 完全向后兼容 - 没有破坏性改动
✅ 所有现有网站适配都能正常工作
✅ 用户体验改进,没有负面影响
✅ 可以安全发布
为什么这个修复重要
核实清单
个人想说的话
我用流畅阅读快一年了,真的希望这个插件能越来越好用,能帮助更多人。这个修复是我能为这个项目做的一点贡献。感谢开发者们的坚持!
感谢 @Bistutu 维护这么好用的插件!
Summary by Sourcery
Adjust translation node selection to respect block-level boundaries and improve paragraph-level translation behavior on GitHub comment content.
Bug Fixes:
Enhancements: