diff --git a/native/chat/text-message-markdown-context.js b/native/chat/text-message-markdown-context.js index 4c6d9acc7..a266c925d 100644 --- a/native/chat/text-message-markdown-context.js +++ b/native/chat/text-message-markdown-context.js @@ -1,45 +1,62 @@ // @flow import * as React from 'react'; import * as SimpleMarkdown from 'simple-markdown'; -import type { SingleASTNode } from 'lib/shared/markdown'; +import type { ASTNode, SingleASTNode } from 'lib/shared/markdown'; import { messageKey } from 'lib/shared/message-utils'; import type { TextMessageInfo } from 'lib/types/messages/text'; import { useTextMessageMarkdownRules } from '../chat/message-list-types'; export type TextMessageMarkdownContextType = { +messageKey: string, +markdownAST: $ReadOnlyArray, + +markdownHasPressable: boolean, }; const TextMessageMarkdownContext: React.Context = React.createContext( null, ); +const pressableMarkdownTypes = new Set(['link']); +const markdownASTHasPressable = (node: ASTNode): boolean => { + if (Array.isArray(node)) { + return node.some(markdownASTHasPressable); + } + const { type, content, items } = node; + if (pressableMarkdownTypes.has(type)) { + return true; + } else if (items) { + return markdownASTHasPressable(items); + } else if (content) { + return markdownASTHasPressable(content); + } + return false; +}; function useTextMessageMarkdown( messageInfo: TextMessageInfo, ): TextMessageMarkdownContextType { // useDarkStyle doesn't affect the AST (only the styles), // so we can safely just set it to false here const rules = useTextMessageMarkdownRules(false); const { simpleMarkdownRules, container } = rules; const { text } = messageInfo; const ast = React.useMemo(() => { const parser = SimpleMarkdown.parserFor(simpleMarkdownRules); return parser(text, { disableAutoBlockNewlines: true, container }); }, [simpleMarkdownRules, text, container]); const key = messageKey(messageInfo); return React.useMemo( () => ({ messageKey: key, markdownAST: ast, + markdownHasPressable: markdownASTHasPressable(ast), }), [key, ast], ); } export { TextMessageMarkdownContext, useTextMessageMarkdown };