Convert a markdown document or text into a terminal friendly output.
TTY::Markdown provides independent markdown processing component for TTY toolkit.
Add this line to your application's Gemfile:
gem 'tty-markdown'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tty-markdown
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
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:
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:
A mardown link:
[I'm an inline-style link](https://www.google.com)
will be rendered with actual link content next to it:
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:
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:
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:
You can specify a horizontal rule in markdown:
***
and then transform it:
parsed = TTY::Markdown.parse(markdown_string)
puts parsed
will output:
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.
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: { ... })
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.
By default formatting will include various Unicode symbols. You can switch to an included ASCII set and/or override individually with the :symbols
key:
TTY::Markdown.parse(markdown_string, symbols: :ascii)
TTY::Markdown.parse(markdown_string, symbols: {base: :ascii})
TTY::Markdown.parse(markdown_string, symbols: {override: {bullet: "x"}})
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.
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.
The gem is available as open source under the terms of the MIT License.
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.