# wikimedia/wikipedia-preview architecture

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

- Interactive diagram: https://gitdiagram.com/wikimedia/wikipedia-preview
- Repository: https://github.com/wikimedia/wikipedia-preview

## Overview

##### Project Type and Purpose
- This is a front-end component/library that primarily provides an interactive preview of Wikipedia articles.
- Its purpose is to be embeddable as a standalone script, via an npm package, or wrapped inside a WordPress plugin.
- The library handles user interaction (hover/touch), fetches related content from Wikipedia (using its API), and displays the content in a themed popup component (supporting both light/dark modes).

##### Analyzing the File Structure
- Key directories:
- “src”: Contains the main source code including entry points (src/index.js), the API for triggering the previews (src/api.js) and modules for the gallery, popup, and preview components.
- “demo”: Contains HTML, CSS, and image files used to demonstrate the component’s functionality in a browser context.
- “i18n”: Contains a comprehensive set of translations for internationalization support.
- “cypress” and “test”: Provide e2e tests and unit tests respectively, demonstrating a focus on quality and automated testing.
- Configuration files (e.g., .babelrc, .eslintrc, vite.config.js, Storybook configuration in .storybook) and scripts indicate a modern JavaScript development setup.
- The structure reveals a clear separation of concerns:
- Core library logic (in src)
- Demo showcasing and testing (demo, cypress, test)
- Build, configuration, and story-driven testing (Storybook and Vite)

a. Core Library Module (src/)
- API Module: Entry point (src/api.js) that exposes the main functions such as init() and version().
- Gallery/Preview Components: Modules handling different interactive parts – gallery (src/gallery/*), popup (src/popup.js), preview (src/preview.js), and touch interactions (src/touchPopup.js).
- Utility Files: Helper modules such as src/cachedRequest.js, src/event.js, and src/utils.js.
- Internationalization: i18n directory representing the language support module.
b. Integration Points
- Browser Integration: Loading the standalone script, usage via script tags, or as an npm package.
- WordPress Plugin Wrapper: Although this code isn’t in the repository, note that this library can be extended through a thin plugin wrapper (referenced in the README).
c. Presentation/Demo Layer
- Demo HTML/CSS in the “demo” directory that shows how the component appears on a webpage.
d. Testing and Validation
- Unit tests (in “test”) and e2e tests (in “cypress”) that ensure component functionality.
e. Development & Build Infrastructure
- Storybook for isolated component development (files in .storybook and src/stories).
- Build and lint configurations (babel, eslint, stylelint, Vite) for code quality and bundling.
- CI configurations (GitHub workflows in .github/workflows) which manage building, testing, and deploying the project.

##### Relationships and Interactions
- Data flow from the raw input (hover or touch) to the API module, then fetching content (possibly representing an asynchronous network request) and finally rendering the popup.
- How internationalization (via the i18n directory) integrates with rendering by mapping language codes to text strings.
- Testing components that validate the data flow and component behavior.

##### Architectural Patterns and Design Principles
- Modular Design: The code is split into clear modules (gallery, popup, preview), making it easier to maintain and extend.
- Separation of Concerns: A clear distinction between UI rendering, event handling, API interactions, and testing.
- Extensibility: The plugin-based extension model and configurable options exemplify how the component can integrate with various systems.
- Configurable Initialization: The init() method accepts numerous options for targeting DOM nodes, event handling, and themes, illustrating the flexibility in design.

##### Diagramming Details
- From the UI events (hover/touch) to the API module.
- From the API module to the various UI components.
- Configuration dependencies between the demo and the UI rendering.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Core Library"
        Core_Library["Core Library (src/)"]:::core
        API_Module["API Module (api.js)"]:::core
        Gallery_Module["Gallery Module (gallery/)"]:::core
        Popup_Component["Popup Component (popup.js)"]:::core
        Preview_Component["Preview Component (preview.js)"]:::core
        Touch_Module["Touch Interaction (touchPopup.js)"]:::core
        Utility_Modules["Utility Modules (cachedRequest.js, event.js, utils.js)"]:::core
        Internationalization["Internationalization (i18n)"]:::core
    end

    subgraph "Integration"
        Browser_Integration["Browser Integration"]:::integration
        WP_Plugin["WordPress Plugin Wrapper"]:::integration
    end

    subgraph "Presentation / Demo"
        Demo_Showcase["Demo/Showcase (demo/)"]:::presentation
    end

    subgraph "Testing"
        Unit_Tests["Unit Tests (test/)"]:::testing
        E2E_Tests["E2E Tests (cypress/)"]:::testing
    end

    subgraph "Development & Build"
        Storybook["Storybook (.storybook, src/stories/)"]:::development
        Build_Config["Build & Config (vite.config.js, etc.)"]:::development
        CICD["CI/CD (.github/workflows/)"]:::development
    end

    Wikipedia_API["Wikipedia API"]:::external
    User_Event["User Interaction (Hover/Touch)"]:::external

    %% Relationships
    User_Event -->|"triggers"| API_Module
    API_Module -->|"calls"| Gallery_Module
    API_Module -->|"calls"| Popup_Component
    API_Module -->|"calls"| Preview_Component
    API_Module -->|"calls"| Touch_Module
    API_Module -->|"uses"| Utility_Modules
    API_Module -->|"fetches"| Wikipedia_API
    Internationalization -->|"supports"| Popup_Component
    Demo_Showcase -->|"demonstrates"| API_Module

    Unit_Tests -->|"validates"| API_Module
    Unit_Tests -->|"validates"| Gallery_Module
    Unit_Tests -->|"validates"| Popup_Component
    Unit_Tests -->|"validates"| Preview_Component
    E2E_Tests -->|"tests"| Demo_Showcase

    Storybook -->|"develops"| API_Module
    Build_Config -->|"bundles"| API_Module
    CICD -->|"deploys"| Build_Config

    Browser_Integration -->|"integrates"| API_Module
    WP_Plugin -->|"wraps"| Browser_Integration

    %% Styles
    classDef core fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef integration fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef presentation fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef testing fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef development fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef external fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;

    %% Click Events
    click Core_Library "https://github.com/wikimedia/wikipedia-preview/tree/main/src/"
    click API_Module "https://github.com/wikimedia/wikipedia-preview/blob/main/src/api.js"
    click Gallery_Module "https://github.com/wikimedia/wikipedia-preview/tree/main/src/gallery/"
    click Popup_Component "https://github.com/wikimedia/wikipedia-preview/blob/main/src/popup.js"
    click Preview_Component "https://github.com/wikimedia/wikipedia-preview/blob/main/src/preview.js"
    click Touch_Module "https://github.com/wikimedia/wikipedia-preview/blob/main/src/touchPopup.js"
    click Utility_Modules "https://github.com/wikimedia/wikipedia-preview/blob/main/src/cachedRequest.js"
    click Internationalization "https://github.com/wikimedia/wikipedia-preview/tree/main/i18n/"
    click Demo_Showcase "https://github.com/wikimedia/wikipedia-preview/tree/main/demo/"
    click Unit_Tests "https://github.com/wikimedia/wikipedia-preview/tree/main/test/"
    click E2E_Tests "https://github.com/wikimedia/wikipedia-preview/tree/main/cypress/"
    click Storybook "https://github.com/wikimedia/wikipedia-preview/tree/main/.storybook/"
    click Build_Config "https://github.com/wikimedia/wikipedia-preview/blob/main/vite.config.js"
    click CICD "https://github.com/wikimedia/wikipedia-preview/tree/main/.github/workflows/"
```
