Install TTY::Markdown
masterYou can install tty-markdown using one of the following methods:
Using Bundler (Recommended for applications):
Add this to your Gemfile:
gem "tty-markdown"Then run:
bundleUsing Gem:
gem install tty-markdownrepository·master·Indexed 43 days ago
https://github.com/piotrmurach/tty-markdownA 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.
You can install tty-markdown using one of the following methods:
Using Bundler (Recommended for applications):
Add this to your Gemfile:
gem "tty-markdown"Then run:
bundleUsing Gem:
gem install tty-markdownThe :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)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)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, :tableTTY::Markdown.parse(markdown_string, theme: {link: :magenta, list: %i[magenta bold]})Use the :width option to control the maximum width of the output. If not specified, the terminal screen width is used by default.
TTY::Markdown.parse(markdown_string, width: 80)By default, content (except for the main h1 header) is indented by 2 spaces. Use the :indent option to provide a custom number of spaces or to disable indentation entirely.
TTY::Markdown.parse(markdown_string, indent: 0)By default, the output uses Unicode symbols. Use the :symbols option to switch to an ASCII set or to override specific symbols.
Usage patterns:
symbols: :asciisymbols: {base: :ascii}symbols: {override: {bullet: "x"}}Common symbol mappings:
| Name | ASCII | Unicode |
|---|---|---|
:arrow | -> | » |
:bullet | * | `● |
:pipe | | | │ |
:line | - | ─ |
TTY::Markdown.parse(markdown_string, symbols: :ascii)To use TTY::Markdown in your Ruby project, require the library. It is designed to parse Markdown text and convert it into formatted strings containing ANSI escape sequences for terminal output (e.g., converting **bold** to [1mbold[0m).
require "tty-markdown"Standard Markdown inline links are supported, including links with optional titles.
An [inline-style link](https://ttytoolkit.org)
An [inline-style link with title](https://ttytoolkit.org "TTY Toolkit Homepage")$$.$$5+5$$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.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 |