Skip to content

WriterOptions

The WriterOptions struct controls how the Markdown output is formatted by CommonMarkWriter. It provides a way to customize various aspects of the generated CommonMark text.

Creating Options

The recommended way to create options is using the builder pattern:

rust
use cmark_writer::options::WriterOptionsBuilder;

let options = WriterOptionsBuilder::new()
    .soft_break("\n")
    .hard_break("  \n")
    .list_marker('*')
    .code_fence_char('`')
    .build();

Available Options

Text Formatting

OptionDescriptionDefaultExample
soft_breakString to use for soft line breaks"\n""\n"
hard_breakString to use for hard line breaks" \n"" \n"
html_escapeWhether to escape HTML special characterstruetrue

List Formatting

OptionDescriptionDefaultExample
list_markerCharacter to use for unordered lists'-''*', '-', '+'
ordered_list_markerCharacter to use after numbers in ordered lists'.''.', ')'

Code Blocks

OptionDescriptionDefaultExample
code_fence_charCharacter to use for code fences'`''`', '~'
code_fence_lengthNumber of fence characters33, 4

Spacing

OptionDescriptionDefaultExample
heading_spacingWhether to add a space after heading markerstrue# Heading vs #Heading
list_item_spacingWhether to add a space after list markerstrue- Item vs -Item

Tables (GFM)

These options are available when the gfm feature is enabled:

OptionDescriptionDefaultExample
table_cell_paddingMinimum number of spaces for table cell padding1`
align_table_pipesWhether to align table pipes verticallytrueAligned vs non-aligned pipes

Advanced Options

OptionDescriptionDefaultExample
preserve_reference_definitionsWhether to preserve reference definitions when writingtrue[ref]: url
end_with_newlineWhether to ensure output ends with a newlinetrueDocument with trailing newline

Usage Examples

Customizing List Formatting

rust
use cmark_writer::{Node, CommonMarkWriter, options::WriterOptionsBuilder};

// Create options with asterisks for unordered lists
let options = WriterOptionsBuilder::new()
    .list_marker('*')
    .build();

let list = Node::UnorderedList(vec![/* ... */]);
let mut writer = CommonMarkWriter::with_options(options);
writer.write(&list).expect("Failed to write list");

// Result will use asterisks instead of hyphens:
// * Item 1
// * Item 2

Customizing Code Blocks

rust
use cmark_writer::{Node, CommonMarkWriter, options::WriterOptionsBuilder};

// Create options with tildes for code fences
let options = WriterOptionsBuilder::new()
    .code_fence_char('~')
    .code_fence_length(4)
    .build();

let code_block = Node::code_block(Some("rust".to_string()), "fn main() {}".to_string());
let mut writer = CommonMarkWriter::with_options(options);
writer.write(&code_block).expect("Failed to write code block");

// Result will use tildes instead of backticks:
// ~~~~rust
// fn main() {}
// ~~~~

GFM Table Formatting

When the gfm feature is enabled:

rust
use cmark_writer::{Node, CommonMarkWriter, options::WriterOptionsBuilder};

// Create options for table formatting
let options = WriterOptionsBuilder::new()
    .table_cell_padding(2)  // More spacing in cells
    .align_table_pipes(true)
    .build();

let table = /* ... */;
let mut writer = CommonMarkWriter::with_options(options);
writer.write(&table).expect("Failed to write table");

// Result will have additional padding in table cells
// |  Header 1  |  Header 2  |
// |------------|------------|
// |  Data 1    |  Data 2    |

Default Options

The default options are designed to produce clean, widely-compatible Markdown:

rust
use cmark_writer::options::{WriterOptions, WriterOptionsBuilder};

// These are equivalent:
let default1 = WriterOptions::default();
let default2 = WriterOptionsBuilder::new().build();