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::formatand set it as the generic parameterFto create aExternalFormatter. - Markdown
Formatter - Used to format Markdown inputs.
- Paragraph
- A buffer where we write text
- Preserving
Buffer - A buffer where we write HTML blocks. Preserves everything as is.
- Trim
To4Indent - A buffer that trims each line’s leading spaces down to a multiple of 4.
Enums§
- Buffer
Type - Type of the string being written to a
ExternalFormatter. - Formatter
Combination - A convenience combination of
external formatters implementing
ExternalFormatter, using oneExternalFormatterfor each of code block (C), display math (D), HTML block (H), and paragraph (P) formatting. - Formatting
Context - Type of the formatting context an
ExternalFormatteris in.
Traits§
- External
Formatter - A formatter buffer we write non-Markdown string into.
- Formatter
Fn - A formatting function
Fthat takes the buffer type, optional maximum width, and the string to format, and returns the formatted string.
Type Aliases§
- Default
Formatter Combination - A default
ExternalFormatter. Preserve code blocks as is, trim indentation < 4 in display math and HTML blocks, and line-wrap paragraphs.