mdx-formatter

Type to search...

to open search from anywhere

You are viewing documentation for an older version. View the latest version

Rust Crate

Using mdx-formatter-core as a Rust library

Rust Crate

The core formatting engine is available as a standalone Rust crate (mdx-formatter-core) for direct integration in Rust applications. It has no Node.js dependency and can be embedded in CLI tools, desktop apps (Tauri), or any Rust project.

Cargo Dependency

Since the crate is not yet published to crates.io, use one of these dependency forms:

[dependencies]
# Option 1: git dependency (Cargo finds the workspace member by crate name)
mdx-formatter-core = { git = "https://github.com/Takazudo/mdx-formatter" }

# Option 2: local path (for development)
mdx-formatter-core = { path = "../mdx-formatter/crates/mdx-formatter-core" }

Basic Usage

use mdx_formatter_core::{format, FormatterSettings};

let content = "# Hello\nSome text";
let settings = FormatterSettings::default();
let formatted = format(content, &settings);
// formatted == "# Hello\n\nSome text"

The format function runs a convergence loop (up to 3 iterations) to ensure idempotent output. It returns the formatted string directly — no Result wrapping, no async.

FormatterSettings

FormatterSettings mirrors the TypeScript settings structure. Each formatting rule is a separate field:

use mdx_formatter_core::FormatterSettings;

let mut settings = FormatterSettings::default();

// Disable the spacing rule
settings.add_empty_line_between_elements.enabled = false;

let formatted = mdx_formatter_core::format(content, &settings);

Available Settings Fields

FieldTypeDefaultDescription
add_empty_line_between_elementsAddEmptyLineBetweenElementsSettingenabledAdd empty lines between elements
format_multi_line_jsxFormatMultiLineJsxSettingenabledFormat multi-line JSX
format_html_blocks_in_mdxFormatHtmlBlocksInMdxSettingenabledFormat HTML blocks
expand_single_line_jsxExpandSingleLineJsxSettingdisabledExpand single-line JSX
indent_jsx_contentIndentJsxContentSettingdisabledIndent JSX content
add_empty_lines_in_block_jsxAddEmptyLinesInBlockJsxSettingenabledEmpty lines in block JSX
format_yaml_frontmatterFormatYamlFrontmatterSettingenabledFormat YAML frontmatter
preserve_admonitionsPreserveAdmonitionsSettingenabledPreserve Docusaurus admonitions
error_handlingErrorHandlingSettingoffThrow on parse errors
auto_detect_indentAutoDetectIndentSettingdisabledAuto-detect indentation

Loading from JSON

Settings can be created from a partial JSON value, merging with defaults:

use mdx_formatter_core::FormatterSettings;

let json: serde_json::Value = serde_json::json!({
    "addEmptyLineBetweenElements": { "enabled": false },
    "formatMultiLineJsx": { "indentSize": 4 }
});
let settings = FormatterSettings::from_partial_json(&json);

JSON field names use camelCase (matching the TypeScript/config file convention), while Rust struct fields use snake_case.

All 10 settings fields are supported via serde deserialization with camelCase JSON keys.

Public Exports

The crate re-exports through lib.rs:

SymbolModuleDescription
formatformatterThe main formatting function (panics on error if throw_on_error is true)
try_formatformatterReturns Result<String, String> instead of panicking
FormatterSettingstypesSettings struct
format_html_blockhtml_formatterHTML block indentation formatter
load_configconfigLoad and merge config from file
load_full_configconfigLoad config + exclude patterns
load_full_config_fromconfigLoad config from a specific directory
load_exclude_patternsconfigLoad only exclude patterns
FullConfigconfigSettings + exclude patterns struct

All symbols are re-exported at the crate root, so use mdx_formatter_core::{format, FormatterSettings} works directly. The parser, types, and html_formatter modules are also public for consumers who need lower-level access.

Status

The Rust crate implements all formatting rules from the TypeScript version. See Rust Rewrite for the full status and remaining infrastructure work.

Revision History