# yusukebe/gh-markdown-preview architecture

> Architecture diagram of the GitHub repository yusukebe/gh-markdown-preview, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yusukebe/gh-markdown-preview
- Repository: https://github.com/yusukebe/gh-markdown-preview

## Overview

##### Project Type Analysis
This is a command-line tool that serves as an extension to GitHub CLI, providing a local web server functionality to preview Markdown files with GitHub-style rendering.

##### Main Components

A. Core Components:
- CLI Interface (cmd/cli.go)
- Web Server (cmd/server.go)
- WebSocket Handler (cmd/websocket.go)
- File Watcher (cmd/watcher.go)
- Browser Manager (cmd/browser.go)

B. External Services:
- GitHub Markdown API (for rendering)
- GitHub Web CSS (for styling)

**[User Input] → [CLI Interface]**
- ↓
**[File System] ← [File Watcher] → [WebSocket Server]**
- ↓                    ↓
**[GitHub API] ← [Web Server] → [Browser Client]**
- ↑
**[GitHub CSS] ─────────┘**

##### Component Details

a) Input Layer:
- CLI command entry point
- File system watching capabilities
- Configuration options handling

b) Processing Layer:
- Web server component handling HTTP requests
- WebSocket server for live reload functionality
- File watcher for detecting markdown changes
- Browser automation for auto-opening feature

c) External Integration Layer:
- GitHub Markdown API connection
- GitHub CSS fetching mechanism

##### Data Flow Representation

- Command flow from user input to server initialization
- File change notifications from watcher to WebSocket
- Markdown content flow to GitHub API
- Rendered HTML delivery to browser
- Live reload notifications through WebSocket

##### Additional Elements

Notes about:
- Default port (3333) configuration
- Auto-detection of README files
- Dark/Light mode switching capability
- Browser compatibility (Chrome, Firefox, Safari)

##### Technical Details to Highlight

- Zero-dependency nature (except for gh CLI)
- Authentication handling with GitHub
- Port availability checking
- Live reload mechanism
- Browser auto-opening functionality

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Input
        UI[User Input]
        FS[File System]
    end

    subgraph Core["Core Application"]
        CLI["CLI Interface"]:::core
        APP["Main Application"]:::core
        WS["Web Server"]:::core
        WSH["WebSocket Handler"]:::core
        FW["File Watcher"]:::core
        BM["Browser Manager"]:::core
        UTIL["Utility Functions"]:::core
        TMPL["HTML Template"]:::core
    end

    subgraph External["External Services"]
        GHAPI["GitHub Markdown API"]:::external
        GHCSS["GitHub CSS"]:::external
    end

    subgraph Client["Client Layer"]
        Browser["Web Browser"]:::client
    end

    %% Connections
    UI --> CLI
    FS --> FW
    
    CLI --> APP
    APP --> WS
    APP --> WSH
    APP --> FW
    APP --> BM
    
    FW --> WSH
    WSH --> Browser
    
    WS --> GHAPI
    WS --> GHCSS
    WS --> Browser
    
    BM --> Browser

    %% Click Events
    click CLI "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/cli.go"
    click APP "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/app.go"
    click WS "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/server.go"
    click WSH "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/websocket.go"
    click FW "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/watcher.go"
    click BM "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/browser.go"
    click UTIL "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/utils.go"
    click TMPL "https://github.com/yusukebe/gh-markdown-preview/blob/master/cmd/template.html"

    %% Styling
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef external fill:#e67e22,stroke:#d35400,color:white
    classDef client fill:#95a5a6,stroke:#7f8c8d,color:white

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["External Services"]:::external
        L3["Client Interface"]:::client
    end
```
