discord-markdown

Parse discord-flavored markdown

crates.io 0.1.2 license

This parser was written for use with cheesecake, so the convertor function provided is designed for that. If this function doesn't suit your use-case, you can write your own convertor function to generate HTML from the parsed AST. The text in the generated HTML will be HTML-escaped, so you can safely insert the output into the DOM.

Documentation

Read the API documentation on docs.rs.

Usage

Call parser::parse on the input string, and it will return a vector of Expressions. Supply this vector to convertor::to_html to get an HTML string. If your input text will also have custom emoji, user mentions, role mentions, or channel mentions, then use convertor::to_html_with_callbacks instead.

Call parser::parse_with_md_hyperlinks instead if you want to also parse links with alt text, which is supported in discord embeds (Like [example](https://example.com))

Note:

Newlines are not converted to Expression::Newline inside code blocks, so that must be handled in the covertor.

Examples

If all you want is to generate the AST, it's really simple: ```rust use discord_markdown::parser::{parse, Expression::*};

fn main() { let ast = parse("> example formatted ||string||"); assert_eq!(ast, vec![ Blockquote(vec![ Italics(vec![ Bold(vec![Text("example")]), Text(" formatted"), ]), Text(" "), Spoiler(vec![Text("string")]), ]), ]); } ```

If you want to generate an HTML string, it's like this: ```rust use discord_markdown::{parser::parse, convertor::*};

fn dummycallback(x: &str) -> (String, Option) { (x.toowned(), None) }

fn idtoname(id: &str) -> (String, Option) { ( if id == "123456789123456789" {"member"} else {"unknown role"}.toowned(), Some("#ff0000".toowned()), ) }

fn main() { let html = tohtml(parse("> _example formatted ||string||")); assert_eq!(html, "

example formatted\ string
");

// With role mentions
let html = to_html_with_callbacks(
    parse("<@&123456789123456789>"),
    dummy_callback,
    dummy_callback,
    id_to_name,
    dummy_callback,
);
assert_eq!(html, "<div class=\"role\" style=\"color: #ff0000\">@member\
<span style=\"background-color: #ff0000\"></span></div>");

} ```

You can then add styling for .role and .role span in your stylesheet. Here's some example CSS: ```css .role { background-color: initial; display: inline-block; position: relative; word-break: keep-all; }

.role span { border-radius: 4px; height: 100%; width: 100%; opacity: .12; position: absolute; left: 0; top: 0; } ```