TTY::Markdown

repository·master·Indexed 43 days ago

https://github.com/piotrmurach/tty-markdown

A Ruby component that converts Markdown documents or text into terminal-friendly, colorized, and formatted output. It supports headers, lists, blockquotes, tables, and code blocks with syntax highlighting. It provides a flexible API via TTY::Markdown.parse and TTY::Markdown.parse_file, along with a CLI tool (tty-markdown-cli) for direct command-line use. Users can customize output through options for color modes, themes, indentation, width, and Unicode/ASCII symbols.

Tokens
5.4K
Snippets
36
Records
37
Agent score
43%

What's inside TTY::Markdown

  1. Configure color mode with :mode

    master

    The :mode option specifies the maximum number of colors to use for rendering code block elements. By default, it uses the 256 color scheme and attempts to detect terminal color support automatically. You can manually set it to a lower value (e.g., 16) for terminals with limited color support.

    TTY::Markdown.parse(markdown_string, mode: 16)
  2. Control color application with :color

    master

    The :color option determines when to apply coloring to document elements. Valid values are:

    • :always: Always apply color.
    • :auto: Automatically detect if the terminal supports color (default).
    • :never: Never apply color.
    TTY::Markdown.parse(markdown_string, color: :always)
  3. Customize element styles with :theme

    master

    Use the :theme option to override the styles of specific Markdown elements. The value should be a hash where keys are element names and values are styles (compatible with pastel).

    Supported element names:

    • :code, :comment, :delete, :em, :h1, :h2, :h3, :h4, :h5, :h6, :hr, :image, :link, :list, :note, :quote, :strong, :table
    TTY::Markdown.parse(markdown_string, theme: {link: :magenta, list: %i[magenta bold]})
  4. Configure symbols with :symbols

    master

    By default, the output uses Unicode symbols. Use the :symbols option to switch to an ASCII set or to override specific symbols.

    Usage patterns:

    • Switch to ASCII: symbols: :ascii
    • Override a specific symbol base: symbols: {base: :ascii}
    • Override a specific symbol: symbols: {override: {bullet: "x"}}

    Common symbol mappings:

    NameASCIIUnicode
    :arrow->»
    :bullet*`●
    :pipe|│
    :line-─
    TTY::Markdown.parse(markdown_string, symbols: :ascii)
  5. Render blockquotes

    master

    Use the > character to create blockquotes. You can include standard Markdown formatting (like italics or bold) inside the blockquote.

    > Blockquotes are very handy in email to emulate reply text.
    > This line is part of the same quote.
    > *Oh*, you can put **Markdown** into a blockquote.
  6. Render tables

    master

    TTY::Markdown supports Markdown tables with alignment controls. Use pipes | for columns and dashes - for the header separator. Alignment is controlled by colons in the separator row: :--- for left, :---: for center, and ---: for right.

    | Tables   |      Are      |  Cool |
    |----------|:-------------:|------:|
    | col 1 is | left-aligned  | $1600 |
    | col 2 is | centered      | $12   |
    | col 3 is | right-aligned | $1    |