translation: skip translating pre, code and math tags, closes #1693 (#1698)

This commit is contained in:
Huang Xin
2025-07-28 15:18:19 +08:00
committed by GitHub
parent 23fa2dc8b2
commit b9add62ba7
3 changed files with 11 additions and 7 deletions
@@ -54,7 +54,7 @@ export function useTextTranslation(
if (!view || !enabled.current) return;
const observer = createTranslationObserver();
observerRef.current = observer;
const nodes = walkTextNodes(view);
const nodes = walkTextNodes(view, ['pre', 'code', 'math']);
console.log('Observing text nodes for translation:', nodes.length);
allTextNodes.current = nodes;
nodes.forEach((el) => observer.observe(el));
+4 -4
View File
@@ -337,8 +337,8 @@ foliate-view {
.book-spine {
background-image:
linear-gradient(180deg,
hsla(0, 0%, 90%, 0.2) 0%, /* Top highlight */
hsla(0, 0%, 85%, 0.15) 0.5%, /* Top highlight */
hsla(0, 0%, 95%, 0.2) 0%, /* Top highlight */
hsla(0, 0%, 90%, 0.1) 1%, /* Top highlight */
transparent 2%, /* Main book surface */
transparent 50%, /* Main book surface */
hsla(0, 0%, 25%, 0.15) 95%, /* Bottom shadow */
@@ -348,7 +348,7 @@ foliate-view {
hsla(0, 0%, 0%, 0.1) 0%, /* Left binding shadow */
hsla(0, 0%, 20%, 0.2) 0.5%, /* Dark transition */
hsla(0, 0%, 95%, 0.4) 0.8%, /* Peak highlight */
hsla(0, 0%, 90%, 0.4) 1.8%, /* Peak highlight */
hsla(0, 0%, 85%, 0.4) 2.0%, /* Peak highlight */
hsla(0, 0%, 60%, 0.3) 2.8%, /* Gradient highlight */
hsla(0, 0%, 40%, 0.2) 4.2%, /* Shadow between highlights */
hsla(0, 0%, 70%, 0.15) 4.6%, /* Second peak highlight */
@@ -362,6 +362,6 @@ foliate-view {
mask: linear-gradient(180deg,
black 0%,
rgba(0,0,0,0.9) 50%,
rgba(0,0,0,0.7) 100%
rgba(0,0,0,0.5) 100%
);
}
+6 -2
View File
@@ -1,4 +1,4 @@
export const walkTextNodes = (root: HTMLElement): HTMLElement[] => {
export const walkTextNodes = (root: HTMLElement, rejectTags: string[] = []): HTMLElement[] => {
const elements: HTMLElement[] = [];
const walk = (node: HTMLElement | Document | ShadowRoot, depth = 0) => {
if (depth > 15) return;
@@ -7,7 +7,11 @@ export const walkTextNodes = (root: HTMLElement): HTMLElement[] => {
}
const children = 'children' in node ? (Array.from(node.children) as HTMLElement[]) : [];
for (const child of children) {
if (child.tagName === 'STYLE' || child.tagName === 'LINK') {
if (
child.tagName === 'STYLE' ||
child.tagName === 'LINK' ||
rejectTags.includes(child.tagName.toLowerCase())
) {
continue;
}
if (child.shadowRoot) {