How iamacup/react-native-markdown-display works
Main components
• Markdown Component Interface: This serves as the entry point (exported in src/index.js and index.d.ts) that users import into their React Native apps. It accepts props such as markdown content, custom styling, render rules, and event handlers (e.g., for link presses).
• Markdown Parser & Tokenizer: Inside src/lib/parser.js and utility files in src/lib/util (like stringToTokens.js and tokensToAST.js), the markdown input is processed using a markdown-it instance. This component is responsible for parsing Markdown syntax into tokens and generating an Abstract Syntax Tree (AST).
• AST Renderer: Located in src/lib/AstRenderer.js, this part takes the AST and maps it into React Native views by applying render rules.
• Render Rules Engine: Found in src/lib/renderRules.js, this defines how each Markdown element (headings, lists, code blocks, etc.) is rendered. It can be overridden or extended by users.
• Styling Engine: In src/lib/styles.js and related style files, default style rules are maintained. Users can provide overrides via props to change the appearance of elements.
• Utility Modules: The various files in the src/lib/util directory (e.g., cleanupTokens.js, grouping and converting tokens, helper functions for token management) offer support for transforming and preparing the tokens/AST prior to rendering.
• Data & Configuration: The subdirectory src/lib/data contains configuration or style property definitions (like textStyleProps.js) to support consistent styling across rendered elements.
Relationships and interactions
• It then passes the content to the Parser & Tokenizer, which uses the markdown-it instance to tokenize the markdown string.
• The tokens are converted into an AST (via tokensToAST and related utility functions).
• The AST Renderer then traverses the AST, and for each node, it applies a matching render rule from the Render Rules Engine. It uses the default styles from the Styling Engine, which may be merged with any custom style provided.
• Utility functions intercede throughout the process for additional processing (such as cleaning up or modifying tokens) and event handling (e.g., onLinkPress for links).
• Finally, the output is a tree of native React Native components (like Text, View, etc.) that are rendered on the mobile screen.
• Additional optional components include support for plugins (e.g., video embedding via markdown-it plugins) and debug/AST printing.
Architectural patterns
• Modular Design: Each aspect of the process (parsing, AST construction, rendering, styling, utility functions) is encapsulated in separate modules. This aligns with separation of concerns.
• Extensibility: The render rules and style definitions are abstracted, allowing users to override and extend how Markdown is rendered.
• Customization via Injection: The design allows a custom markdown-it instance to be injected, as well as custom renderer implementations (via the renderer prop), promoting flexibility.
• Data Flow: The flow goes from raw markdown -> tokenization -> AST -> component rendering, with clear transformation stages.
Technologies, frameworks, and significant libraries
• React Native: The target framework where native components are used to display the rendered markdown.
• markdown-it: A key dependency used for parsing Markdown into tokens.
• Custom JavaScript: Modules written in JS (and type definitions in the index.d.ts file) that handle AST processing, token conversion, and styling.