Skip to content

Learn Rust by implementing a tiny Markdown formatter.

License

Notifications You must be signed in to change notification settings

yszk0123/md-fmt

Repository files navigation

Rust

md-fmt

Learn Rust by implementing a tiny Markdown formatter.

Example

(WIP)

CLI

hello-world.md

# hello
world!
# Format
$ md-fmt hello-world.md --write
# JSON
$ md-fmt hello-world.md --json | jq '.body[] | select(.type == "Section").value.title'
"hello"

Rust

use anyhow::Result;
use mdfmt_core::{format, parse, Block, Section};

fn main() -> Result<()> {
    let note = mdfmt_core::parse("# foo")?;
    for block in note.body {
        match block {
            Block::Section(Section { title, .. }) => {
                println!("title: {title}");
            },
            _ => {},
        }
    }

    let formatted = format("# foo")?;
    println!("formatted: {}", formatted);
}

TypeScript

import { format, parse } from 'mdfmt-js';

const note = parse('# Hello, world!');
for (const block of note.body) {
  switch (block.type) {
    case 'Section': {
      console.log(`title: ${block.title}`);
      break;
    }
    default: {
      break;
    }
  }
}

const formatted = format('# Hello, world!');
console.log(`formatted: ${formatted}`);