#markdown-parser #pest-parser #transformer

mdtrans

Markdown parser and transformer using pest.rs, focused on flexibility to a project’s needs

5 releases

0.1.5 Mar 25, 2024
0.1.4 Feb 7, 2024
0.1.3 Jan 23, 2024

#5 in #transformer

Download history 16/week @ 2024-01-12 8/week @ 2024-01-19 1/week @ 2024-01-26 5/week @ 2024-02-02 2/week @ 2024-02-16 4/week @ 2024-02-23 4/week @ 2024-03-01 100/week @ 2024-03-22 33/week @ 2024-03-29 5/week @ 2024-04-05

138 downloads per month

GPL-3.0-only

42KB
962 lines

Mdtrans

Markdown parser and transformer, in Rust, using Pest

This is not the fastest parser / transformer, but it's built in order to provide the most flexibility to your needs.

Usage

#[derive(Default)]
pub struct MyOwnTransformer {
   image_count: usize,
   image_trans: usize,
}

impl MarkdownTransformer for MyOwnTransformer {
    fn peek_image(&mut self, alt: String, url: String, add_tags: HashMap<String, String>) {
        self.image_count_total += 1;
    }
    fn tranform_image(&mut self, alt: String, url: String, add_tags: HashMap<String, String>) -> String {
        self.image_trans += 1;
        format!("Image {}/{} <img alt=\"{alt}\" href=\"{url}\">", self.image_trans, self.image_count)
    }
}

fn main() {
    let trans = MyOwnTransformer::default();
    let input = " ... ".to_string();
    let output = transform_markdown_string(input, &mut trans).unwrap();
    println!("{output}");
}

Inside the transform_markdown_string function, the transformer will perform all peek functions before.
This means that in this code we first count the total number of images, and then transform each one of them.
The result will be something like:

Image 1/2 <img alt="toto" href="url">
Image 2/2 <img alt="tutu" href="url">

For an example of Markdown-to-HTML implementation, see this file
For the definition of the trait itself, see this file

Contribute

This is a hobby side-project, but you can contribute if you feel like it !

  • Contributions on the pest grammar file are appreciated as I'm really not an expert in it
  • You can open an issue if you find some Markdown inputs that are not supported (or not well) by this engine, or causes bugs.

Dependencies

~2.2–3MB
~59K SLoC