Skip to content

feat: render math in reader popup - #1425

Open
Golden-Pigeon wants to merge 7 commits into
windingwind:mainfrom
Golden-Pigeon:math-rendering-reader-popup
Open

feat: render math in reader popup#1425
Golden-Pigeon wants to merge 7 commits into
windingwind:mainfrom
Golden-Pigeon:math-rendering-reader-popup

Conversation

@Golden-Pigeon

@Golden-Pigeon Golden-Pigeon commented May 15, 2026

Copy link
Copy Markdown

Summary

This PR adds LaTeX formula rendering support to the Reader selection popup, addressing #1076 (comment).

Changes

  • Added KaTeX rendering for translated text shown in the Reader popup.
  • Supports existing LaTeX delimiters: $...$, $$...$$, \(...\), and \[...\].
  • Makes popup formula rendering stream-safe:
    • incomplete formulas are shown as plain escaped text while streaming
    • formulas render automatically once the closing delimiter arrives
    • invalid KaTeX content falls back to escaped source text
  • Reuses and throttles popup math overlay updates during streaming to reduce unnecessary re-rendering.
  • Keeps Reader popup width stable during streaming so the popup can grow but does not repeatedly shrink.

Known Issues

  • The Reader popup height may not fit the translated text height exactly.(fixed in latest commits)
image

Copilot AI review requested due to automatic review settings May 15, 2026 16:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds KaTeX-based LaTeX rendering to the Reader selection popup and introduces stream-safe/throttled overlay updates, aiming to render formulas once delimiters are complete and reduce UI jitter during streaming translation updates.

Changes:

  • Added stream-safe LaTeX detection + rendering helpers (with fallback to escaped source on KaTeX failures).
  • Reworked Reader popup UI to include a math-rendered overlay layered over the textarea and throttled overlay rendering via requestAnimationFrame.
  • Added per-task popup width tracking to reduce repeated shrinking during streaming updates.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/utils/mathRenderer.ts Adds helpers for determining whether math should render, overlay visibility state, and KaTeX rendering with fallback behavior.
src/modules/popup.ts Introduces the popup math overlay/container, throttled rendering, and task-scoped width tracking to stabilize streaming UI updates.
src/elements/mathTextbox.ts Refactors overlay lifecycle to reuse the overlay element and throttle rendering, using the new shouldRenderMath helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/modules/popup.ts
Comment thread src/modules/popup.ts
Comment on lines 23 to 60
const idPrefix = popup?.getAttribute(`${config.addonRef}-prefix`);
const makeId = (type: string) => `${idPrefix}-${type}`;
const audiobox = popup?.querySelector(
`#${makeId("audiobox")}`,
) as HTMLDivElement;
const translateButton = popup?.querySelector(
`#${makeId("translate")}`,
) as HTMLDivElement;
const textContainer = popup?.querySelector(
`#${makeId("text-container")}`,
) as HTMLDivElement;
const textarea = popup?.querySelector(
`#${makeId("text")}`,
) as HTMLTextAreaElement;
const mathOverlay = popup?.querySelector(
`#${makeId("math-overlay")}`,
) as HTMLDivElement;
const addToNoteButton = popup?.querySelector(
`#${makeId("addtonote")}`,
) as HTMLDivElement;

const updateHidden = (elem: HTMLElement, hidden: boolean) => {
if (hidden) {
elem.style.display = "none";
} else {
elem.style.removeProperty("display");
}
};

if (!enablePopup) {
cancelPopupMathOverlayRender(mathOverlay);
mathOverlay.innerHTML = "";
updateHidden(audiobox, true);
updateHidden(translateButton, true);
updateHidden(textContainer, true);
updateHidden(textarea, true);
updateHidden(mathOverlay, true);
updateHidden(addToNoteButton, true);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread src/modules/popup.ts
Comment on lines +526 to +533
function syncPopupTextContainer(
container: HTMLDivElement,
textarea: HTMLTextAreaElement,
): void {
container.hidden = textarea.hidden;
container.style.width = textarea.style.width;
container.style.height = textarea.style.height;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback. Please use ResizeObserver.

Comment thread src/modules/popup.ts
Comment on lines +9 to +10
const popupMathOverlayFrames = new WeakMap<HTMLDivElement, number>();
const popupTaskMaxWidths = new Map<string, number>();
@github-actions

Copy link
Copy Markdown
Contributor

@Golden-Pigeon
I automatically applied lint fixes but couldn’t push to your branch—could you please run:

npm run lint

and push the results? Thanks!

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

@Golden-Pigeon
I automatically applied lint fixes but couldn’t push to your branch—could you please run:

npm run lint

and push the results? Thanks!

@Golden-Pigeon
Golden-Pigeon force-pushed the math-rendering-reader-popup branch from c741e8b to 81787cc Compare May 16, 2026 08:47
@github-actions

Copy link
Copy Markdown
Contributor

@Golden-Pigeon
I automatically applied lint fixes but couldn’t push to your branch—could you please run:

npm run lint

and push the results? Thanks!

@Golden-Pigeon

Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

@Golden-Pigeon Golden-Pigeon changed the title Render LaTeX math in reader popup and stabilize streaming updates feat: render math in reader popup May 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@Golden-Pigeon
I automatically applied lint fixes but couldn’t push to your branch—could you please run:

npm run lint

and push the results? Thanks!

@Golden-Pigeon

Copy link
Copy Markdown
Author

Updated the Reader popup math rendering behavior to fix the height mismatch issue.

Changes included:

  • Removed the fixed maxHeight limit from the math overlay, so rendered content is no longer constrained to an internal scroll area.
  • Normalized line break handling around rendered math to avoid extra blank lines from repeated <br> elements.
  • Changed popup height calculation for math-rendered content to measure the rendered overlay HTML instead of relying only on the raw textarea
    scrollHeight.

With this, the popup still uses the textarea measurement for plain/raw text, but switches to rendered-content measurement when the math overlay
is displayed.

@windingwind

Copy link
Copy Markdown
Owner

thanks. this will be pended due to planned refactor of the plugin

@Golden-Pigeon
Golden-Pigeon force-pushed the math-rendering-reader-popup branch from 6760690 to 44f081e Compare June 10, 2026 13:36
@cislunarspace

Copy link
Copy Markdown
Contributor

This feature is good. I'm looking forward to this feature update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants