# pazerw/marginnote-companion architecture

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

- Interactive diagram: https://gitdiagram.com/pazerw/marginnote-companion
- Repository: https://github.com/pazerw/marginnote-companion

## Overview

##### Main Components
- Plugin Core: The central entry point appears to be src/mn-main.ts, which likely initializes the plugin and sets up the main orchestration.
- Controls: The src/controls folder contains UI-related files (both TypeScript and LESS styling) that likely manage display components or command buttons within the Obsidian UI.
- Handlers: Found in src/handlers (e.g., add-new-video, frontmatter, mn-data-handler), these modules probably process data incoming from MarginNote (such as handling metadata, video imports, or note transformation).
- Receivers: The src/receivers directory holds modules like autopaste, input-handler, and insert, indicating that these components are responsible for catching external events or data (e.g., receiving pasted content or note selections from MarginNote).
- Macros: Under src/macros, these files (autodef, macro-handler) seem to process template expansions or dynamic text generation.
- Templates: The src/template directory provides several files (basic templates, note-template, toc-template) that specify how inserted MarginNote content should be formatted in Obsidian.
- Settings and Configuration: Files like src/settings.ts and related LESS files (e.g., setting-tab.less) likely manage user configuration and plugin settings.
- Language Support: The src/lang subdirectory supports multi-language configurations (with English and Chinese files).
- Type Definitions: The src/typings directory contains TypeScript definitions to ensure type safety across the codebase.
- Build & Configuration: At the top level, you have ESBuild, Rollup, Jest configuration files, and others that set up the development environment and building process.

##### Out Relationships & Data Flow
- User actions in Obsidian (triggered via commands, toolbar buttons, or hotkeys) likely propagate to the Controls, which then call the Handlers.
- Handlers process the imported data (including video notes and PDF hierarchies) and pass them to the Receivers and Macro processing. For example, the autopaste receiver listens for selections from MarginNote.
- The Template modules then format these processed data inputs according to user-defined or default templates.
- Settings and language modules feed configuration values to the Controls, Handlers, and Template modules.
- Build/Configuration files ensure that these modules are properly bundled and deployed as an Obsidian plugin.

##### Architectural Patterns and Design Principles
- Modular Design: The clear separation between controls, handlers, receivers, templates, macros, and settings shows a modular architecture. Emphasize independent modules interacting through well-defined interfaces.
- Event-Driven Flow: Many components (such as receivers and handlers) indicate an event-based data flow, with direct responses to incoming commands or data from MarginNote.
- Layered Approach: At the base, build configurations and type definitions support the higher-level business logic layers (handlers and macros), which in turn support the presentation layer (controls and templates).

##### Relevant Technologies & Tools
- TypeScript is the primary language.
- LESS is used for styling (as seen in various .less files).
- Build tools include esbuild and rollup.
- Testing is set up via Jest (jest.config.ts).
- The plugin also depends on external tools (like media-extended) and interacts with Obsidian’s API.

##### Elements
- Each major component should be represented as a node with a clear label (e.g., “Plugin Core”, “Controls Module”, “Handlers”, “Receivers”, “Templates”, “Settings”, “Language Support”).
- One for core orchestration (central Hub),
- One for UI/presentation (Controls/Template),
- One for business logic (Handlers/Receivers/Macros),
- One for configuration (Settings/Language).
- Optionally, add annotations or callouts indicating key external integrations, such as the connection with MarginNote 4 and dependency on media-extended.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    UA["UserAction/Command"]:::external
    MN["MarginNote 4"]:::external

    %% Core Layer
    subgraph "Core Layer"
        PC["Plugin Core"]:::core
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        CR["Controls Module"]:::ui
        TP["Templates"]:::ui
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        HD["Handlers"]:::logic
        RV["Receivers"]:::logic
        MC["Macros"]:::logic
    end

    %% Configuration Layer
    subgraph "Configuration Layer"
        ST["Settings & Configuration"]:::config
        LS["Language Support"]:::config
        TD["Type Definitions"]:::config
    end

    %% Development Tools Layer
    subgraph "Development Tools"
        BD["Build & Development Tools"]:::build
    end

    %% Arrows representing Data Flow
    UA -->|"triggers"| CR
    MN -->|"feeds data"| RV

    PC -->|"orchestrates"| CR
    PC -->|"orchestrates"| HD
    PC -->|"orchestrates"| RV
    PC -->|"orchestrates"| TP

    CR -->|"calls"| HD
    HD -->|"processes & passes"| RV
    HD -->|"invokes"| MC
    MC -->|"formats output"| TP

    ST -->|"configures"| CR
    ST -->|"configures"| HD
    ST -->|"configures"| TP
    LS -->|"supports"| ST
    TD -->|"types"| PC
    TD -->|"types"| CR
    TD -->|"types"| HD
    TD -->|"types"| RV
    TD -->|"types"| MC
    TD -->|"types"| TP

    BD -->|"bundles and deploys"| PC

    %% Click Events
    click PC "https://github.com/pazerw/marginnote-companion/blob/master/src/mn-main.ts"
    click CR "https://github.com/pazerw/marginnote-companion/tree/master/src/controls"
    click HD "https://github.com/pazerw/marginnote-companion/tree/master/src/handlers"
    click RV "https://github.com/pazerw/marginnote-companion/tree/master/src/receivers"
    click MC "https://github.com/pazerw/marginnote-companion/tree/master/src/macros"
    click TP "https://github.com/pazerw/marginnote-companion/tree/master/src/template"
    click ST "https://github.com/pazerw/marginnote-companion/blob/master/src/settings.ts"
    click LS "https://github.com/pazerw/marginnote-companion/tree/master/src/lang"
    click TD "https://github.com/pazerw/marginnote-companion/tree/master/src/typings"
    click BD "https://github.com/pazerw/marginnote-companion/blob/master/esbuild.js"

    %% Styles
    classDef core fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef ui fill:#87CEFA,stroke:#333,stroke-width:2px;
    classDef logic fill:#98FB98,stroke:#333,stroke-width:2px;
    classDef config fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef build fill:#D8BFD8,stroke:#333,stroke-width:2px;
    classDef external fill:#E0E0E0,stroke:#333,stroke-width:2px;
```
