Skip to content

Commit

Permalink
Add support for serializing mdast as markdown
Browse files Browse the repository at this point in the history
Closes GH-64.
Closes GH-127.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
bnchi authored Oct 7, 2024
1 parent a2ef756 commit 567e7e0
Show file tree
Hide file tree
Showing 66 changed files with 8,324 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
components: rustfmt, clippy
- run: cargo fmt --check && cargo clippy --examples --tests --benches --all-features
- run: cargo test --all-features
- run: cargo clippy -p mdast_util_to_markdown
- run: cargo test -p mdast_util_to_markdown
coverage:
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ keywords = ["commonmark", "markdown", "parse", "render", "tokenize"]
categories = ["compilers", "encoding", "parser-implementations", "parsing", "text-processing"]
include = ["src/", "license"]

[workspace]
members = ["generate", "mdast_util_to_markdown"]

[workspace.dependencies]
pretty_assertions = "1"

[[bench]]
name = "bench"
path = "benches/bench.rs"
Expand All @@ -31,14 +37,11 @@ serde = { version = "1", features = ["derive"], optional = true }
[dev-dependencies]
env_logger = "0.11"
criterion = "0.5"
pretty_assertions = "1"
pretty_assertions = { workspace = true }
serde_json = { version = "1" }
swc_core = { version = "0.100", features = [
"ecma_ast",
"ecma_visit",
"ecma_parser",
"common",
] }

[workspace]
members = ["generate"]
12 changes: 12 additions & 0 deletions mdast_util_to_markdown/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "mdast_util_to_markdown"
version = "0.0.0"
edition = "2018"
license = "MIT"

[dependencies]
markdown = { path = "../" }
regex = { version = "1" }

[dev-dependencies]
pretty_assertions = { workspace = true }
37 changes: 37 additions & 0 deletions mdast_util_to_markdown/src/association.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use alloc::string::String;
use markdown::mdast::{Definition, ImageReference, LinkReference};

pub trait Association {
fn identifier(&self) -> &String;
fn label(&self) -> &Option<String>;
}

impl Association for Definition {
fn identifier(&self) -> &String {
&self.identifier
}

fn label(&self) -> &Option<String> {
&self.label
}
}

impl Association for ImageReference {
fn identifier(&self) -> &String {
&self.identifier
}

fn label(&self) -> &Option<String> {
&self.label
}
}

impl Association for LinkReference {
fn identifier(&self) -> &String {
&self.identifier
}

fn label(&self) -> &Option<String> {
&self.label
}
}
77 changes: 77 additions & 0 deletions mdast_util_to_markdown/src/configure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
pub struct Options {
/// Marker to use for bullets of items in unordered lists ('*', '+', or '-', default: '*').
pub bullet: char,
// Marker to use in certain cases where the primary bullet doesn’t work
// ('*', '+', or '-', default: '-' when bullet is '*', '*' otherwise).
pub bullet_other: char,
/// Marker to use for bullets of items in ordered lists ('.' or ')', default: '.').
pub bullet_ordered: char,
/// Marker to use for emphasis ('*' or '_', default: '*').
pub emphasis: char,
// Marker to use for fenced code ('`' or '~', default: '`').
pub fence: char,
/// Whether to use fenced code always (bool, default: true). The default is to use fenced code
/// if there is a language defined, if the code is empty, or if it starts or ends in blank lines.
pub fences: bool,
// How to indent the content of list items (default: 'IndentOptions::One').
pub list_item_indent: IndentOptions,
/// Marker to use for titles ('"' or "'", default: '"').
pub quote: char,
/// Marker to use for thematic breaks ('*', '-', or '_', default: '*').
pub rule: char,
// Marker to use for strong ('*' or '_', default: '*').
pub strong: char,
// Whether to increment the counter of ordered lists items (bool, default: true).
pub increment_list_marker: bool,
/// Whether to add the same number of number signs (#) at the end of an ATX heading as the
/// opening sequence (bool, default: false).
pub close_atx: bool,
/// Whether to always use resource links (bool, default: false). The default is to use autolinks
/// (<https://example.com>) when possible and resource links ([text](url)) otherwise.
pub resource_link: bool,
/// Whether to add spaces between markers in thematic breaks (bool, default: false).
pub rule_spaces: bool,
/// Whether to use setext headings when possible (bool, default: false). The default is to always
/// use ATX headings (# heading) instead of setext headings (heading\n=======). Setext headings
/// cannot be used for empty headings or headings with a rank of three or more.
pub setext: bool,
/// Whether to join definitions without a blank line (bool, default: false).
pub tight_definitions: bool,
// Number of markers to use for thematic breaks (u32, default: 3, min: 3).
pub rule_repetition: u32,
}

#[derive(Copy, Clone)]
pub enum IndentOptions {
// Depends on the item and its parent list uses 'One' if the item and list are tight and 'Tab'
// otherwise.
Mixed,
// The size of the bullet plus one space.
One,
/// Tab stop.
Tab,
}

impl Default for Options {
fn default() -> Self {
Self {
bullet: '*',
bullet_other: '-',
bullet_ordered: '.',
emphasis: '*',
fence: '`',
fences: true,
increment_list_marker: true,
rule_repetition: 3,
list_item_indent: IndentOptions::One,
quote: '"',
rule: '*',
strong: '*',
close_atx: false,
rule_spaces: false,
resource_link: false,
setext: false,
tight_definitions: false,
}
}
}
Loading

0 comments on commit 567e7e0

Please sign in to comment.