@opengeni/react-native 6.39.1-canary.45627434309001 → 6.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,87 @@
1
- export declare const MAX_MARKDOWN_LENGTH = 8192;
2
- export declare function prepareBoundedMarkdown(text: string): import("@opengeni/react/markdown-math").PreparedMathMarkdown | null;
1
+ import { type PreparedMathMarkdown } from "@opengeni/react/markdown-math";
2
+ /** The most text one Markdown parse sees. */
3
+ export declare const MAX_MARKDOWN_CHUNK_LENGTH = 8192;
4
+ /** The longest message drawn with formatting (about 10,000 words). */
5
+ export declare const MAX_MARKDOWN_LENGTH = 65536;
6
+ /** Math-normalized Markdown that fits one bounded parse, or null. */
7
+ export declare function prepareBoundedMarkdown(text: string): PreparedMathMarkdown | null;
8
+ export type MarkdownSegment =
9
+ /** At most one chunk of Markdown (a parse after math normalization, or plain pages). */
10
+ {
11
+ kind: "markdown";
12
+ source: string;
13
+ }
14
+ /** A top-level code fence, drawn without Markdown. `closed` is false while it streams. */
15
+ | {
16
+ kind: "fence";
17
+ source: string;
18
+ language: string;
19
+ content: string;
20
+ closed: boolean;
21
+ }
22
+ /** A block too long for one parse, shown as plain-text pages. */
23
+ | {
24
+ kind: "plain";
25
+ source: string;
26
+ };
27
+ /** The fields of a markdown-it block token the segmenter reads. */
28
+ type BlockToken = {
29
+ type: string;
30
+ level: number;
31
+ nesting: number;
32
+ map: [number, number] | null;
33
+ info: string;
34
+ content: string;
35
+ markup: string;
36
+ };
37
+ /** A markdown-it instance: the parser whose block structure the cuts follow. */
38
+ export type MarkdownBlockParser = {
39
+ block: {
40
+ parse(src: string, md: never, env: object, tokens: BlockToken[]): void;
41
+ };
42
+ };
43
+ type RulableParser = {
44
+ block: {
45
+ ruler: {
46
+ before: (name: string, rule: string, fn: never, options: {
47
+ alt: string[];
48
+ }) => void;
49
+ };
50
+ };
51
+ };
52
+ /**
53
+ * The segmenting parser's view of display math. A chunk with math is drawn from its
54
+ * math-normalized text, where each display block (the math pass's `displayMathRanges`)
55
+ * is a `$$` block that interrupts paragraphs and ends lazy lines (`markdownItMath`). This
56
+ * rule makes the same lines one block in the original text, so the cuts follow the block
57
+ * structure the renderer sees. Without `env.mathRangeEnds` it never matches.
58
+ */
59
+ export declare function markdownItMathRanges(md: RulableParser): void;
60
+ /**
61
+ * How to draw a message: one Markdown segment when it fits one parse, bounded segments
62
+ * when it is long, or null when it is too long to format at all (plain-text pages).
63
+ *
64
+ * Cuts follow markdown-it's own block structure: a long message is cut only where one of
65
+ * its top-level blocks starts (or, inside a list longer than one chunk, where one of its
66
+ * items starts), so every chunk parses to the same blocks a single parse would give, and
67
+ * a fence is a fence exactly when a single parse says so. Display math blocks, as the
68
+ * math pass finds them, are never cut. Linear in the message length.
69
+ *
70
+ * The segmenter keeps the block structure of the last message it saw: when the new text
71
+ * extends it (streaming), only the blocks from the end of the stable part are parsed again.
72
+ */
73
+ export declare function createMarkdownSegmenter(parser: MarkdownBlockParser): (text: string) => MarkdownSegment[] | null;
74
+ /** `createMarkdownSegmenter` for a single message. */
75
+ export declare function markdownSegments(text: string, parser: MarkdownBlockParser): MarkdownSegment[] | null;
76
+ /**
77
+ * How many leading segments to have mounted after one more frame, starting from
78
+ * `mounted`: at least one more, then more while they fit one frame's budget. The first
79
+ * frame takes up to one full chunk, the bound a single message had before chunking.
80
+ */
81
+ export declare function mountedSegmentCount(segments: readonly MarkdownSegment[], mounted: number): number;
3
82
  export declare function plainTextPage(text: string, requestedPage: number): {
4
83
  index: number;
5
84
  count: number;
6
85
  text: string;
7
86
  };
87
+ export {};
@@ -1,15 +1,15 @@
1
- import { withAlpha, useReleaseFollow, useNativeTimelineMessages, DecisionActions, DecisionButton } from '../chunk-MSNV24K4.js';
2
- import { fontStyle, MONO_FALLBACK, insetSurface, useNativeTimelineTheme, Icon } from '../chunk-XB56VWJW.js';
1
+ import { useReleaseFollow, useNativeTimelineMessages, withAlpha, DecisionActions, DecisionButton } from '../chunk-MSNV24K4.js';
2
+ import { useNativeTimelineTheme, fontStyle, insetSurface, Icon, MONO_FALLBACK } from '../chunk-XB56VWJW.js';
3
3
  import { __commonJS, __export, __esm, __toESM, __toCommonJS } from '../chunk-V4OQ3NZ2.js';
4
- import { createContext, useMemo, Fragment, useState, useContext, useRef, cloneElement } from 'react';
5
- import { Text, View, Pressable, ScrollView, Image, ActivityIndicator, Linking } from 'react-native';
4
+ import { createContext, memo, useMemo, useState, useContext, useRef, useEffect, Fragment as Fragment$1, cloneElement } from 'react';
5
+ import { View, Text, Pressable, ScrollView, ActivityIndicator, Linking, Image } from 'react-native';
6
6
  import Markdown, { MarkdownIt, renderRules } from 'react-native-markdown-display';
7
7
  import Svg, { SvgXml, Defs, LinearGradient, Stop, Rect } from 'react-native-svg';
8
8
  import { parseOpenGeniLink, isReservedOpenGeniLink, parseRetainedFileReference } from '@opengeni/sdk';
9
9
  import { interactivePreviewKind } from '@opengeni/react/native-previews';
10
- import { prepareMathMarkdown } from '@opengeni/react/markdown-math';
10
+ import { prepareMathMarkdown, displayMathRanges } from '@opengeni/react/markdown-math';
11
11
  import { useOpenGeniText } from '@opengeni/react/messages';
12
- import { jsx, jsxs, Fragment as Fragment$1 } from 'react/jsx-runtime';
12
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
13
13
  import { INTERACTIVE_ANSWER_FENCE, trailingOpenFence, settleInteractiveAnswer, parseInteractiveAnswer, interactiveAnswerKey, useInteractiveAnswerHost, useInteractiveAnswerState, visibleInteractiveItems, bestInteractiveItems, interactiveSelectionWasSent, interactiveFilterOptions, formatInteractiveValue, interactiveActionLabel, toggleInteractiveSelection, interactiveSelectionMessage } from '@opengeni/react/interactive-answer-model';
14
14
  export { trailingOpenFence } from '@opengeni/react/interactive-answer-model';
15
15
 
@@ -35669,13 +35669,173 @@ var init_math_engine = __esm({
35669
35669
  };
35670
35670
  }
35671
35671
  });
35672
- var MAX_MARKDOWN_LENGTH = 8192;
35672
+ var MAX_MARKDOWN_CHUNK_LENGTH = 8192;
35673
+ var MAX_MARKDOWN_LENGTH = 65536;
35674
+ var CHUNK_TARGET_LENGTH = MAX_MARKDOWN_CHUNK_LENGTH / 2;
35673
35675
  function prepareBoundedMarkdown(text) {
35674
- if (text.length > MAX_MARKDOWN_LENGTH) return null;
35676
+ if (text.length > MAX_MARKDOWN_CHUNK_LENGTH) return null;
35675
35677
  const prepared = prepareMathMarkdown(text);
35676
- return prepared.text.length <= MAX_MARKDOWN_LENGTH ? prepared : null;
35678
+ return prepared.text.length <= MAX_MARKDOWN_CHUNK_LENGTH ? prepared : null;
35677
35679
  }
35678
- var PAGE_LENGTH = MAX_MARKDOWN_LENGTH - 1;
35680
+ function linesOf(text) {
35681
+ const lines = [];
35682
+ const ending = /\r\n?|\n/g;
35683
+ let start = 0;
35684
+ while (start < text.length) {
35685
+ ending.lastIndex = start;
35686
+ const match = ending.exec(text);
35687
+ const stop = match ? match.index : text.length;
35688
+ const end = match ? stop + match[0].length : text.length;
35689
+ lines.push({ start, end, text: text.slice(start, stop) });
35690
+ start = end;
35691
+ }
35692
+ return lines;
35693
+ }
35694
+ var FENCE_CLOSE = /^ {0,3}(`{3,}|~{3,})[ \t]*$/;
35695
+ function unitsOf(parser, lines, mathEnds, from) {
35696
+ const tokens = [];
35697
+ const src = lines.slice(from).map((line) => line.text).join("\n").replaceAll("\0", "\uFFFD");
35698
+ const env = { mathRangeEnds: mathEnds, mathRangeOffset: from };
35699
+ parser.block.parse(src, parser, env, tokens);
35700
+ const units = [];
35701
+ let list = null;
35702
+ for (const token of tokens) {
35703
+ if (!token.map) continue;
35704
+ const line = from + token.map[0];
35705
+ if (token.level === 0 && token.nesting !== -1) {
35706
+ const isList = token.type === "bullet_list_open" || token.type === "ordered_list_open";
35707
+ const unit = { line, fence: null, items: isList ? [] : null };
35708
+ if (token.type === "fence") {
35709
+ const last = token.map[1] - token.map[0] >= 2 ? lines[from + token.map[1] - 1] : null;
35710
+ const closer = last ? FENCE_CLOSE.exec(last.text)?.[1] : void 0;
35711
+ unit.fence = {
35712
+ language: token.info.trim().split(/\s+/)[0] ?? "",
35713
+ content: token.content.endsWith("\n") ? token.content.slice(0, -1) : token.content,
35714
+ closed: !!closer && closer[0] === token.markup[0] && closer.length >= token.markup.length
35715
+ };
35716
+ }
35717
+ units.push(unit);
35718
+ list = isList ? unit : null;
35719
+ } else if (list && token.level === 1 && token.type === "list_item_open") {
35720
+ list.items.push(line);
35721
+ }
35722
+ }
35723
+ return units;
35724
+ }
35725
+ function markdownItMathRanges(md) {
35726
+ const rule = (state, startLine, endLine, silent) => {
35727
+ const offset = state.env.mathRangeOffset ?? 0;
35728
+ const end = (state.env.mathRangeEnds?.[startLine + offset] ?? -1) - offset;
35729
+ if (end < startLine || end >= endLine) return false;
35730
+ const first = state.src.slice(state.bMarks[startLine] + state.tShift[startLine], state.eMarks[startLine]).trim();
35731
+ if (!first.startsWith("$$") && !first.startsWith("\\[")) return false;
35732
+ if (silent) return true;
35733
+ state.push("math_range", "math", 0).map = [startLine, end + 1];
35734
+ state.line = end + 1;
35735
+ return true;
35736
+ };
35737
+ md.block.ruler.before("fence", "math_range", rule, {
35738
+ alt: ["paragraph", "reference", "blockquote", "list"]
35739
+ });
35740
+ }
35741
+ function createMarkdownSegmenter(parser) {
35742
+ let cached = null;
35743
+ return (text) => {
35744
+ if (text.length > MAX_MARKDOWN_LENGTH) return null;
35745
+ if (text.length <= MAX_MARKDOWN_CHUNK_LENGTH) return [{ kind: "markdown", source: text }];
35746
+ const lines = linesOf(text);
35747
+ const ranges = displayMathRanges(lines.map((line) => line.text));
35748
+ const mathEnds = new Int32Array(lines.length).fill(-1);
35749
+ const inMath = new Uint8Array(lines.length);
35750
+ for (const { start, end } of ranges) {
35751
+ mathEnds[start] = end;
35752
+ inMath.fill(1, start + 1, end + 1);
35753
+ }
35754
+ let keep = -1;
35755
+ if (cached && text.startsWith(cached.text)) {
35756
+ const last = cached.lineCount - 1;
35757
+ for (let index = cached.units.length - 1; index > 0; index--) {
35758
+ const line = cached.units[index].line;
35759
+ if (line + 2 <= last && lines[line - 1].text.trim() === "" && !inMath[line]) {
35760
+ keep = index;
35761
+ break;
35762
+ }
35763
+ }
35764
+ }
35765
+ const units = cached && keep > 0 ? [
35766
+ ...cached.units.slice(0, keep),
35767
+ ...unitsOf(parser, lines, mathEnds, cached.units[keep].line)
35768
+ ] : unitsOf(parser, lines, mathEnds, 0);
35769
+ cached = { text, lineCount: lines.length, units };
35770
+ return segmentsOf(text, lines, units, inMath);
35771
+ };
35772
+ }
35773
+ function segmentsOf(text, lines, units, inMath) {
35774
+ const offset = (line) => line < lines.length ? lines[line].start : text.length;
35775
+ const pieces = [];
35776
+ const add = (start, end, fence) => {
35777
+ const previous = pieces[pieces.length - 1];
35778
+ if (previous && inMath[start]) {
35779
+ previous.end = end;
35780
+ previous.fence = null;
35781
+ } else {
35782
+ pieces.push({ start, end, fence });
35783
+ }
35784
+ };
35785
+ units.forEach((unit, index) => {
35786
+ const start = index === 0 ? 0 : unit.line;
35787
+ const end = units[index + 1]?.line ?? lines.length;
35788
+ if (!unit.items || offset(end) - offset(start) <= CHUNK_TARGET_LENGTH) {
35789
+ add(start, end, unit.fence);
35790
+ return;
35791
+ }
35792
+ const starts = [start, ...unit.items.filter((line) => line > start)];
35793
+ starts.forEach((item, at) => add(item, starts[at + 1] ?? end, null));
35794
+ });
35795
+ if (pieces.length === 0) pieces.push({ start: 0, end: lines.length, fence: null });
35796
+ const segments = [];
35797
+ let chunk = null;
35798
+ const flush = () => {
35799
+ if (chunk) {
35800
+ const source = text.slice(chunk.start, chunk.end);
35801
+ segments.push({
35802
+ kind: source.length <= MAX_MARKDOWN_CHUNK_LENGTH ? "markdown" : "plain",
35803
+ source
35804
+ });
35805
+ }
35806
+ chunk = null;
35807
+ };
35808
+ for (const piece of pieces) {
35809
+ const start = offset(piece.start);
35810
+ const end = offset(piece.end);
35811
+ if (piece.fence && end - start > CHUNK_TARGET_LENGTH) {
35812
+ flush();
35813
+ segments.push({ kind: "fence", source: text.slice(start, end), ...piece.fence });
35814
+ } else if (chunk && end - chunk.start <= CHUNK_TARGET_LENGTH) {
35815
+ chunk.end = end;
35816
+ } else {
35817
+ flush();
35818
+ chunk = { start, end };
35819
+ }
35820
+ }
35821
+ flush();
35822
+ return segments;
35823
+ }
35824
+ var MOUNT_LENGTH_PER_FRAME = CHUNK_TARGET_LENGTH;
35825
+ var MOUNT_SEGMENTS_PER_FRAME = 8;
35826
+ function mountedSegmentCount(segments, mounted) {
35827
+ if (mounted >= segments.length) return segments.length;
35828
+ const budget = mounted === 0 ? MAX_MARKDOWN_CHUNK_LENGTH : MOUNT_LENGTH_PER_FRAME;
35829
+ let next = mounted;
35830
+ let length = 0;
35831
+ while (next < segments.length && next - mounted < MOUNT_SEGMENTS_PER_FRAME) {
35832
+ length += segments[next].source.length;
35833
+ if (next > mounted && length > budget) break;
35834
+ next++;
35835
+ }
35836
+ return next;
35837
+ }
35838
+ var PAGE_LENGTH = MAX_MARKDOWN_CHUNK_LENGTH - 1;
35679
35839
  function plainTextPage(text, requestedPage) {
35680
35840
  const count = Math.max(1, Math.ceil(text.length / PAGE_LENGTH));
35681
35841
  const index = Math.max(0, Math.min(requestedPage, count - 1));
@@ -36601,6 +36761,11 @@ var MATH_PARSER = (() => {
36601
36761
  parser.use(markdownItMath);
36602
36762
  return parser;
36603
36763
  })();
36764
+ var SEGMENT_PARSER = (() => {
36765
+ const parser = MarkdownIt({ typographer: false, linkify: true });
36766
+ parser.use(markdownItMathRanges);
36767
+ return parser;
36768
+ })();
36604
36769
  function webMarkdownStyles(theme, tone) {
36605
36770
  const c = theme.colors;
36606
36771
  const muted = tone === "muted";
@@ -36975,7 +37140,7 @@ function webRules(theme, tone, onCopy, media = {}) {
36975
37140
  const drawn = media.renderLink({ href, children, standalone });
36976
37141
  if (drawn !== void 0) {
36977
37142
  if (standalone && group) BLOCK_LINK_GROUPS.add(group);
36978
- return /* @__PURE__ */ jsx(Fragment, { children: drawn }, node.key);
37143
+ return /* @__PURE__ */ jsx(Fragment$1, { children: drawn }, node.key);
36979
37144
  }
36980
37145
  }
36981
37146
  const target = media.renderOpenGeniLink && typeof href === "string" && parseOpenGeniLink(href);
@@ -36985,7 +37150,7 @@ function webRules(theme, tone, onCopy, media = {}) {
36985
37150
  const drawn = media.renderOpenGeniLink({ target, children });
36986
37151
  if (drawn !== void 0) {
36987
37152
  BLOCK_LINK_GROUPS.add(group);
36988
- return /* @__PURE__ */ jsx(Fragment, { children: drawn }, node.key);
37153
+ return /* @__PURE__ */ jsx(Fragment$1, { children: drawn }, node.key);
36989
37154
  }
36990
37155
  }
36991
37156
  }
@@ -37038,29 +37203,7 @@ function webRules(theme, tone, onCopy, media = {}) {
37038
37203
  const content = raw.endsWith("\n") ? raw.slice(0, -1) : raw;
37039
37204
  const info = node.sourceInfo;
37040
37205
  const language = typeof info === "string" ? info.trim().split(/\s+/)[0] ?? "" : "";
37041
- if (language === INTERACTIVE_ANSWER_FENCE) {
37042
- return /* @__PURE__ */ jsx(
37043
- AnswerFence,
37044
- {
37045
- content,
37046
- renderSource: () => /* @__PURE__ */ jsx(CodeFence, { content, language, onCopy })
37047
- },
37048
- node.key
37049
- );
37050
- }
37051
- const kind = interactivePreviewKind(language);
37052
- if (kind && media.renderInteractiveBlock) {
37053
- return /* @__PURE__ */ jsx(
37054
- InteractiveFence,
37055
- {
37056
- kind,
37057
- content,
37058
- render: media.renderInteractiveBlock
37059
- },
37060
- node.key
37061
- );
37062
- }
37063
- return /* @__PURE__ */ jsx(CodeFence, { content, language, onCopy }, node.key);
37206
+ return fenceElement(node.key, content, language, onCopy, media);
37064
37207
  },
37065
37208
  image: (node) => {
37066
37209
  const attributes = node.attributes;
@@ -37100,6 +37243,31 @@ function webRules(theme, tone, onCopy, media = {}) {
37100
37243
  }
37101
37244
  };
37102
37245
  }
37246
+ function fenceElement(key, content, language, onCopy, media) {
37247
+ if (language === INTERACTIVE_ANSWER_FENCE) {
37248
+ return /* @__PURE__ */ jsx(
37249
+ AnswerFence,
37250
+ {
37251
+ content,
37252
+ renderSource: () => /* @__PURE__ */ jsx(CodeFence, { content, language, onCopy })
37253
+ },
37254
+ key
37255
+ );
37256
+ }
37257
+ const kind = interactivePreviewKind(language);
37258
+ if (kind && media.renderInteractiveBlock) {
37259
+ return /* @__PURE__ */ jsx(
37260
+ InteractiveFence,
37261
+ {
37262
+ kind,
37263
+ content,
37264
+ render: media.renderInteractiveBlock
37265
+ },
37266
+ key
37267
+ );
37268
+ }
37269
+ return /* @__PURE__ */ jsx(CodeFence, { content, language, onCopy }, key);
37270
+ }
37103
37271
  var BLOCK_LINK_GROUPS = /* @__PURE__ */ new WeakSet();
37104
37272
  var FenceStateContext = createContext({
37105
37273
  openTail: null,
@@ -37113,7 +37281,7 @@ function InteractiveFence({
37113
37281
  const { openTail, streaming } = useContext(FenceStateContext);
37114
37282
  const open = openTail !== null && openTail.trimEnd() === content.trimEnd();
37115
37283
  const state = !open ? "complete" : streaming ? "streaming" : "incomplete";
37116
- return /* @__PURE__ */ jsx(Fragment$1, { children: render({ kind, content: state === "complete" ? content : "", state }) });
37284
+ return /* @__PURE__ */ jsx(Fragment, { children: render({ kind, content: state === "complete" ? content : "", state }) });
37117
37285
  }
37118
37286
  function AnswerFence({
37119
37287
  content,
@@ -37161,6 +37329,85 @@ function MarkdownImage({ src, alt }) {
37161
37329
  }
37162
37330
  );
37163
37331
  }
37332
+ var MarkdownChunk = memo(
37333
+ function MarkdownChunk2({
37334
+ segment,
37335
+ last,
37336
+ streaming,
37337
+ rendering
37338
+ }) {
37339
+ const { styles, rules, onLinkPress, interactive } = rendering;
37340
+ const { source } = segment;
37341
+ const prepared = useMemo(() => prepareBoundedMarkdown(source), [source]);
37342
+ const openTail = useMemo(
37343
+ () => last && (interactive || source.includes(INTERACTIVE_ANSWER_FENCE)) ? trailingOpenFence(source) : null,
37344
+ [last, interactive, source]
37345
+ );
37346
+ const fenceState = useMemo(() => ({ openTail, streaming }), [openTail, streaming]);
37347
+ if (!prepared) {
37348
+ return /* @__PURE__ */ jsx(View, { style: { marginBottom: 10 }, children: /* @__PURE__ */ jsx(PlainTextMessage, { text: source, style: styles.body }) });
37349
+ }
37350
+ return /* @__PURE__ */ jsx(FenceStateContext.Provider, { value: fenceState, children: /* @__PURE__ */ jsx(
37351
+ Markdown,
37352
+ {
37353
+ markdownit: prepared.hasMath ? MATH_PARSER : PARSER,
37354
+ style: styles,
37355
+ rules,
37356
+ ...onLinkPress ? { onLinkPress } : {},
37357
+ children: prepared.hasMath ? prepared.text : source
37358
+ }
37359
+ ) });
37360
+ },
37361
+ // The segment object is rebuilt on every delta; its source determines it.
37362
+ (previous, next) => previous.segment.source === next.segment.source && previous.last === next.last && previous.streaming === next.streaming && previous.rendering === next.rendering
37363
+ );
37364
+ var DirectFence = memo(
37365
+ function DirectFence2({
37366
+ segment,
37367
+ streaming,
37368
+ onCopy,
37369
+ media,
37370
+ bodyStyle
37371
+ }) {
37372
+ const fenceState = useMemo(
37373
+ () => ({ openTail: segment.closed ? null : segment.content, streaming }),
37374
+ [segment.closed, segment.content, streaming]
37375
+ );
37376
+ const preview = !!interactivePreviewKind(segment.language) && !!media.renderInteractiveBlock;
37377
+ if (!preview && segment.content.length > MAX_MARKDOWN_CHUNK_LENGTH) {
37378
+ return /* @__PURE__ */ jsx(View, { style: { marginBottom: 10 }, children: /* @__PURE__ */ jsx(PlainTextMessage, { text: segment.source, style: bodyStyle }) });
37379
+ }
37380
+ return /* @__PURE__ */ jsx(FenceStateContext.Provider, { value: fenceState, children: fenceElement("fence", segment.content, segment.language, onCopy, media) });
37381
+ },
37382
+ // The segment object is rebuilt on every delta; its source determines it.
37383
+ (previous, next) => previous.segment.source === next.segment.source && previous.streaming === next.streaming && previous.onCopy === next.onCopy && previous.media === next.media && previous.bodyStyle === next.bodyStyle
37384
+ );
37385
+ function nextFrame(run) {
37386
+ if (typeof requestAnimationFrame === "function") {
37387
+ const handle2 = requestAnimationFrame(run);
37388
+ return () => cancelAnimationFrame(handle2);
37389
+ }
37390
+ const handle = setTimeout(run, 16);
37391
+ return () => clearTimeout(handle);
37392
+ }
37393
+ function useMountedSegments(segments) {
37394
+ const [mounted, setMounted] = useState(0);
37395
+ const latest = useRef(segments);
37396
+ useEffect(() => {
37397
+ latest.current = segments;
37398
+ });
37399
+ const count = segments?.length ?? 0;
37400
+ if (mounted > count) setMounted(count);
37401
+ const shown = segments ? Math.max(mounted, mountedSegmentCount(segments, 0)) : 0;
37402
+ useEffect(() => {
37403
+ if (shown >= count) return void 0;
37404
+ return nextFrame(() => {
37405
+ const current = latest.current;
37406
+ if (current) setMounted(mountedSegmentCount(current, Math.min(shown, current.length)));
37407
+ });
37408
+ }, [count, shown]);
37409
+ return Math.min(shown, count);
37410
+ }
37164
37411
  function TrimmedMarkdown({
37165
37412
  text,
37166
37413
  tone,
@@ -37171,28 +37418,51 @@ function TrimmedMarkdown({
37171
37418
  media
37172
37419
  }) {
37173
37420
  const theme = useNativeTimelineTheme();
37174
- const math = useMemo(() => prepareBoundedMarkdown(text), [text]);
37421
+ const [segmenter] = useState(() => createMarkdownSegmenter(SEGMENT_PARSER));
37422
+ const segments = useMemo(() => segmenter(text), [segmenter, text]);
37423
+ const shown = useMountedSegments(segments);
37175
37424
  const styles = useMemo(() => {
37176
37425
  const base = webMarkdownStyles(theme, tone);
37177
37426
  return textStyle ? { ...base, body: { ...base.body, ...textStyle } } : base;
37178
37427
  }, [theme, tone, textStyle]);
37179
37428
  const rules = useMemo(() => webRules(theme, tone, onCopy, media), [theme, tone, onCopy, media]);
37180
- const openTail = useMemo(
37181
- () => math && (media.renderInteractiveBlock || text.includes(INTERACTIVE_ANSWER_FENCE)) ? trailingOpenFence(text) : null,
37182
- [math, media.renderInteractiveBlock, text]
37429
+ const rendering = useMemo(
37430
+ () => ({ styles, rules, onLinkPress, interactive: !!media.renderInteractiveBlock }),
37431
+ [styles, rules, onLinkPress, media.renderInteractiveBlock]
37183
37432
  );
37184
- const fenceState = useMemo(() => ({ openTail, streaming }), [openTail, streaming]);
37185
- if (!math) return /* @__PURE__ */ jsx(PlainTextMessage, { text, style: styles.body });
37186
- return /* @__PURE__ */ jsx(View, { style: { marginBottom: -10 }, children: /* @__PURE__ */ jsx(FenceStateContext.Provider, { value: fenceState, children: /* @__PURE__ */ jsx(
37187
- Markdown,
37188
- {
37189
- markdownit: math.hasMath ? MATH_PARSER : PARSER,
37190
- style: styles,
37191
- rules,
37192
- ...onLinkPress ? { onLinkPress } : {},
37193
- children: math.hasMath ? math.text : text
37433
+ if (!segments) return /* @__PURE__ */ jsx(PlainTextMessage, { text, style: styles.body });
37434
+ let offset = 0;
37435
+ return /* @__PURE__ */ jsx(View, { style: { marginBottom: -10 }, children: segments.slice(0, shown).map((segment, index) => {
37436
+ const last = index === segments.length - 1;
37437
+ const key = `${segment.kind}:${offset}`;
37438
+ offset += segment.source.length;
37439
+ if (segment.kind === "markdown") {
37440
+ return segment.source.trim() ? /* @__PURE__ */ jsx(
37441
+ MarkdownChunk,
37442
+ {
37443
+ segment,
37444
+ last,
37445
+ streaming: streaming && last,
37446
+ rendering
37447
+ },
37448
+ key
37449
+ ) : null;
37450
+ }
37451
+ if (segment.kind === "fence") {
37452
+ return /* @__PURE__ */ jsx(
37453
+ DirectFence,
37454
+ {
37455
+ segment,
37456
+ streaming: streaming && last,
37457
+ onCopy,
37458
+ media,
37459
+ bodyStyle: styles.body
37460
+ },
37461
+ key
37462
+ );
37194
37463
  }
37195
- ) }) });
37464
+ return /* @__PURE__ */ jsx(View, { style: { marginBottom: 10 }, children: /* @__PURE__ */ jsx(PlainTextMessage, { text: segment.source, style: styles.body }) }, key);
37465
+ }) });
37196
37466
  }
37197
37467
  function createWebMarkdownRenderer(options = {}) {
37198
37468
  const onLinkPress = (url) => {