tty logo

TTY::Markdown Gitter

Gem Version Build Status Build status Maintainability Coverage Status Inline docs

Convert a markdown document or text into a terminal friendly output.

TTY::Markdown provides independent markdown processing component for TTY toolkit.

Installation

Add this line to your application's Gemfile:

gem 'tty-markdown'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tty-markdown

Contents

1. Usage

Using parse method, you can transform a markdown string into a terminal formatted content:

parsed = TTY::Markdown.parse("# Hello")
puts parsed
# => "\e[36;1mHello\e[0m\n"

The parse_file allows you to transform a markdown document into a terminal formatted output:

parsed = TTY::Markdown.parse_file('example.md')
puts parsed

1.1 Header

TTY::Markdown
=============

**tty-markdown** converts markdown document into a terminal friendly output.

## Examples

### Nested list items

and transforming it:

parsed = TTY::Markdown.parse(markdown_string)

puts parsed will output:

Code highlight

1.2 List

Both numbered and unordered lists are supported. Given a markdown:

- Item 1
  - Item 2
  - Item 3
    - Item 4
- Item 5

and transforming it:

parsed = TTY::Markdown.parse(markdown_string)

puts parsed will produce:

Code highlight

A mardown link:

[I'm an inline-style link](https://www.google.com)

will be rendered with actual link content next to it:

Code highlight

1.4 Blockquote

Given a markdown quote:

> 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.

and transforming it:

parsed = TTY::Markdown.parse(markdown_string)

puts parsed will output:

Code highlight

1.5 Code and Syntax Highlighting

The parser can highlight syntax of many programming languages. Given the markdown codeblock with language specification:

```ruby
class Greeter
  def hello(name)
    puts "Hello #{name}"
  end
end
```

and converting this snippet:

parsed = TTY::Markdown.parse(code_snippet)

puts parsed will produce:

Code highlight

1.6 Table

You can transform tables which understand the markdown alignment.

For example, given the following table:

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

and transforming it:

parsed = TTY::Markdown.parse(markdown_string)

puts parsed will output:

Code highlight

1.7 Horizontal Rule

You can specify a horizontal rule in markdown:

***

and then transform it:

parsed = TTY::Markdown.parse(markdown_string)

puts parsed will output:

Code highlight

2. Options

2.1 :colors

By default the 256 colors scheme is used to render code block elements. You can change that by specifying desired number of colors:

TTY::Markdown.pasre(markdown_string, colors: 16)

This feauture may be handy when working in terminals with limited color support. By default, TTY::Markdown detects your terminal color mode and adjusts output automatically.

2.2 :theme

A hash of styles that allows to customize specific elements of the markdown text. By default the following styles are used:

THEME = {
  em: :yellow,
  header: [:cyan, :bold],
  hr: :yellow,
  link: [:yellow, :underline],
  list: :yellow,
  strong: [:yellow, :bold],
  table: :yellow,
  quote: :yellow,
}

In order to provide new styles use :theme key:

TTY::Markdown.parse(markdown_string, theme: { ... })

2.3 :width

You can easily control the maximum width of the output by using the :width key:

TTY::Markdown.parse(markdown_string, width: 80)

By default the terminal screen width is used.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-markdown. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the TTY::Markdown project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright (c) 2018 Piotr Murach. See LICENSE for further details.