#markdown #formatter #common-mark

fmtm_ytmimi_markdown_fmt

Fork of @ytmimi's Markdown formatter; powers FMTM

3 releases

0.0.3 May 29, 2024
0.0.2 May 29, 2024
0.0.1 May 28, 2024

#1341 in Text processing

Download history 15/week @ 2024-09-21 1/week @ 2024-09-28

109 downloads per month
Used in fmtm

MIT license

130KB
2.5K SLoC

FMTM: fork of @ytmimi's markdown-fmt

This is a dependency of FMTM, the diff-friendly Markdown formatter. Therefore, it needs to be published on crates.io.


lib.rs:

Easily format Markdown. fmtm_ytmimi_markdown_fmt supports CommonMark and GitHub Flavored Markdown.

Getting Started

use fmtm_ytmimi_markdown_fmt::MarkdownFormatter;

let markdown = r##" # Getting Started
1. numbered lists
1.  are easy!
"##;

let expected = r##"# Getting Started
1. numbered lists
1. are easy!
"##;

let output = MarkdownFormatter::default().format(markdown)?;
assert_eq!(output, expected);

Using MarkdownFormatter as a builder

The formatter gives you control to configure Markdown formatting.

use fmtm_ytmimi_markdown_fmt::*;
#[derive(Default)]
struct CodeBlockFormatter;
impl FormatterFn for CodeBlockFormatter {
    fn format(
        &mut self,
        buffer_type: BufferType,
        _max_width: Option<usize>,
        input: String,
    ) -> String {
        let BufferType::CodeBlock { info } = buffer_type else {
            unreachable!();
        };
        match info {
            Some(info) if info.as_ref() == "markdown" => {
                MarkdownFormatter::default().format(&input).unwrap_or(input)
            }
            _ => input,
        }
    }
}

let input = r##" # Using the Builder
+ markdown code block nested in a list
  ```markdown
  A nested markdown snippet!

   * unordered lists
   are also pretty easy!
   - `-` or `+` can also be used as unordered list markers.
   ```
"##;

let expected = r##"# Using the Builder
- markdown code block nested in a list
    ```markdown
    A nested markdown snippet!

    * unordered lists
      are also pretty easy!
    - `-` or `+` can also be used as unordered list markers.
    ```
"##;

type MyFormatter = MarkdownFormatter<
    FormatterCombination<
        FnFormatter<CodeBlockFormatter>,
        TrimTo4Indent,
        TrimTo4Indent,
        Paragraph,
    >,
>;
let output =
    MyFormatter::with_config_and_external_formatter(Config::sichanghe_opinion()).format(input)?;
assert_eq!(output, expected);

Dependencies

~3.5MB
~61K SLoC