Crate fmtm_ytmimi_markdown_fmt

Crate fmtm_ytmimi_markdown_fmt 

Source
Expand description

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);

Re-exports§

pub use crate::list::ListMarker;
pub use crate::list::OrderedListMarker;
pub use crate::list::ParseListMarkerError;
pub use crate::list::UnorderedListMarker;

Modules§

list
List marker types.

Structs§

Config
Configuration options for the Markdown formatter.
FnFormatter
A convenience function-based formatter. Implement a single function FormatterFn::format and set it as the generic parameter F to create a ExternalFormatter.
MarkdownFormatter
Used to format Markdown inputs.
Paragraph
A buffer where we write text
PreservingBuffer
A buffer where we write HTML blocks. Preserves everything as is.
TrimTo4Indent
A buffer that trims each line’s leading spaces down to a multiple of 4.

Enums§

BufferType
Type of the string being written to a ExternalFormatter.
FormatterCombination
A convenience combination of external formatters implementing ExternalFormatter, using one ExternalFormatter for each of code block (C), display math (D), HTML block (H), and paragraph (P) formatting.
FormattingContext
Type of the formatting context an ExternalFormatter is in.

Traits§

ExternalFormatter
A formatter buffer we write non-Markdown string into.
FormatterFn
A formatting function F that takes the buffer type, optional maximum width, and the string to format, and returns the formatted string.

Type Aliases§

DefaultFormatterCombination
A default ExternalFormatter. Preserve code blocks as is, trim indentation < 4 in display math and HTML blocks, and line-wrap paragraphs.