Repository navigation
fix(media): resolve React VDOM crashes, lightbox leaks, Mermaid SVG ID collisions, and i18n caption sync #692
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
Closed
bhuvan-somisetty
wants to merge
3
commits into
Project-HAMi:master
from
bhuvan-somisetty:fix/media-subsystem-vdom-lightbox
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e0d4eee
fix(media): resolve React VDOM crashes, lightbox leaks, Mermaid SVG I…
bhuvan-somisetty fcbd47a
fix(media): refine logo detection heuristics, lightbox consumer lifec…
bhuvan-somisetty 15d302d
fix(media): refactor lightbox to a declarative React Portal with focu…
bhuvan-somisetty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,105 +1,8 @@ | ||
| /** | ||
| * Client module to automatically add figure numbers and captions to images | ||
| * based on their alt text in blog posts and docs. | ||
| * Client module hook for route updates. | ||
| * Figure numbering and captions are now handled natively via React MDX component | ||
| * (@theme/MDXComponents/Img) and CSS counters, avoiding raw VDOM DOM mutations. | ||
| */ | ||
| export function onRouteDidUpdate({ location, previousLocation }) { | ||
| // Only run on blog and doc pages | ||
| if (!location.pathname.match(/\/blog\//) && !location.pathname.match(/\/docs\//)) { | ||
| return; | ||
| } | ||
|
|
||
| // Wait for DOM to be ready with multiple attempts | ||
| setTimeout(() => addFigureNumbers(), 100); | ||
| setTimeout(() => addFigureNumbers(), 500); | ||
| setTimeout(() => addFigureNumbers(), 1000); | ||
| } | ||
|
|
||
| function addFigureNumbers() { | ||
| // Find all images in markdown content | ||
| const articleContent = document.querySelector("article"); | ||
| if (!articleContent) { | ||
| return; | ||
| } | ||
|
|
||
| // Find all images that are not in header or footer | ||
| const images = Array.from(articleContent.querySelectorAll("img")).filter((img) => { | ||
| // Filter out logos, avatars, icons | ||
| const parentClass = img.parentElement?.className || ""; | ||
| const isLogo = parentClass.includes("logo") || img.alt.includes("logo"); | ||
| const isIcon = img.width < 100 || img.height < 100; | ||
| return !isLogo && !isIcon; | ||
| }); | ||
|
|
||
| let figureCount = 0; | ||
|
|
||
| images.forEach((img) => { | ||
| // Get alt text | ||
| const altText = img.getAttribute("alt") || ""; | ||
| if (!altText.trim()) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if already has figcaption with figure number | ||
| const existingFigure = img.closest("figure"); | ||
| if (existingFigure) { | ||
| const existingFigcaption = existingFigure.querySelector("figcaption"); | ||
| if (existingFigcaption && existingFigcaption.textContent.match(/^(图|Figure)\d+:/)) { | ||
| figureCount++; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Increment counter | ||
| figureCount++; | ||
|
|
||
| // Create figure wrapper if it doesn't exist | ||
| let figure = img.closest("figure"); | ||
| if (!figure) { | ||
| figure = document.createElement("figure"); | ||
| img.parentNode.insertBefore(figure, img); | ||
| figure.appendChild(img); | ||
| } | ||
|
|
||
| // Remove existing figcaption if any (but only if it doesn't have figure number) | ||
| const existingFigcaption = figure.querySelector("figcaption"); | ||
| if (existingFigcaption && !existingFigcaption.textContent.match(/^(图|Figure)\d+:/)) { | ||
| existingFigcaption.remove(); | ||
| } else if (existingFigcaption) { | ||
| // Update existing figcaption with figure number | ||
| const currentLang = document.documentElement.lang || "en"; | ||
| const prefix = currentLang.startsWith("zh") ? "图" : "Figure"; | ||
| existingFigcaption.textContent = `${prefix}${figureCount}: ${altText}`; | ||
| return; | ||
| } | ||
|
|
||
| // Create figcaption with number and alt text | ||
| const figcaption = document.createElement("figcaption"); | ||
| const currentLang = document.documentElement.lang || "en"; | ||
|
|
||
| // Use appropriate prefix based on language | ||
| const prefix = currentLang.startsWith("zh") ? "图" : "Figure"; | ||
| figcaption.textContent = `${prefix}${figureCount}: ${altText}`; | ||
|
|
||
| // Append figcaption to figure | ||
| figure.appendChild(figcaption); | ||
|
|
||
| // Add styling class | ||
| figure.style.cssText = ` | ||
| margin: 2em 0; | ||
| text-align: center; | ||
| `; | ||
|
|
||
| img.style.cssText = ` | ||
| max-width: 100%; | ||
| height: auto; | ||
| border-radius: 8px; | ||
| `; | ||
|
|
||
| figcaption.style.cssText = ` | ||
| margin-top: 0.8em; | ||
| font-size: 0.9em; | ||
| color: var(--ifm-color-emphasis-600); | ||
| font-style: italic; | ||
| `; | ||
| }); | ||
| export function onRouteDidUpdate() { | ||
| // No imperative DOM mutation needed | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from "react"; | ||
|
|
||
| export default function MDXImg(props) { | ||
| const { alt = "", className = "" } = props; | ||
| const trimmedAlt = alt.trim(); | ||
|
|
||
| const isLogo = | ||
| className.includes("logo") || | ||
| className.includes("avatar") || | ||
| trimmedAlt.toLowerCase().includes("logo"); | ||
|
|
||
| if (!trimmedAlt || isLogo) { | ||
| return <img {...props} />; | ||
| } | ||
|
|
||
| return ( | ||
| <figure className="hami-doc-figure"> | ||
| <img {...props} /> | ||
| <figcaption>{trimmedAlt}</figcaption> | ||
| </figure> | ||
| ); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import MDXComponents from "@theme-original/MDXComponents"; | ||
| import MDXImg from "@site/src/theme/MDXComponents/Img"; | ||
|
|
||
| export default { | ||
| ...MDXComponents, | ||
| img: MDXImg, | ||
| }; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.