emailmd

repository·main·Indexed 23 days ago

https://github.com/anypost/emailmd

A tool that converts markdown into responsive, email-safe HTML designed for major email clients like Gmail, Outlook, and Apple Mail using MJML. It provides a core rendering API, a CLI, a Model Context Protocol (MCP) server for AI assistants, and a React integration package featuring the useEmailmd hook, EmailPreview component, and a full-featured EmailmdBuilder editor.

Tokens
35.8K
Snippets
133
Records
218
Agent score
79%

What's inside emailmd

  1. Overview of emailmd features

    main

    emailmd is a markdown-to-email converter that uses MJML under the hood to ensure high compatibility across email clients like Gmail, Outlook, Apple Mail, and Yahoo Mail.

    Key capabilities include:

    • Email-safe HTML: Bulletproof layouts using MJML.
    • Layout Directives: Support for heroes, columns, callouts, and accordions.
    • Theming: Brand colors, custom fonts, and automatic dark mode support.
    • Buttons: Primary, secondary, and semantic variants using a simple attribute.
    • Frontmatter: Metadata and theme overrides defined per email.
    • Linting: Checks for deliverability and accessibility.
    • Dual Output: Automatic generation of text/plain versions.
    • Tooling: Includes a CLI, React builder, MCP server for AI, and reusable partials.
  2. Handle additive changes in union types

    main

    New capabilities (new RenderOptions, new frontmatter keys, new directives, or new warning stages) are introduced in minor versions.

    If your code exhaustively matches on union types such as RenderWarning['stage'] or SegmentType, you must include a default case. This ensures your code remains compatible when these unions grow in future minor releases.

  3. Table styling and mobile optimization

    main

    Automatic Styling

    Tables automatically inherit theme colors and apply the following styles:

    • Header row: Bold text with a 2px bottom border.
    • Body rows: Subtle 1px bottom borders between rows.
    • Cell padding: 8px vertical, 12px horizontal.

    Mobile Best Practices

    To ensure tables look good on mobile devices:

    • Keep tables simple and narrow.
    • Aim for 2–4 columns to avoid horizontal scrolling on small screens.
  4. Rules and constraints for Partials

    main

    When using partials, keep the following rules in mind:

    • Nesting: Partial expansion happens textually before markdown parsing. You can include directives, buttons, images, and even other partials. Nesting is supported up to 10 levels deep.
    • Cycles: Circular includes (e.g., A includes B, and B includes A) are detected and skipped with a warning.
    • Frontmatter: If a partial starts with a --- block, it will be stripped with a warning. Frontmatter should reside in the main document, not the partial.
    • Naming: Partial names can include letters, digits, _, -, ., and / (useful for namespacing like blocks/footer).
    • Code Fences: Include lines inside code fences or indented code blocks are ignored, allowing you to document the syntax itself.
  5. Understand the emailmd Semver contract

    main

    emailmd follows Semantic Versioning (Semver). The core contract is that the input language and the API are stable, but the output bytes (the exact HTML/MJML generated) are not.

    What is guaranteed (Major version required for changes):

    • The render(markdown, options?) API signature and its Promise<RenderResult> return shape (html, text, meta, warnings).
    • Documented RenderOptions fields and values.
    • Documented markdown behavior (headings, lists, tables, emoji, etc.).
    • Directive names and parameters (e.g., ::: callout, bg=, center).
    • Button syntax and attributes.
    • Frontmatter keys (preheader, theme, fonts, etc.).
    • Template tag passthrough syntax ({{ x }}, {% x %}, ${x}, %%x%%, [[x]]).
    • Exported functions, types, and CLI arguments.
    • Rendering semantics (e.g., a callout must always render as a visually distinct card).

    What is NOT guaranteed (Minor/Patch changes allowed):

    • The exact HTML markup structure, attribute order, or whitespace.
    • The specific version of MJML bundled.
    • The exact wording of warning messages (though the stage is stable).
    • Internal formats and markers like <!--EMAILMD:...-->.
    • Undocumented behavior.
  6. Use Directives for email layout

    main

    Directives are special block-level sections used to control the layout of an email. They are implemented using the ::: fenced container syntax.

    For directives that require content, use a closing ::: tag. For single-line directives like spacer or divider, no content or closing tag is required.

    ::: directive
    Content goes here
    :::
  7. Use Partials for reusable markdown components

    main

    Partials are named blocks of markdown that can be reused across multiple emails using the ::: include <name> directive. When you edit a partial, the changes propagate to every email that includes it.

    Partials are provided to the render() function via a partials map where the key is the partial name and the value is the markdown string.

    In the React builder, you can pass this map to the renderOptions prop to see includes expand live in the preview.

    import { render } from 'emailmd';
    
    const { html } = await render(markdown, {
      partials: {
        'legal-footer': `::: footer
    **Acme Inc.** · 123 Main St, Portland OR
    [Unsubscribe]({{unsubscribe_url}}) · [Preferences]({{prefs_url}})
    :::`,
      },
    });
  8. Configure themes and automatic dark mode

    main

    Use the theme key to switch the base theme for a single email. Valid values are:

    • light (default)
    • dark (renders dark for everyone)
    • auto (renders light by default but adapts to the reader's dark mode settings)

    To customize the dark variant when using theme: auto, provide a dark: map containing snake_case theme overrides. Providing a dark: map automatically implies theme: auto.

    Note: Explicitly setting theme: light or theme: dark pins the theme and prevents automatic adaptation to the reader's client settings.

    ---
    theme: auto
    dark:
      background_color: "#111827"
      content_color: "#1f2937"
    ---
  9. Understand Accordion fallback behavior in email clients

    main

    Accordion interactivity depends on the email client's support for specific CSS/HTML patterns:

    • Supported Clients (e.g., Apple Mail, iOS Mail): Panels are interactive and can be collapsed/expanded.
    • Unsupported Clients (e.g., Gmail, Outlook): All panels are shown in an expanded state. The content renders as a natural sequence of headings and text.
    • Plain-text output: The accordion is flattened into a standard text sequence.
  10. Compare Partials, Snippets, and Wrappers

    main

    Understanding the difference between these three concepts is key to choosing the right tool:

    FeatureSnippets (Builder)PartialsWrappers
    BehaviorInserts a static copy at the cursor.A live reference to a named block.Replaces the whole document chrome.
    UpdatesEdits don't affect existing emails.Edits propagate to all including emails.Controls layout-level structure.
    Use CaseQuick content insertion.Reusable components (footers, headers).Global layout/template control.