# iamacup/react-native-markdown-display architecture

> Architecture diagram of the GitHub repository iamacup/react-native-markdown-display, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iamacup/react-native-markdown-display
- Repository: https://github.com/iamacup/react-native-markdown-display

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global nodes
    Consumer["React Native App (Consumer)"]:::consumer

    %% Core Pipeline Subgraph
    subgraph "Core Pipeline"
        MC_Int["Markdown Component Interface"]:::interface
        Parser["Markdown Parser & Tokenizer"]:::parser
        AST_Renderer["AST Renderer"]:::renderer
        Native["Native Components"]:::native
    end

    %% Rendering Configuration Subgraph
    subgraph "Rendering Configuration"
        Render_Rules["Render Rules Engine"]:::rules
        Styling["Styling Engine"]:::styling
        DataConfig["Data & Configuration"]:::data
    end

    %% Utility Modules Subgraph
    subgraph "Utility Modules"
        Utils["Utility Modules"]:::utility
    end

    %% Data Flow Connections
    Consumer -->|"provides markdown"| MC_Int
    MC_Int -->|"processes input"| Parser
    Parser -->|"generates AST"| AST_Renderer
    AST_Renderer -->|"renders view tree"| Native
    AST_Renderer -->|"applies"| Render_Rules
    AST_Renderer -->|"incorporates"| Styling
    Styling -->|"uses defaults from"| DataConfig
    Parser -->|"utilizes"| Utils
    Utils -->|"assists processing"| AST_Renderer

    %% Click Events for Component Mapping
    click MC_Int "https://github.com/iamacup/react-native-markdown-display/blob/master/src/index.js"
    click MC_Int "https://github.com/iamacup/react-native-markdown-display/blob/master/src/index.d.ts"
    click Parser "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/parser.js"
    click Parser "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/stringToTokens.js"
    click Parser "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/tokensToAST.js"
    click AST_Renderer "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/AstRenderer.js"
    click Render_Rules "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/renderRules.js"
    click Styling "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/styles.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/cleanupTokens.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/convertAdditionalStyles.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/flattenInlineTokens.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/getTokenTypeByToken.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/getUniqueID.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/groupTextTokens.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/hasParents.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/omitListItemParagraph.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/openUrl.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/removeTextStyleProps.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/renderInlineAsText.js"
    click Utils "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/util/splitTextNonTextNodes.js"
    click DataConfig "https://github.com/iamacup/react-native-markdown-display/blob/master/src/lib/data/textStyleProps.js"

    %% Styles
    classDef consumer fill:#FFEB3B,stroke:#000,stroke-width:1px
    classDef interface fill:#BBDEFB,stroke:#0D47A1,stroke-width:1px
    classDef parser fill:#C8E6C9,stroke:#2E7D32,stroke-width:1px
    classDef renderer fill:#FFE0B2,stroke:#E65100,stroke-width:1px
    classDef rules fill:#E1BEE7,stroke:#6A1B9A,stroke-width:1px
    classDef styling fill:#F8BBD0,stroke:#AD1457,stroke-width:1px
    classDef utility fill:#E0E0E0,stroke:#757575,stroke-width:1px
    classDef data fill:#B2EBF2,stroke:#006064,stroke-width:1px
    classDef native fill:#FFF9C4,stroke:#F57F17,stroke-width:1px
```
