Important
This parser uses not the most ideal grammar, so it is recommended to avoid using any complex or ambiguous combinations of styles, etc. For example, if you use bold and italic styles at the same time, then it is recommended to use underscores (_italic_
) for italic styling.
Crates.io: click here Github: click here
This is a Rust library that parses Markdown text, covering essential Markdown syntax elements such as headers, lists, emphasis, links, code blocks, and more. It parses Markdown into an Abstract Syntax Tree (AST), making it easier to manipulate, transform, or render Markdown content in various formats.
- Headers - Parses Markdown headers (
#
,##
,###
, etc.) into structured nodes in the AST. - Emphasis - Recognizes italic and bold text, along with other emphasis markers.
- Links - Parses inline links (
[text](url)
) and reference links. - Code Blocks - Detects inline code (
code
) and fenced code blocks. - Blockquotes - Parses quoted text (
> Quote
) as distinct elements. - Images - Recognizes inline images (
![alt text](url)
). - Horizontal Rule - Detects horizontal rules
---
in your file.
- Lists - Support for both ordered (
1. Item
) and unordered (- Item or * Item
) lists. - Tables - Recognition of tables with rows and columns.
- Footnotes - Support for footnotes, allowing references in the text and corresponding notes at the bottom.
- Task List - Parsing of task lists, with checkboxes (e.g.,
- [ ] Task or - [x] Done
). - Emoji - Recognition of shortcodes for emojis (e.g.,
:smile:
) and converting them to the appropriate Unicode or image representation. - Highlighted Text - Support for highlighted text (e.g., using
==highlighted==
). - Subscript - Parsing of subscript text (e.g.,
H~2~O
). - Superscript - Parsing of superscript text (e.g.,
X^2^
). - Definition Lists - Parsing of definition lists.
- Markdown extensions - Support for extended Markdown features like GitHub-flavored Markdown (GFM), including task lists, strikethrough, and more.
The parser processes Markdown into an Abstract Syntax Tree, which can be used for rendering Markdown as HTML, analyzing document structure, or exporting to other formats or editing a markdown file.
markdown = { SOI ~ (block ~ empty_line*)* ~ EOI }
block = _{
heading
| quote
| code_block
| horizontal_rule
| paragraph
}
- The file starts with the Start of Input (
SOI
) and ends with the End of Input (EOI
). - Markdown documents are composed of blocks separated by zero or more empty lines.
heading = _{
heading1
| heading2
| heading3
}
heading1 = {
"#" ~ ws ~ single_line_text ~ NEWLINE?
}
heading2 = {
"##" ~ ws ~ single_line_text ~ NEWLINE?
}
heading3 = {
"###" ~ ws ~ single_line_text ~ NEWLINE?
}
single_line_text = {
(!NEWLINE ~ ANY)+
}
- Represented using one or more
#
symbols at the start of the line. - One
#
corresponds to Heading 1, two##
to Heading 2, and three###
to Heading 3. - Must be followed by a space and a single line of text.
- Example:
# Heading 1
## Heading 2
### Heading 3
horizontal_rule = {
("---"|"***"|"–––") ~ ws* ~ (NEWLINE | EOI)
}
- Created with three or more of the following symbols:
- Dashes (
---
) - Asterisks (
***
) - En-dashes (
–––
)
- Dashes (
- Can optionally include trailing whitespace and must end with a newline.
- Example:
---
***
–––
quote = {
">" ~ paragraph
}
- Indicated by a
>
character followed by a paragraph. - Example:
> This is a quote. Hello!
code_block = {
"```" ~ (code_lang ~ ws* ~ NEWLINE)? ~ code_content ~ NEWLINE? ~ "```" ~ NEWLINE?
}
code_lang = {
ws* ~ ('a'..'z' | 'A'..'Z')+
}
code_content = {
(!(NEWLINE? ~ "```") ~ ANY)+
}
- Start and end with three backticks (
```
). - May optionally include a programming language name after the opening backticks.
- Example:
```py
print("Hello World!")
```
paragraph = {
paragraph_line+
}
paragraph_line = {
text+ ~ paragraph_break?
}
paragraph_break = _{
NEWLINE
}
text = _{
plain_text
| escaped
| styled_text
}
- Consist of one or more paragraph lines.
- Paragraphs are separated by an empty line or a paragraph break (newline).
styled_text = _{
escaped* ~ (bold | underline | italic | strikethrough | inline_image | inline_link | content) ~ escaped*
}
strikethrough = {
"~~" ~ (styled_text)+ ~ "~~"
}
underline = {
"__" ~ (styled_text)+ ~ "__"
}
bold = {
"**" ~ (styled_text)+ ~ "**"
}
italic = {
("*" ~ (styled_text)+ ~ "*")
| ("_" ~ (styled_text)+ ~ "_")
}
content = @{
(!(exclude_styles | exclude_block_elems) ~ ANY)+
}
- Bold: Enclosed in double asterisks (
**
). - Italic: Enclosed in single asterisks (
*
) or underscores (_
). - Underline: Enclosed in double underscores (
__
). - Strikethrough: Enclosed in double tildes (
~~
). - Content contains text which those elements are styling, used as plain text within styled elements.
inline_link = {
"[" ~ link_text ~ "](" ~ url ~ ")"
}
link_text = {
(!"]" ~ ANY)+
}
url = {
(!")" ~ ANY)+
}
- Formatted as
[link text](url)
.
inline_image = {
"![" ~ alt_text ~ "](" ~ url ~ ")"
}
alt_text = {
(!"]" ~ ANY)+
}
url = {
(!")" ~ ANY)+
}
- Formatted as
![alt text](url)
.
escaped = {
"\\" ~ (!ws ~ char)
}
char = {
ANY
}
- Special characters can be escaped using a backslash (
\
).
plain_text = @{
!exclude_block_elems ~ (!exclude_styles ~ ANY)+
}
- Any text not enclosed by styled elements or part of block elements.
empty_line = {
NEWLINE
}
- Represented by a single newline (
\n
).
Note
More additional rules and their description can be found in the src/grammar.pest
!
You can add this project as a dependency to your Rust project by fetching it from crates.io.
- Open the folder of your desired project in a terminal.
- Use
cargo add
to add the crate to your project's dependencies:
$ cargo add rins_markdown_parser
- Import crate in any
.rs
file inside your project:
// any .rs file, e.g. main.rs
use rins_markdown_parser::{Grammar, parse_to_console}
Alternatively, you can use this project as a standalone command-line interface (CLI). To do so:
- Clone the repository:
$ git clone https://github.com/r-rin/rins-markdown-parser.git
$ cd rins-markdown-parser
- Build the project:
$ cargo build --release
The compiled binary will be located in the target/release directory.
- Run the CLI to parse a Markdown file:
$ ./target/release/rins-markdown-parser help
or use make
$ make run args="..."
Crate provides various utilities for parsing Markdown text and converting it into HTML. Below are examples and explanations of how to use the provided functions.
You can use the str_to_html
function to parse Markdown text from a string and convert it to HTML.
use rins_markdown_parser::{str_to_html, ErrorParse};
fn main() -> Result<(), ErrorParse> {
let markdown_text = "# Hello, World!\nThis is **bold** and *italic*.";
let html_lines = str_to_html(markdown_text)?;
for line in html_lines {
println!("{}", line);
}
Ok(())
}
Output:
<h1>Hello, World!</h1>
<p>This is <strong>bold</strong> and <em>italic</em>.</p>
Use the md_to_html_file function
to convert a Markdown file into an HTML file.
use rins_markdown_parser::{md_to_html_file, ErrorParse};
use std::path::Path;
fn main() -> Result<(), ErrorParse> {
let markdown_path = Path::new("example.md");
let html_path = Path::new("example.html");
md_to_html_file(markdown_path, html_path)?;
println!("Markdown converted to HTML successfully!");
Ok(())
}
The parse_to_console
function allows you to parse Markdown text and print the resulting HTML directly to the console.
use rins_markdown_parser::parse_to_console;
fn main() {
let markdown_text = r#"
# Welcome
This is **\*bold _bold and italic_** text!
"#;
if let Err(err) = parse_to_console(markdown_text) {
println!("Error: {}", err);
}
}
If you need to parse only specific parts of the Markdown using custom rules defined in grammar.pest
, use the parse_by_rule
function.
use rins_markdown_parser::{parse_by_rule, Grammar, Rule, ErrorParse};
fn main() -> Result<(), ErrorParse> {
let markdown_text = "## Subheading\nSome text here.";
let pairs = parse_by_rule(Rule::heading2, markdown_text)?;
for pair in pairs {
println!("Parsed pair: {:?}", pair);
}
Ok(())
}
The rins_markdown_parser
provides a Command Line Interface (CLI) to interact with the markdown parser. You can use it to parse markdown files or text into HTML or view project credits.
If you have cloned the project, build it using Cargo:
$ cargo build --release
This will create an executable in the target/release
directory. Alternatively, if you installed it as a binary crate, you can directly use rins_markdown_parser
.
To see the available commands, use the --help
option or help
subcommand:
$ rins_markdown_parser --help
Output:
rins_markdown_parser vX.X.X
Allows to interact with markdown parser via a Command Line Interface.
Usage: rins_markdown_parser [COMMAND]
Commands:
parse Parses provided markdown text and returns it in html format
credits Displays credits and project information
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
parse
The parse
command is used to convert Markdown text to HTML. It accepts input either from a file or directly as text.
Options
-I, --in <input_file>
Specifies the location of the input markdown file.
-O, --out <output_file>
Specifies the location where the HTML output will be saved. If not provided, the result is printed to the console.
-t, --text <markdown_text>
Accepts Markdown text directly from the CLI.
Note
This option conflicts with --in and --out.
Examples
- Parse a Markdown file and save the output to an HTML file:
$ rins_markdown_parser parse --in example.md --out example.html
- Parse Markdown text directly from the CLI:
$ rins_markdown_parser parse --text "# Hello World\nThis is **Markdown**."
credits
Displays project information and credits.
$ rins_markdown_parser credits
help [COMMAND]
Displays helpful information about available subcommands and their arguments.
This project is intended solely for personal and educational use. It was never intented to be used at production. Use with caution.
- Author: r-rin
- This parser was developed as part of the Rust Programming Language course at NaUKMA with the support of the Ukrainian Rust community.