import { modelChipIcon } from "../lib/contextChipIconSource"; import { useMemo } from "react"; import { Image } from "react-native"; import type { LinkPillContent, MarkdownLinkOccurrence, MarkdownStyle, } from "react-native-enriched-markdown"; import type { ComposerContextRecord } from "@t3tools/contracts"; import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks"; import { contextChipAccent, contextChipPresentation } from "../lib/composerChipPresentation"; import { CONTEXT_CHIP_ICON_SOURCES } from "../lib/contextChipIcons.generated"; import { FONT_FAMILIES } from "../lib/fontFamilies"; import { contextChipIconSource } from "../lib/contextChipIconSource"; import { markdownFileIconSource } from "../lib/markdownFileIcons"; import { markdownLinkIcon } from "../lib/markdownLinkIcons"; import { useUniwindTheme } from "../lib/useUniwindTheme"; import { resolveMarkdownFileIcon, resolveMarkdownLinkPresentation } from "../lib/markdownLinks"; import type { MarkdownLinkCustomization, SelectableMarkdownTextProps, } from "./SelectableMarkdownText.types"; import { enrichedContextLinkPresentation, enrichedLinkVariantPattern, enrichedModelReference, enrichedSkillDisplayName, enrichedSkillReference, enrichedThreadLinkPresentation, } from "../lib/enrichedLinkPresentation"; function chipCustomization( chip: ReturnType, ): MarkdownLinkCustomization | undefined { if (!chip) return undefined; return { label: chip.label, icon: contextChipIconSource(chip.icon), chip: { accent: chip.accent, tintsIcon: chip.icon.kind === "chip" }, }; } export function enrichedContextLinkCustomization( href: string, records?: ReadonlyArray, ) { return chipCustomization(enrichedContextLinkPresentation(href, records)); } export function enrichedThreadLinkCustomization(href: string) { return chipCustomization(enrichedThreadLinkPresentation(href)); } export function withChipIcon( customization: MarkdownLinkCustomization | undefined, iconUri: string | undefined, ): MarkdownLinkCustomization | undefined { return iconUri && customization?.chip ? { ...customization, icon: { uri: iconUri }, chip: { ...customization.chip, tintsIcon: false }, } : customization; } // Autolinks and inline-code paths link their own text, but the parser keeps backslash escapes // (preserveWindowsPaths) and drops emphasis markers, e.g. `src/__tests__/a.ts` reads src/tests/a.ts. const LINK_TEXT_MARKUP = /[\\*_~$|^=]/g; function isLinkTextOfUrl(label: string, url: string) { return label.replace(LINK_TEXT_MARKUP, "") === url.replace(LINK_TEXT_MARKUP, ""); } function presentation( url: string, props: Pick, authoredLabels: ReadonlyArray, ): MarkdownLinkCustomization | undefined { const custom = props.linkCustomization?.(url); if (custom) return custom; const context = enrichedContextLinkCustomization(url); if (context) return context; const skill = enrichedSkillReference(url, props.skills); if (skill) return { label: enrichedSkillDisplayName(skill), icon: CONTEXT_CHIP_ICON_SOURCES.skill, chip: { accent: contextChipPresentation("skill").accent, tintsIcon: true }, }; const model = enrichedModelReference(url, props.models); if (model) { const icon = modelChipIcon(model); return { label: model.label, icon: icon.source, chip: { accent: icon.accent, tintsIcon: icon.tintsIcon, }, }; } const link = resolveMarkdownLinkPresentation(url); if (link.kind === "file") return { // Without a label the native pill shows the authored text, e.g. [the setup steps](docs/guide.md). label: authoredLabels.every( (label) => isMarkdownFileLinkLabel(label, link.href) || isLinkTextOfUrl(label, url), ) ? link.label : undefined, icon: markdownFileIconSource(link.icon), chip: { accent: contextChipPresentation("mention").accent, tintsIcon: false }, }; const icon = link.kind === "external" ? markdownLinkIcon(link.host) : null; return icon ? { color: props.textStyle.linkColor, icon: icon.source, ...(icon.tints ? { iconTintColor: props.textStyle.linkColor } : {}), } : undefined; } export function useEnrichedLinkVariants( urls: ReadonlyArray, props: SelectableMarkdownTextProps, fileHrefs: ReadonlySet, occurrences: ReadonlyArray, ) { const { linkCustomization, skills, models, textStyle, fileEmbedLabel } = props; const theme = useUniwindTheme(); const links = useMemo(() => { const labelsByUrl = new Map(); for (const link of occurrences) { const labels = labelsByUrl.get(link.url); if (labels) labels.push(link.label); else labelsByUrl.set(link.url, [link.label]); } const byUrl = new Map(); for (const url of urls) { if (byUrl.has(url)) continue; const label = fileHrefs.has(url) ? fileEmbedLabel?.(url, false) : null; const value = label ? { label, color: textStyle.fileTextColor, icon: markdownFileIconSource(resolveMarkdownFileIcon(label)), } : presentation( url, { linkCustomization, skills, models, textStyle }, labelsByUrl.get(url) ?? [], ); if (value) byUrl.set(url, value); } return byUrl; }, [urls, linkCustomization, skills, models, textStyle, fileHrefs, fileEmbedLabel, occurrences]); const chipForegrounds = useMemo( () => new Map( [...links].map(([url, link]) => [ url, link.chip ? (contextChipAccent(link.chip.accent, theme, textStyle.color) ?? textStyle.color) : undefined, ]), ), [links, textStyle, theme], ); const linkVariants = useMemo( () => Object.fromEntries( [...links].map(([url, link]) => { const chipForeground = chipForegrounds.get(url); return [ enrichedLinkVariantPattern(url), { pill: { borderColor: "transparent", borderWidth: 0, borderRadius: 6, paddingHorizontal: 5, paddingVertical: 2, }, fontFamily: chipForeground ? FONT_FAMILIES.medium : textStyle.fontFamily, color: chipForeground ?? link.color ?? textStyle.fileTextColor, underline: false, backgroundColor: "transparent", }, ]; }), ), [chipForegrounds, links, textStyle], ); const linkPillContent = useMemo>( () => Object.fromEntries( [...links].map(([url, link]) => [ url, { label: link.label, iconUri: link.icon ? Image.resolveAssetSource(link.icon)?.uri : undefined, iconTintColor: link.chip?.tintsIcon ? chipForegrounds.get(url) : link.iconTintColor, }, ]), ), [chipForegrounds, links], ); return { linkVariants, linkPillContent }; }