Marpit Documentation

repository·main·Indexed 23 days ago

https://github.com/marp-team/marpit

A lightweight Markdown slide deck framework that transforms Markdown and CSS into static HTML/CSS slide decks. Marpit serves as the core engine for the Marp ecosystem, featuring a CSS-based theming system, specialized slide syntax via directives, and extensibility through a markdown-it compatible plugin interface. It supports global and local directives for controlling slide properties, pagination, and backgrounds, as well as experimental inline SVG slides for pixel-perfect scaling.

Tokens
12.9K
Snippets
37
Records
81
Agent score
72%

What's inside Marpit

  1. What is Marpit?

    main

    Marpit is a lightweight Markdown slide deck framework designed to transform Markdown and CSS themes into a slide deck composed of static HTML and CSS. The resulting output is a web page that can be converted into a slide PDF via printing.

    Marpit is intended to be used as a base framework for other tools and applications, specifically serving as the core converter for the Marp ecosystem. It focuses on outputting minimum assets for the slide deck.

  2. Core features of Marpit

    main

    Marpit provides several key capabilities for slide creation:

    • Marpit Markdown: An extended markdown-it parser that supports features like Directives and Slide backgrounds.
    • Theme CSS: A pure CSS theming system. Unlike other frameworks, Marpit does not use predefined classes or mixins; you style standard HTML elements directly.
    • Inline SVG slides (Experimental): Allows using <svg> elements as slide containers for pixel-perfect scaling via CSS and advanced backgrounds using <foreignObject> while maintaining the Markdown DOM structure.
  3. Marpit Markdown extended features

    main

    Beyond standard Markdown, Marpit provides several extended syntaxes to enhance slide creation:

    • Directives: Used to control slide-deck properties like themes, page numbers, headers, footers, and custom styles.
    • Image syntax: An extended version of the standard ![](image.jpg) syntax designed for better slide layout and styling.
    • Fragmented lists: Since v0.9.0, you can use specific markers in lists to create 'fragmented lists', where list items appear one by one during a presentation.
  4. Use the :root selector in Marpit themes

    main

    In Marpit, the :root pseudo-class selector targets the <section> element of each slide page rather than the <html> element.

    Using :root is functionally similar to using section, but :root has higher CSS specificity. If both are used in a theme, declarations in :root will take precedence.

    Additionally, rem units used within :root or section will automatically resolve to relative values based on the parent <section> element, ensuring consistent scaling regardless of the host document's font size.

    /* @theme marpit-theme */
    
    :root {
      width: 1280px;
      height: 960px;
      font-size: 40px;
      padding: 1rem;
    }
    
    h1 {
      font-size: 1.5rem;
      color: #09c;
    }
    
    h2 {
      font-size: 1.25rem;
    }
  5. Understand the HTML output for fragmented lists

    main

    Marpit renders fragmented lists using standard HTML list structures but injects specific data-marpit-* attributes for fragment handling.

    • On the <section> element: Adds data-marpit-fragments="N" where N is the total number of fragmented items on that slide.
    • On each <li> element: Adds data-marpit-fragment="N" where N is the sequential index of the fragment (starting from 1).

    Fragmented lists do not change the DOM structure or appearance by themselves; they rely on the integrated application (e.g., a Marp presentation viewer) to interpret these attributes and implement the step-by-step visibility behavior.

    <section id="2" data-marpit-fragments="3">
      <h1>Fragmented list</h1>
      <ul>
        <li data-marpit-fragment="1">One</li>
        <li data-marpit-fragment="2">Two</li>
        <li data-marpit-fragment="3">Three</li>
      </ul>
    </section>
  6. How Inline SVG slides work and why to use them

    main

    The inlineSVG mode provides several architectural advantages for presentation engines:

    • Pixel-perfect scaling: You can delegate slide scaling to the SVG engine. By defining a size for viewing (e.g., fitting the SVG to the viewport), the browser handles the scaling of the content inside the <foreignObject>.
    • JavaScript-less presentations: Because Marpit uses scroll-snap-align on <section> elements, slides can align to the viewport using CSS Scroll Snap, allowing for minimal, logic-less web presentations.
    • Isolated layers: Advanced backgrounds are injected into the <foreignObject> layer. This prevents background elements from breaking CSS selectors like :first-child or the adjacent sibling combinator (+) within the Markdown DOM structure.
  7. Develop a markdown-it compatible plugin

    main
    Marpit's plugin interface is compatible with markdown-it. If you want to manipulate the result of Markdown rendering, you should follow the standard markdown-it plugin architecture. For detailed information on how to manipulate rendering results, refer to the official markdown-it documentation.
  8. Marpit CSS theming system

    main
    Marpit uses a CSS-based theming system that allows for complete design control. Unlike other slide frameworks, Marpit does not use predefined classes or mixins. Instead, users focus on styling HTML elements using pure CSS, and Marpit handles the necessary conversions based on the selected theme.
  9. Marpit Markdown features

    main

    Marpit extends the markdown-it parser to support specialized slide syntax, including:

    • Directives: For controlling slide behavior and properties.
    • Slide backgrounds: Using specific image syntax to set backgrounds.

    The syntax is designed to maintain compatibility with general Markdown documents.

  10. Extract presenter notes from HTML comments

    main

    Marpit collects HTML comments (that are not Marpit directives) during rendering. These are returned in the comments property of the object returned by marpit.render().

    The comments value is a two-dimensional array where each inner array contains the comments found on a specific slide page.

  11. Understand Global vs Local Directives

    main

    Directives are categorized by their scope:

    Global Directives

    These settings apply to the entire slide deck. If the same global directive is defined multiple times, Marpit only recognizes the last value.

    Local Directives

    These settings apply to the defined page and all following pages.

    <!-- backgroundColor: aqua -->
    
    This page has aqua background.
    
    ---
    
    The second page also has same color.

    Spot Directives (Single Page)

    To apply a local directive to only the current page, prefix the directive name with an underscore (_).

    <!-- _backgroundColor: aqua -->
    
    Add underscore prefix `_` to the name of local directives.
    
    ---
    
    The second page would not apply setting of directives.
    <!-- _backgroundColor: aqua -->
  12. Inline SVG slides (Experimental)

    main

    Marpit optionally supports using the <svg> element as the container for each slide page. This approach allows for:

    • Pixel-perfect scaling: Achieved entirely through CSS.
    • Simplified integration: Easier handling of slides within integrated applications.
    • Advanced backgrounds: Using an isolated layer made by <foreignObject> to provide backgrounds while preserving the original Markdown DOM structure.