You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by "Astro-Han (via GitHub)" <gi...@apache.org> on 2026/10/14 05:30:51 UTC
Re: [PR] fix(ui): keep escaped brackets in link labels out of display math [maka]
Astro-Han commented on code in PR #5311:
URL: https://github.com/apache/maka/pull/5311#discussion_r4267125108
##########
packages/ui/src/markdown-math.tsx:
##########
@@ -154,16 +265,104 @@ function translateMarkdownMath(
continue;
}
+ const link = readMarkdownLink(source, index, leadingBackslashes);
+ if (link?.kind === 'pending') {
+ // Remember the scan only when everything before its opener already
+ // settled: an earlier unresolved backtick or math opener must be
+ // reparsed together with later chunks, and resuming just the label
+ // would skip it forever.
+ if (link.labelState !== undefined && safeSourceEnd === index) {
+ onLabelPending?.(link.labelState);
+ }
+ text += source.slice(index, link.end);
+ index = link.end;
+ atLineStart = false;
+ if (isFinalSegment) {
+ markSafe();
+ } else {
+ canMarkSafe = false;
+ }
+ continue;
+ }
+ if (link?.kind === 'match') {
+ // A bare closed label with nothing after it can still grow an inline
+ // or reference tail, so it must not settle: resuming after it would
+ // scan that tail without the label context. Once any byte follows the
+ // label the link question is decided and settling is safe again.
+ const mayGrowTail = link.end === link.labelEnd + 1 && link.end >= source.length;
+ // Display math renders as a block, which cannot live inside an inline
+ // link label, and Markdown reads `\[` / `\]` there as literal escaped
+ // brackets. Re-run every closed label with display math disabled so
+ // escaped brackets survive for Markdown to unescape; inline math still
+ // renders inside. Astryx decides later whether the label is an inline
+ // link, reference use, shortcut, image, or definition, so all forms
+ // must share this representation. Image alt text included:
+ // Astryx keeps alt as a raw string, and the image component restores
+ // literal tokens, so no private-use characters reach the DOM.
+ const translatedLabel = translateMarkdownMath(source.slice(link.labelStart, link.labelEnd), {
+ startsAtLineStart: false,
+ priorTextChar: source[link.labelStart - 1],
+ allowDisplayMath: false,
+ protectEscapedBrackets: true,
+ isFinalSegment: true,
+ });
+ // The explicit identifier of a full reference must go through the same
+ // transform, or use-site and definition IDs diverge and the link breaks.
+ let translatedRefText = '';
+ let refSafe = true;
+ if (link.refLabelStart !== undefined && link.refLabelEnd !== undefined) {
+ const translatedRef = translateMarkdownMath(
+ source.slice(link.refLabelStart, link.refLabelEnd),
+ {
+ startsAtLineStart: false,
+ priorTextChar: source[link.refLabelStart - 1],
+ allowDisplayMath: false,
+ protectEscapedBrackets: true,
+ isFinalSegment: true,
+ },
+ );
+ translatedRefText = translatedRef.text;
+ refSafe = translatedRef.safeSourceEnd >= link.refLabelEnd - link.refLabelStart;
+ }
+ const refStart = link.refLabelStart ?? link.end;
+ const refEnd = link.refLabelEnd ?? link.end;
+ text += source.slice(index, link.labelStart)
+ + translatedLabel.text
+ + source.slice(link.labelEnd, refStart)
+ + translatedRefText
+ + source.slice(refEnd, link.end);
+ index = link.end;
+ atLineStart = source[index - 1] === '\n';
+ const labelSafe = translatedLabel.safeSourceEnd >= link.labelEnd - link.labelStart;
+ if (labelSafe && refSafe && !mayGrowTail) {
+ markSafe();
+ } else {
+ canMarkSafe = false;
+ }
+ continue;
+ }
+
// Destinations take `$` verbatim: escaping there would reach the href,
// where `\$` does not round-trip back to `$`.
const destination = readLinkDestination(source, index);
- if (destination !== undefined) {
- text += source.slice(index, destination);
- index = destination;
+ if (destination.kind === 'matched') {
+ text += source.slice(index, destination.end);
+ index = destination.end;
atLineStart = false;
markSafe();
continue;
}
+ if (destination.kind === 'pending') {
+ text += source.slice(index);
Review Comment:
[P2] Preserve prose math behavior in an incomplete inline tail
This raw-suffix branch also runs for a stray `](` with no closing `)`, even in a completed message. Through production MarkdownBody, `prefix ](literal $x$ text` now renders x as KaTeX rather than retaining literal dollars, while `prefix ](literal \(x+1\) text` loses the explicit formula and renders plain (x+1). Both additional assertions fail here and pass with the exact e81c1fa4 math module on current dependencies. Keep the boundary unsettled for a future chunk without skipping normal prose translation until a destination actually resolves; cover both completed-message cases while retaining the chunk-boundary fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org