mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* feat(mobile): render math in Markdown with desktop chat's delimiter rules Move the price, citation, closer and display rules plus the KaTeX settings to src/shared/markdown-math-rules and point desktop chat at them. Mobile's markdown-it gets the same grammar, and pages typeset formulas with KaTeX, whose fonts ship as page assets. Mermaid's engine leaves katex external so both share one katex chunk. * build(mobile): count KaTeX fonts in the page budget and keep math to one chunk Only the licensed terminal font keeps its own allowance; KaTeX's fonts count toward the budget like code. The typesetter imports katex itself, sharing mermaid's chunk, so math adds one chunk instead of four. * fix(mobile): draw the PR line-count table's +/− glyphs at GitHub's size github.com draws \Huge at \Large (1.44em), so the line-count table's glyphs sit inline at text height. Cap the larger size commands the same way, and let inline math take its height from KaTeX's struts so rows keep normal height and numbers stay on the text baseline. * test(mobile): pin math delimiters to desktop chat's results * refactor(mobile): drop type assertions from the math tree nodes * chore(mobile): register the math mdast nodes beside <details> * fix(mobile): read bracket math only in chat, keep math parsing linear GitHub text (PRs, Tasks, files) reads $ and $$ only, so \[Bug\] stays a bracket; chat rows opt into desktop chat's \( and \[ too. Remember failed block scans and skip dollar-run tails so hostile input stays linear, leave unfinished math out of remend like desktop, bound the formula cache by bytes, retry a failed KaTeX load, and count symbol commands in table widths. * fix(mobile): copy formulas as TeX and keep GitHub's unclosed $$ as text Share desktop's selection-to-TeX reader so a copied selection carries each formula once, as its source. An unclosed $$ in GitHub text stays a paragraph, chat's math delimiters reach HTML-derived Markdown and summaries, failed block scans only skip openers inside the lines they read, unfinished math anywhere in the streaming tail skips remend, and table widths no longer spread every command into Math.max. * refactor(mobile): share the formula copy handler and cap sizes only in GitHub text Desktop and mobile copy through one shared handler. GitHub surfaces draw \Huge as github.com does while chat keeps desktop chat's sizes, PR and file text skip bracket scanning entirely, and the dollar-run scan and token alias live in one place. * chore(mobile): stop tracking the generated KaTeX stylesheet #38611 committed it by mistake; postinstall writes it and .gitignore now lists it. * fix(mobile): follow desktop chat's new dollar rule through the shared module #38651 changed desktop chat's inline $ rule (shell variables and prices stay text, a single $ can span lines, $5+2$ is math). Move those checks into src/shared/markdown-math-rules, point desktop at them, and give mobile's markdown-it rule the same behaviour.
98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { memo, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
import { ScrollView, StyleSheet, Text, type StyleProp, type TextStyle } from 'react-native'
|
|
import {
|
|
loadMobileMathTypesetter,
|
|
type MobileMathTypesetter
|
|
} from './mobile-markdown-math-typesetter'
|
|
|
|
let loaded: MobileMathTypesetter | null = null
|
|
let loading: Promise<MobileMathTypesetter | null> | null = null
|
|
|
|
function useMathTypesetter(): MobileMathTypesetter | null {
|
|
const [typesetter, setTypesetter] = useState<MobileMathTypesetter | null>(() => loaded)
|
|
useEffect(() => {
|
|
if (typesetter) {
|
|
return
|
|
}
|
|
let disposed = false
|
|
// A failed chunk load leaves the source showing; the next formula to mount tries again.
|
|
loading ??= loadMobileMathTypesetter().then(
|
|
(ready) => (loaded = ready),
|
|
() => {
|
|
loading = null
|
|
return null
|
|
}
|
|
)
|
|
void loading.then((ready) => {
|
|
if (!disposed) {
|
|
setTypesetter(() => ready)
|
|
}
|
|
})
|
|
return () => {
|
|
disposed = true
|
|
}
|
|
}, [typesetter])
|
|
return typesetter
|
|
}
|
|
|
|
type MobileMarkdownMathProps = {
|
|
value: string
|
|
/** Shown until KaTeX loads, and instead of a formula KaTeX can't parse, as on desktop. */
|
|
source: string
|
|
display: boolean
|
|
/** GitHub text: large sizes drawn as github.com draws them (the PR line-count table). */
|
|
githubSizes: boolean
|
|
style?: StyleProp<TextStyle>
|
|
}
|
|
|
|
/** A formula typeset by KaTeX into the page's DOM; react-native-web renders Text as a span. */
|
|
export const MobileMarkdownMath = memo(function MobileMarkdownMath({
|
|
value,
|
|
source,
|
|
display,
|
|
githubSizes,
|
|
style
|
|
}: MobileMarkdownMathProps) {
|
|
const typesetter = useMathTypesetter()
|
|
const html = useMemo(
|
|
() => typesetter?.(value, display, githubSizes) ?? null,
|
|
[typesetter, value, display, githubSizes]
|
|
)
|
|
const hostRef = useRef<Text>(null)
|
|
|
|
useLayoutEffect(() => {
|
|
const host = hostRef.current
|
|
if (html === null || !(host instanceof HTMLElement)) {
|
|
return
|
|
}
|
|
host.innerHTML = html
|
|
// Copying a selection reads the formula back as this source (markdownMathSelectionText).
|
|
host.dataset.mathSource = source
|
|
return () => {
|
|
host.replaceChildren()
|
|
delete host.dataset.mathSource
|
|
}
|
|
}, [html, source])
|
|
|
|
if (html === null) {
|
|
return <Text style={style}>{source}</Text>
|
|
}
|
|
// React owns the empty host; KaTeX's markup inside it is written by the effect above.
|
|
const host = <Text key="typeset" ref={hostRef} style={style} />
|
|
return display ? (
|
|
<ScrollView
|
|
horizontal
|
|
showsHorizontalScrollIndicator={false}
|
|
contentContainerStyle={styles.display}
|
|
>
|
|
{host}
|
|
</ScrollView>
|
|
) : (
|
|
host
|
|
)
|
|
})
|
|
|
|
const styles = StyleSheet.create({
|
|
display: { flexGrow: 1, justifyContent: 'center' }
|
|
})
|