import { decodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
import {
collectComposerContextReferences,
formatComposerContextHref,
formatComposerContextReference,
} from "@t3tools/shared/composerContextReferences";
import {
markdownForNativeRendering,
markdownWithFileEmbeds,
markdownForNativeParsing,
markdownWithInlineFileLinks,
markdownWithTableBreaks,
markdownWithTextDirection,
} from "../lib/markdownDocument";
import { useGT } from "gt-react-native";
import { isValidElement, useMemo, useState, type ReactElement, type ReactNode } from "react";
import { Platform, Share, StyleSheet, useWindowDimensions, View } from "react-native";
import {
EnrichedMarkdownText,
type AccessibilityLabels,
type MarkdownCodeBlock,
type MarkdownImageBlock,
type MarkdownLinkOccurrence,
type MarkdownStyle,
type TextSelectionMenuConfig,
} from "react-native-enriched-markdown";
import { codeFenceRenderer } from "./codeBlockMarkdown";
import { HighlightedCodeBlock } from "./HighlightedCodeBlock";
import { MermaidCodeBlock } from "./MermaidCodeBlock";
import type { SelectableMarkdownTextProps } from "./SelectableMarkdownText.types";
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
import { Menu } from "../components/ui/Menu";
import { useEnrichedLinkVariants } from "./useEnrichedLinkVariants";
import { useRetryingImageSource } from "./useRetryingImageSource";
import * as Dialog from "../components/ui/Dialog";
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
import type { HighlightScheme } from "../lib/highlights";
import {
ENRICHED_INLINE_FILE_LINK_REGEX,
enrichedTextLinkRegex,
enrichedModelDetails,
enrichedModelReference,
enrichedLinkVariantPattern,
} from "../lib/enrichedLinkPresentation";
function enrichedStyle(
style: SelectableMarkdownTextProps["textStyle"],
dark: boolean,
fontScale: number,
): MarkdownStyle {
const monospace = Platform.OS === "ios" ? "Menlo" : "monospace";
// Android only bolds fallback glyphs when the bold family is asked for at a bold weight (app.config.ts).
const boldFontWeight = Platform.OS === "android" ? "bold" : undefined;
// Keep the radius modest: iOS clips square table-cell borders without stroking the corner arcs.
const block = {
marginTop: 4,
marginBottom: 12,
borderRadius: 8,
borderWidth: StyleSheet.hairlineWidth,
borderColor: style.dividerColor,
};
const body = {
color: style.color,
fontFamily: style.fontFamily,
fontSize: style.fontSize,
lineHeight: style.lineHeight,
marginTop: 0,
marginBottom: 8,
};
const heading = (level: number) => {
const fontSize = style.headingFontSizes?.[level - 1] ?? style.fontSize * (1.6 - level * 0.1);
return {
...body,
color: style.strongColor,
fontFamily: style.headingFontFamily,
fontWeight: boldFontWeight,
fontSize,
lineHeight: fontSize * 1.35,
marginTop: 8,
};
};
return {
paragraph: body,
math: {
fontSize: style.fontSize,
color: style.color,
backgroundColor: "transparent",
padding: 8,
marginTop: 4,
marginBottom: 12,
},
inlineMath: { color: style.color },
h1: heading(1),
h2: heading(2),
h3: heading(3),
h4: heading(4),
h5: heading(5),
h6: heading(6),
strong: {
color: style.strongColor,
fontFamily: style.boldFontFamily,
fontWeight: boldFontWeight ?? "normal",
},
link: { color: style.linkColor, underline: false },
code: {
fontFamily: monospace,
color: style.inlineCodeColor,
backgroundColor: "transparent",
borderColor: "transparent",
fontSize: style.fontSize * 0.9,
},
codeBlock: {
...body,
color: style.codeColor,
backgroundColor: style.codeBlockBackgroundColor,
fontFamily: monospace,
fontSize: style.fontSize * 0.9,
...block,
padding: 12,
syntaxColors: dark
? {
keyword: "#ff7b72",
string: "#a5d6ff",
number: "#79c0ff",
constant: "#79c0ff",
comment: "#8b949e",
function: "#d2a8ff",
type: "#ffa657",
property: "#79c0ff",
tag: "#7ee787",
attribute: "#79c0ff",
}
: {
keyword: "#cf222e",
string: "#0a3069",
number: "#0550ae",
constant: "#0550ae",
comment: "#6e7781",
function: "#8250df",
type: "#953800",
property: "#0550ae",
tag: "#116329",
attribute: "#0550ae",
},
},
blockquote: {
...body,
color: style.mutedColor,
backgroundColor: "transparent",
borderColor: style.quoteMarkerColor,
borderWidth: 2,
gapWidth: 12,
},
list: {
...body,
bulletColor: style.mutedColor,
markerColor: style.mutedColor,
// The bullet is centred on the gap's edge and scales with the system text size; the gap must too.
gapWidth: 12 * fontScale,
},
thematicBreak: { color: style.dividerColor, height: 1, marginTop: 8, marginBottom: 8 },
table: {
...body,
...block,
headerFontFamily: style.boldFontFamily,
headerTextColor: style.strongColor,
headerBackgroundColor: style.codeBlockBackgroundColor,
rowEvenBackgroundColor: "transparent",
rowOddBackgroundColor: "transparent",
cellPaddingHorizontal: 10,
cellPaddingVertical: 7,
},
taskList: {
checkedColor: style.linkColor,
checkedTextColor: style.color,
borderColor: style.mutedColor,
checkedStrikethrough: false,
},
};
}
/**
* Diagram and math fences, as web draws them, and code enriched-markdown cannot colour.
* Diagrams and math stay native until the library reports them complete; fallback code keeps one block throughout.
*/
function codeBlockElement(
block: MarkdownCodeBlock,
textStyle: SelectableMarkdownTextProps["textStyle"],
markdownStyle: MarkdownStyle,
mathRenderingEnabled: boolean,
scheme: HighlightScheme,
accessibilityLabels: AccessibilityLabels,
selectionMenuConfig: TextSelectionMenuConfig,
): ReactElement | undefined {
const renderer = codeFenceRenderer(block.language, mathRenderingEnabled);
if (renderer.kind === "mermaid") {
return block.isComplete ? (
) : undefined;
}
if (renderer.kind === "math") {
return block.isComplete ? (
) : undefined;
}
if (renderer.kind === "highlight" && markdownStyle.codeBlock) {
return (
);
}
return undefined;
}
export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
const { renderImage, resolveImageSource, fileEmbedLabel } = props;
const gt = useGT();
const { themeAppearance, mathRenderingEnabled } = useAppearancePreferences();
// The library fills in {n}, {content} and {speech} itself when VoiceOver or TalkBack speaks.
const accessibilityLabels = useMemo(
() => ({
list: {
bulletPoint: gt("Bullet point"),
nestedBulletPoint: gt("Nested bullet point"),
orderedItem: gt("List item {n}", { n: "{n}" }),
nestedOrderedItem: gt("Nested list item {n}", { n: "{n}" }),
},
blockquote: { quote: gt("Blockquote"), nestedQuote: gt("Nested blockquote") },
table: { row: gt("Row {n}: {content}", { n: "{n}", content: "{content}" }) },
math: { equation: gt("Math: {speech}", { speech: "{speech}" }) },
codeBlock: { wrap: gt("Wrap lines"), unwrap: gt("Disable line wrap") },
rotor: {
headings: gt("Headings", { $context: "screen reader rotor category" }),
links: gt("Links", { $context: "screen reader rotor category" }),
images: gt("Images", { $context: "screen reader rotor category" }),
},
}),
[gt],
);
// The library's built-in selection menu items default to English labels.
const selectionMenuConfig = useMemo(
() => ({
copy: { label: gt("Copy", { $context: "verb" }) },
copyAsMarkdown: { label: gt("Copy as Markdown") },
copyImageUrl: {
label: gt("Copy Image URL"),
pluralLabels: { other: gt("Copy {count} Image URLs", { count: "{count}" }) },
},
}),
[gt],
);
const { markdown, fileHrefs } = useMemo(() => {
if (!fileEmbedLabel) {
return {
markdown: markdownWithTextDirection(
markdownForNativeRendering(props.markdown, ENRICHED_INLINE_FILE_LINK_REGEX),
),
fileHrefs: new Set(),
};
}
const content = markdownWithInlineFileLinks(
markdownForNativeParsing(props.markdown),
ENRICHED_INLINE_FILE_LINK_REGEX,
);
const files = markdownWithFileEmbeds(content, fileEmbedLabel);
return {
...files,
markdown: markdownWithTextDirection(markdownWithTableBreaks(files.markdown)),
};
}, [props.markdown, fileEmbedLabel]);
const [parsedLinkUrls, setParsedLinkUrls] = useState>([]);
const onLinksChange = (links: MarkdownLinkOccurrence[]) => {
const urls = [...new Set(links.map((link) => link.url))];
setParsedLinkUrls((previous) =>
previous.length === urls.length && previous.every((url, index) => url === urls[index])
? previous
: urls,
);
};
const textLinkRegex = useMemo(
() => enrichedTextLinkRegex(props.skills, props.models),
[props.skills, props.models],
);
// Recognized text links are added as the text is drawn, so
// onLinksChange never reports them. Joined so a streaming tick keeps the value.
const textLinkKey = useMemo(
() =>
textLinkRegex
? [...new Set(markdown.match(new RegExp(textLinkRegex.source, "gu")) ?? [])].join("\n")
: "",
[markdown, textLinkRegex],
);
const linkUrls = textLinkKey
? [...new Set([...parsedLinkUrls, ...textLinkKey.split("\n")])]
: parsedLinkUrls;
const { linkVariants, linkPillContent } = useEnrichedLinkVariants(linkUrls, props, fileHrefs);
const linkRecognition = useMemo(() => ({ text: textLinkRegex ?? undefined }), [textLinkRegex]);
const [activeMenu, setActiveMenu] = useState<{
url: string;
menu: NonNullable>>;
} | null>(null);
const { fileContextMenu, onFileContextMenuAction } = props;
const onLinkPress = (url: string) => {
const model = enrichedModelReference(url, props.models);
if (model) {
Dialog.alert(model.label, enrichedModelDetails(model));
return;
}
(fileHrefs.has(url) ? (props.onImagePress ?? props.onLinkPress) : props.onLinkPress)?.(url);
};
const modelContextMenu = (url: string) => {
const model = enrichedModelReference(url, props.models);
return model
? {
title: model.label,
actions: [
{ id: "enriched-copy-link", title: gt("Copy model reference") },
{ id: "enriched-share-link", title: gt("Share model reference") },
],
}
: undefined;
};
const performMenuAction = (url: string, actionId: string) => {
if (actionId === "enriched-open-link") onLinkPress(url);
else if (actionId === "enriched-copy-link") copyTextWithHaptic(url);
else if (actionId === "enriched-share-link") void Share.share({ message: url });
else onFileContextMenuAction?.(url, actionId);
};
const linkContextMenuItems =
Platform.OS === "ios" && (fileContextMenu || props.onLinkPress || props.onImagePress)
? Object.fromEntries(
linkUrls.map((url) => {
const menu = modelContextMenu(url) ??
fileContextMenu?.(url) ?? {
title: url,
actions: [
{ id: "enriched-open-link", title: gt("Open link") },
{ id: "enriched-copy-link", title: gt("Copy link") },
{ id: "enriched-share-link", title: gt("Share link") },
],
};
return [
enrichedLinkVariantPattern(url),
menu.actions.map((action) => ({
text: action.title,
disabled: "disabled" in action && action.disabled,
onPress: () => performMenuAction(url, action.id),
})),
];
}),
)
: undefined;
const selectionClipboard = useMemo(() => {
const fragment = decodeComposerContextFragment(props.contextClipboardFragment);
if (!fragment) return undefined;
const references = collectComposerContextReferences(props.markdown);
const linkTextByUrl = Object.fromEntries(
references.flatMap((reference) => {
const record = fragment.records.find((entry) => entry.contextId === reference.contextId);
return record
? [
[
formatComposerContextHref(reference.kind, reference.contextId),
formatComposerContextReference({ ...reference, label: record.label }),
],
]
: [];
}),
);
return {
linkTextByUrl,
htmlAttributes: {
"data-t3-context-fragment": encodeURIComponent(props.contextClipboardFragment!),
},
mimeTypes: { "app.t3.context-fragment": props.contextClipboardFragment! },
};
}, [props.contextClipboardFragment, props.markdown]);
const { fontScale } = useWindowDimensions();
const baseMarkdownStyle = useMemo(() => {
const style = enrichedStyle(props.textStyle, themeAppearance === "dark", fontScale);
return {
...style,
paragraph: props.textAlign
? { ...style.paragraph, textAlign: props.textAlign }
: style.paragraph,
};
}, [props.textStyle, props.textAlign, themeAppearance, fontScale]);
// Link variants change as a message streams. The code renderer reads only the base style, so
// it stays the same function and the library keeps its rendered blocks.
const markdownStyle = useMemo(
() => ({ ...baseMarkdownStyle, linkVariants }),
[baseMarkdownStyle, linkVariants],
);
const renderers = {
image: renderImage
? (block: MarkdownImageBlock) => {
const element = renderImage({
href: block.url,
alt: block.altText || null,
title: block.title || null,
});
return isValidElement(element) ? element : undefined;
}
: undefined,
code: (block: MarkdownCodeBlock) =>
codeBlockElement(
block,
props.textStyle,
baseMarkdownStyle,
mathRenderingEnabled,
themeAppearance,
accessibilityLabels,
selectionMenuConfig,
),
};
const resolveNativeImageSource = useRetryingImageSource(resolveImageSource);
const renderMarkdown = (openAndroidMenu?: () => void) => {
const onLinkLongPress =
Platform.OS === "android" && props.fileContextMenu
? ({ url }: { url: string }) => {
const menu = modelContextMenu(url) ??
props.fileContextMenu?.(url) ?? {
title: url,
actions: [
{ id: "enriched-open-link", title: gt("Open link") },
{ id: "enriched-copy-link", title: gt("Copy link") },
],
};
setActiveMenu({ url, menu });
openAndroidMenu?.();
}
: undefined;
return (
(props.onImagePress ?? props.onLinkPress)?.(url)
: undefined
}
linkRecognition={linkRecognition}
containerStyle={{ flexShrink: 1, minWidth: 0 }}
flavor="github"
selectable
contextMenuItems={
props.onCiteText
? [
{
text: gt("Cite", { $context: "verb, quote selected text" }),
icon: "quote.bubble",
onPress: ({ text }) => props.onCiteText?.(text),
},
]
: undefined
}
selectionClipboard={selectionClipboard}
selectionMenuConfig={selectionMenuConfig}
selectionColor={props.textStyle.selectionColor}
selectionHandleColor={props.textStyle.selectionHandleColor}
md4cFlags={{
latexMath: mathRenderingEnabled,
hardSoftBreaks: props.preserveSoftBreaks ?? false,
admonitions: true,
preserveWindowsPaths: true,
}}
enableTaskListItemToggle={props.onTaskListItemPress !== undefined}
onTaskListItemPress={props.onTaskListItemPress}
enableCodeBlockWrapToggle
spoilerOverlay="solid"
onLinkPress={
props.onLinkPress || props.onImagePress ? ({ url }) => onLinkPress(url) : undefined
}
onLinkLongPress={onLinkLongPress}
linkContextMenuItems={linkContextMenuItems}
linkPillContent={linkPillContent}
accessibilityLabels={accessibilityLabels}
/>
);
};
return Platform.OS === "android" && props.fileContextMenu ? (
) : (
renderMarkdown()
);
}
function MarkdownMenuAnchor(props: {
readonly renderMarkdown: (openMenu?: () => void) => ReactNode;
readonly onLongPress?: () => void;
}) {
return props.renderMarkdown(props.onLongPress);
}