TTY::Markdown
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('example.md')
puts parsed
1.1 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:
1.2 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:
1.3 Quote
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:
1.4 Codeblock
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:
1.5 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:
2. Options
2.1 :theme
A hash of styles that allows to customize specific elements of the markdown text. By default the following styles are used:
THEME = {
em: :italic,
header: [:cyan, :bold],
hr: :yellow,
link: [:blue, :underline],
list: :yellow,
strong: [:yellow, :bold],
table: :blue,
quote: :yellow,
}
In order to provide new styles use :theme
key:
TTY::Markdown.parse(markdown_string, theme: { ... })
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
Copyright (c) 2018 Piotr Murach. See LICENSE for further details.