Skip to content

Commit 567e7e0

Browse files
authored
Add support for serializing mdast as markdown
Closes GH-64. Closes GH-127. Reviewed-by: Titus Wormer <[email protected]>
1 parent a2ef756 commit 567e7e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+8324
-4
lines changed

.github/workflows/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
components: rustfmt, clippy
1414
- run: cargo fmt --check && cargo clippy --examples --tests --benches --all-features
1515
- run: cargo test --all-features
16+
- run: cargo clippy -p mdast_util_to_markdown
17+
- run: cargo test -p mdast_util_to_markdown
1618
coverage:
1719
runs-on: ubuntu-latest
1820
steps:

Cargo.toml

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ keywords = ["commonmark", "markdown", "parse", "render", "tokenize"]
1212
categories = ["compilers", "encoding", "parser-implementations", "parsing", "text-processing"]
1313
include = ["src/", "license"]
1414

15+
[workspace]
16+
members = ["generate", "mdast_util_to_markdown"]
17+
18+
[workspace.dependencies]
19+
pretty_assertions = "1"
20+
1521
[[bench]]
1622
name = "bench"
1723
path = "benches/bench.rs"
@@ -31,14 +37,11 @@ serde = { version = "1", features = ["derive"], optional = true }
3137
[dev-dependencies]
3238
env_logger = "0.11"
3339
criterion = "0.5"
34-
pretty_assertions = "1"
40+
pretty_assertions = { workspace = true }
3541
serde_json = { version = "1" }
3642
swc_core = { version = "0.100", features = [
3743
"ecma_ast",
3844
"ecma_visit",
3945
"ecma_parser",
4046
"common",
4147
] }
42-
43-
[workspace]
44-
members = ["generate"]

mdast_util_to_markdown/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "mdast_util_to_markdown"
3+
version = "0.0.0"
4+
edition = "2018"
5+
license = "MIT"
6+
7+
[dependencies]
8+
markdown = { path = "../" }
9+
regex = { version = "1" }
10+
11+
[dev-dependencies]
12+
pretty_assertions = { workspace = true }
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use alloc::string::String;
2+
use markdown::mdast::{Definition, ImageReference, LinkReference};
3+
4+
pub trait Association {
5+
fn identifier(&self) -> &String;
6+
fn label(&self) -> &Option<String>;
7+
}
8+
9+
impl Association for Definition {
10+
fn identifier(&self) -> &String {
11+
&self.identifier
12+
}
13+
14+
fn label(&self) -> &Option<String> {
15+
&self.label
16+
}
17+
}
18+
19+
impl Association for ImageReference {
20+
fn identifier(&self) -> &String {
21+
&self.identifier
22+
}
23+
24+
fn label(&self) -> &Option<String> {
25+
&self.label
26+
}
27+
}
28+
29+
impl Association for LinkReference {
30+
fn identifier(&self) -> &String {
31+
&self.identifier
32+
}
33+
34+
fn label(&self) -> &Option<String> {
35+
&self.label
36+
}
37+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
pub struct Options {
2+
/// Marker to use for bullets of items in unordered lists ('*', '+', or '-', default: '*').
3+
pub bullet: char,
4+
// Marker to use in certain cases where the primary bullet doesn’t work
5+
// ('*', '+', or '-', default: '-' when bullet is '*', '*' otherwise).
6+
pub bullet_other: char,
7+
/// Marker to use for bullets of items in ordered lists ('.' or ')', default: '.').
8+
pub bullet_ordered: char,
9+
/// Marker to use for emphasis ('*' or '_', default: '*').
10+
pub emphasis: char,
11+
// Marker to use for fenced code ('`' or '~', default: '`').
12+
pub fence: char,
13+
/// Whether to use fenced code always (bool, default: true). The default is to use fenced code
14+
/// if there is a language defined, if the code is empty, or if it starts or ends in blank lines.
15+
pub fences: bool,
16+
// How to indent the content of list items (default: 'IndentOptions::One').
17+
pub list_item_indent: IndentOptions,
18+
/// Marker to use for titles ('"' or "'", default: '"').
19+
pub quote: char,
20+
/// Marker to use for thematic breaks ('*', '-', or '_', default: '*').
21+
pub rule: char,
22+
// Marker to use for strong ('*' or '_', default: '*').
23+
pub strong: char,
24+
// Whether to increment the counter of ordered lists items (bool, default: true).
25+
pub increment_list_marker: bool,
26+
/// Whether to add the same number of number signs (#) at the end of an ATX heading as the
27+
/// opening sequence (bool, default: false).
28+
pub close_atx: bool,
29+
/// Whether to always use resource links (bool, default: false). The default is to use autolinks
30+
/// (<https://example.com>) when possible and resource links ([text](url)) otherwise.
31+
pub resource_link: bool,
32+
/// Whether to add spaces between markers in thematic breaks (bool, default: false).
33+
pub rule_spaces: bool,
34+
/// Whether to use setext headings when possible (bool, default: false). The default is to always
35+
/// use ATX headings (# heading) instead of setext headings (heading\n=======). Setext headings
36+
/// cannot be used for empty headings or headings with a rank of three or more.
37+
pub setext: bool,
38+
/// Whether to join definitions without a blank line (bool, default: false).
39+
pub tight_definitions: bool,
40+
// Number of markers to use for thematic breaks (u32, default: 3, min: 3).
41+
pub rule_repetition: u32,
42+
}
43+
44+
#[derive(Copy, Clone)]
45+
pub enum IndentOptions {
46+
// Depends on the item and its parent list uses 'One' if the item and list are tight and 'Tab'
47+
// otherwise.
48+
Mixed,
49+
// The size of the bullet plus one space.
50+
One,
51+
/// Tab stop.
52+
Tab,
53+
}
54+
55+
impl Default for Options {
56+
fn default() -> Self {
57+
Self {
58+
bullet: '*',
59+
bullet_other: '-',
60+
bullet_ordered: '.',
61+
emphasis: '*',
62+
fence: '`',
63+
fences: true,
64+
increment_list_marker: true,
65+
rule_repetition: 3,
66+
list_item_indent: IndentOptions::One,
67+
quote: '"',
68+
rule: '*',
69+
strong: '*',
70+
close_atx: false,
71+
rule_spaces: false,
72+
resource_link: false,
73+
setext: false,
74+
tight_definitions: false,
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)