A Quick Guide To Markdown in Ruby Using Kramdown
In today’s programming environment, it?s vital to understand how to make sense of Markdown, a lightweight markup language that simplifies the writing of web-based content. This guide will walk you through using Markdown in Ruby, one of the most popular high-level programming languages. Our goal here is to help software engineers exploring Markdown in Ruby yield more power in generating and manipulating web content.
Table of Contents
- Prerequisite
- Understanding Markdown
- Markdown Usage in Ruby
- Hands-On Ruby Markdown Examples
- Final Thoughts
Prerequisite
Before getting started, let’s ensure you have Ruby installed. You can ascertain this by running this command in your command line:
ruby -v
The output should look something like this:
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-linux]
This means you have Ruby version 2.7.3 installed on your machine.
Understanding Markdown
Markdown language uses plain text syntax, allowing the writer to focus on content without being distracted by the formatting. Developers mainly use Markdown to draft readmes, documentation, and content for static site generators.
A typical markdown document might look like this:
# Heading 1
## Heading 2
### Heading 3
- Bulleted
- List
1. Numbered
2. List
**Bold** and _Italic_ and `Code` text
Markdown Usage in Ruby
Ruby has several gems (libraries) that simplify using Markdown. The most popular among these is kramdown, offering a fast and feature-rich conversion from Markdown to HTML.
To get started with kramdown for example, first, install the gem:
gem install kramdown
After the installation, you can use the Kramdown::Document
class to convert Markdown to HTML:
require 'kramdown'
markdown = "## Hello, World!"
puts Kramdown::Document.new(markdown).to_html
Running the above code should output this HTML content:
<h2>Hello, World!</h2>
Hands-On Ruby Markdown Examples
Now, let’s get into examples of using kramdown to convert Markdown content to HTML with Ruby. Note that these examples use the kramdown flavor of Markdown.
Example 1: Basic conversion
This is a simple conversion from Markdown to HTML.
require 'kramdown'
markdown_text = "# Hello Markdown World\n This is a **sample** text"
html = Kramdown::Document.new(markdown_text).to_html
puts html
When you run the above code, you can expect the following output:
<h1>Hello Markdown World</h1>
<p>This is a <strong>sample</strong> text</p>
Example 2: Markdown table to HTML
require 'kramdown'
table_md = "| First Header | Second Header |\n| ----- | ---- |\n| Content from cell 1 | Content from cell 2 |\n"
puts Kramdown::Document.new(table_md).to_html
The above code should produce this HTML output:
<table>
<thead>
<tr>
<th>First Header</th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content from cell 1</td>
<td>Content from cell 2</td>
</tr>
</tbody>
</table>
Final Thoughts
As software engineers, we are constantly seeking tools and practices that make our jobs easier and our work more efficient. Markdown, especially used in conjunction with Ruby, gives us a simple, intuitive way to create structured, web-ready text. With the power of kramdown library, we can convert Markdown to HTML in a breeze, thus enhancing our productivity. Happy coding!