Mermaid.js Packet Diagram Syntax & Layout Guide

A Packet diagram is a specialized visualization component used by network engineers, security analysts, and systems developers to map out the exact binary structure of network protocols, memory allocations, or custom hardware packet headers. Introduced natively in Mermaid.js, this tool replaces tedious manual table drawing by automatically rendering a responsive, perfectly aligned bit-level grid map from a clean, text-based field breakdown.

Understanding the Packet Grid Layout

Unlike standard free-form flowcharts, a Packet diagram relies on a strict, mathematical grid system modeled directly after networking RFCs:

  • Bit Width (Horizontal Axis): The layout engine automatically segments each row into a 32-bit grid system (spanning bits 0 to 31) by default.
  • Automatic Bit Calculation: You simply declare the bit length size for each field. The rendering engine handles the math—automatically packing adjacent fields into the same row or wrapping them cleanly to the next row when the cumulative size passes the 32-bit threshold.

Basic Syntax Structure

Every packet map begins with the packet declaration header. It is followed by block fields declared as sequential pairs matching a strict "Field Name": BitSize pattern on individual lines.

packet
  0-3: "Version"
  4-7: "IHL"
  8-15: "Type of Service"
  16-31: "Total Length"

Syntax Reference

The table below breaks down the foundational data parameters and formatting layout rules required to construct a Packet diagram in Mermaid.js.

Syntax Element Type Requirement Description & Visual Grid Behavior
Declaration Keyword Identifier Initializes the bit-level protocol layout workspace. Must use the exact packet header block.
Field Name Quoted String The label that displays inside the block space. Must always be enclosed in double quotation marks.
Bit Size Positive Integer The absolute bit length of the field (e.g., 1 for single-bit flags, 8 for bytes, 16 for shorts). Sets the visual width.
Row Break Trigger Automatic Tracker Occurs automatically every time the combined total bit sizes hit a multiple of 32 bits.

Real-World Blueprint: IPv4 Packet Header Structural Map

This comprehensive blueprint tracks a standard internet routing packet structure (IPv4). It showcases how multi-bit fields, single-bit flags, and larger 16-bit blocks assemble sequentially inside the standard 32-bit horizontal grid layout. Fields that hit the 32-bit mark drop cleanly to the next row automatically.

packet
  0-3: "Version"
  4-7: "IHL"
  8-15: "Type of Service"
  16-31: "Total Length"
  32-47: "Identification"
  48-50: "Flags"
  51-63: "Fragment Offset"
  64-71: "TTL"
  72-79: "Protocol"
  80-95: "Header Checksum"


Syntax Tip: If a single declared field is larger than 32 bits (such as a 64-bit cryptographic salt or timestamp), the rendering engine will smoothly wrap and stretch that block vertically across multiple sequential rows, using clean connection indicators to preserve readability.


Common Syntax Pitfalls & System Constraints

When engineering highly precise byte-level documentation maps, keep these troubleshooting parameters in mind to prevent compiling faults:

  • Mandatory Quote Wrappers: Every single field descriptor text string must be wrapped inside double quotation marks. Omitting quotation brackets will cause immediate syntax parsing errors.
  • Invalid String Bit Sizes: The value thickness assignment following the colon separator must be a raw, unquoted integer. Passing decimals or alphabetic sizes (e.g., "Data": 8bits) will halt diagram compilation.
  • Grid Edge Alignment: Ensure your combined fields total up cleanly to match multiples of 32 if you want uniform, box-shaped header diagrams. Leaving a row trailing with unassigned bits can cause uneven looking end blocks.
  • Scale View Constraints: While the engine dynamically calculates layout widths, cramming ultra-long text descriptions into tiny 1-bit or 2-bit blocks will cut off typography or cause visual crowding. Keep small-bit labels concise.
Scroll to Top