Convert a Markdown document or text into a terminal friendly output.
TTY::Markdown provides a Markdown processing component for the 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
Parsing the following Markdown headers:
TTY::Markdown
=============
**tty-markdown** converts markdown document into a terminal friendly output.
## Examples
### Nested list items
The terminal output looks like this:
Both numbered and unordered lists are supported. Given a Markdown:
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
The parsed output looks like this:
Given a definition list:
Item 1
: This is the description for Item 1
Item 2
: This is the description for Item 2
: This is another description for Item 2
The parsed output looks like this:
A Markdown link:
[An inline-style link](https://ttytoolkit.org)
[An inline-style link with title](https://ttytoolkit.org "TTY Toolkit Homepage")
The link text will be rendered with the link 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.
The rendered output looks like this:
The parser can highlight syntax of many programming languages.
Given a Markdown codeblock with a language specification:
```ruby
class Greeter
def hello(name)
puts "Hello #{name}"
end
end
```
The terminal output will look like this:
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 |
Then the terminal output will look like this:
You can specify a horizontal rule in Markdown:
***
and then transform it:
parsed = TTY::Markdown.parse(markdown_string)
puts parsed
will output:
You can create footnote references:
It is not down on any map[^foo]; true places[^bar] never are.
[^foo]: A diagrammatic representation of an area of land or sea.
[^bar]: A particular position, point, or area in space; a location.
All footnotes will be displayed with a sequential number and rendered in the terminal like this:
By default the 256
color scheme is used to render code block elements.
Use the :mode
keyword to specify the maximum number of colors, for example,
to be 16
ANSI colors:
TTY::Markdown.parse(markdown_string, mode: 16)
This feature may be handy when working in terminals with limited color support.
By default, TTY::Markdown detects terminal color mode and adjusts output automatically.
Use the :theme
keyword to change specific Markdown element styles.
For example, to override styles for the link
and list
elements do:
TTY::Markdown.parse(markdown_string, theme: {link: :magenta, list: %i[magenta bold]})
Here's a complete list of element names with corresponding styles:
Name | Style |
---|---|
:code |
%i[yellow] |
:comment |
%i[bright_black] |
:delete |
%i[red] |
:em |
%i[yellow] |
:header |
%i[cyan bold] |
:heading1 |
%i[cyan bold underline] |
:hr |
%i[yellow] |
:image |
%i[bright_black] |
:link |
%i[yellow underline] |
:list |
%i[yellow] |
:note |
%i[yellow] |
:quote |
%i[yellow] |
:strong |
%i[yellow bold] |
:table |
%i[yellow] |
Read the pastel documentation for all supported styles.
Use the :width
keyword to control the maximum width of the output:
TTY::Markdown.parse(markdown_string, width: 80)
By default the terminal screen width is used.
By default, formatted output includes various Unicode symbols. Use the
:symbols
keyword to switch to an ASCII set and/or override individual
symbols.
TTY::Markdown.parse(markdown_string, symbols: :ascii)
TTY::Markdown.parse(markdown_string, symbols: {base: :ascii})
TTY::Markdown.parse(markdown_string, symbols: {override: {bullet: "x"}})
Here's a complete list of symbol names with corresponding ASCII and Unicode characters:
Name | ASCII | Unicode |
---|---|---|
:arrow |
-> |
» |
:bar |
| |
┃ |
:bottom_center |
+ |
┴ |
:bottom_left |
+ |
└ |
:bottom_right |
+ |
┘ |
:bracket_left |
[ |
[ |
:bracket_right |
] |
] |
:bullet |
* |
● |
:diamond |
* |
◈ |
:hash |
# |
# |
:hellip |
... |
… |
:laquo |
<< |
« |
:laquo_space |
<< |
« |
:ldquo |
" |
“ |
:lsquo |
" |
‘ |
:line |
- |
─ |
:mdash |
- |
— |
:mid_center |
+ |
┼ |
:mid_left |
+ |
├ |
:mid_right |
+ |
┤ |
:ndash |
- |
- |
:paren_left |
( |
( |
:paren_right |
) |
) |
:pipe |
| |
│ |
:raquo |
>> |
» |
:raquo_space |
>> |
» |
:rdquo |
" |
” |
:rsquo |
" |
’ |
:top_center |
+ |
┬ |
:top_left |
+ |
┌ |
:top_right |
+ |
┐ |
By default, any content apart from the main h1
header is indented with 2
spaces. Use the :indent
keyword to provide custom indent or no indent at all:
TTY::Markdown.parse(markdown_string, indent: 0)
Use the :color
keyword to control when to apply coloring to various document
elements. Valid values are :always
, :auto
or :never
. The default :auto
value automatically detects whether to color the output or not.
For example, to always color content regardless of terminal support:
TTY::Markdown.parse(markdown_string, color: :always)
Install the tty-markdown-cli
to use the tty-markdown
executable in the terminal:
$ tty-markdown README.md
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 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.txt for further details.