Skip to content

Commit 47eba20

Browse files
committed
Refactor examples
1 parent 7ff3f73 commit 47eba20

File tree

3 files changed

+14
-51
lines changed

3 files changed

+14
-51
lines changed

examples/lib.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,7 @@
11
fn main() -> Result<(), markdown::message::Message> {
2-
// Turn on debugging.
3-
// You can show it with `RUST_LOG=debug cargo run --features log --example lib`
4-
env_logger::init();
5-
6-
// Safely turn (untrusted?) markdown into HTML.
7-
println!("{:?}", markdown::to_html("## Hello, *world*!"));
8-
9-
// Turn trusted markdown into HTML.
10-
println!(
11-
"{:?}",
12-
markdown::to_html_with_options(
13-
"<div style=\"color: tomato\">\n\n# Hello, tomato!\n\n</div>",
14-
&markdown::Options {
15-
compile: markdown::CompileOptions {
16-
allow_dangerous_html: true,
17-
allow_dangerous_protocol: true,
18-
..markdown::CompileOptions::default()
19-
},
20-
..markdown::Options::default()
21-
}
22-
)
23-
);
24-
25-
// Support GFM extensions.
26-
println!(
27-
"{}",
28-
markdown::to_html_with_options(
29-
"* [x] [email protected] ~~strikethrough~~",
30-
&markdown::Options::gfm()
31-
)?
32-
);
33-
34-
// Access syntax tree and support MDX extensions:
352
println!(
363
"{:?}",
37-
markdown::to_mdast(
38-
"# <HelloMessage />, {username}!",
39-
&markdown::ParseOptions::mdx()
40-
)?
4+
markdown::to_mdast("# Hi *Earth*!", &markdown::ParseOptions::default())?
415
);
426

437
Ok(())

readme.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ cargo add [email protected]
120120

121121
```rs
122122
fn main() {
123-
println!("{}", markdown::to_html("## Hello, *world*!"));
123+
println!("{}", markdown::to_html("## Hi, *Saturn*! 🪐"));
124124
}
125125
```
126126

127127
Yields:
128128

129129
```html
130-
<h2>Hello, <em>world</em>!</h2>
130+
<h2>Hi, <em>Saturn</em>! 🪐</h2>
131131
```
132132

133133
Extensions (in this case GFM):
@@ -137,7 +137,7 @@ fn main() -> Result<(), markdown::message::Message> {
137137
println!(
138138
"{}",
139139
markdown::to_html_with_options(
140-
"* [x] contact@example.com ~~strikethrough~~",
140+
"* [x] contact ~Mercury~Venus at [email protected]!",
141141
&markdown::Options::gfm()
142142
)?
143143
);
@@ -152,8 +152,7 @@ Yields:
152152
<ul>
153153
<li>
154154
<input checked="" disabled="" type="checkbox" />
155-
156-
<del>strikethrough</del>
155+
contact <del>Mercury</del>Venus at <a href="mailto:[email protected]">[email protected]</a>!
157156
</li>
158157
</ul>
159158
```
@@ -164,7 +163,7 @@ Syntax tree ([mdast][]):
164163
fn main() -> Result<(), markdown::message::Message> {
165164
println!(
166165
"{:?}",
167-
markdown::to_mdast("# Hey, *you*!", &markdown::ParseOptions::default())?
166+
markdown::to_mdast("# Hi *Earth*!", &markdown::ParseOptions::default())?
168167
);
169168

170169
Ok(())
@@ -174,7 +173,7 @@ fn main() -> Result<(), markdown::message::Message> {
174173
Yields:
175174

176175
```text
177-
Root { children: [Heading { children: [Text { value: "Hey, ", position: Some(1:3-1:8 (2-7)) }, Emphasis { children: [Text { value: "you", position: Some(1:9-1:12 (8-11)) }], position: Some(1:8-1:13 (7-12)) }, Text { value: "!", position: Some(1:13-1:14 (12-13)) }], position: Some(1:1-1:14 (0-13)), depth: 1 }], position: Some(1:1-1:14 (0-13)) }
176+
Root { children: [Heading { children: [Text { value: "Hi ", position: Some(1:3-1:6 (2-5)) }, Emphasis { children: [Text { value: "Earth", position: Some(1:7-1:12 (6-11)) }], position: Some(1:6-1:13 (5-12)) }, Text { value: "!", position: Some(1:13-1:14 (12-13)) }], position: Some(1:1-1:14 (0-13)), depth: 1 }], position: Some(1:1-1:14 (0-13)) }
178177
```
179178

180179
## API

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ use alloc::string::String;
8484
/// ```
8585
/// use markdown::to_html;
8686
///
87-
/// assert_eq!(to_html("# Hello, world!"), "<h1>Hello, world!</h1>");
87+
/// assert_eq!(to_html("# Hi Mercury!"), "<h1>Hi Mercury!</h1>");
8888
/// ```
8989
pub fn to_html(value: &str) -> String {
9090
to_html_with_options(value, &Options::default()).unwrap()
@@ -107,12 +107,12 @@ pub fn to_html(value: &str) -> String {
107107
/// # fn main() -> Result<(), markdown::message::Message> {
108108
///
109109
/// // Use GFM:
110-
/// let result = to_html_with_options("~hi~hello!", &Options::gfm())?;
110+
/// let result = to_html_with_options("~Venus~Mars!", &Options::gfm())?;
111111
///
112-
/// assert_eq!(result, "<p><del>hi</del>hello!</p>");
112+
/// assert_eq!(result, "<p><del>Venus</del>Mars!</p>");
113113
///
114114
/// // Live dangerously / trust the author:
115-
/// let result = to_html_with_options("<div>\n\n# Hello, world!\n\n</div>", &Options {
115+
/// let result = to_html_with_options("<div>\n\n# Hi Jupiter!\n\n</div>", &Options {
116116
/// compile: CompileOptions {
117117
/// allow_dangerous_html: true,
118118
/// allow_dangerous_protocol: true,
@@ -121,7 +121,7 @@ pub fn to_html(value: &str) -> String {
121121
/// ..Options::default()
122122
/// })?;
123123
///
124-
/// assert_eq!(result, "<div>\n<h1>Hello, world!</h1>\n</div>");
124+
/// assert_eq!(result, "<div>\n<h1>Hi Jupiter!</h1>\n</div>");
125125
/// # Ok(())
126126
/// # }
127127
/// ```
@@ -150,10 +150,10 @@ pub fn to_html_with_options(value: &str, options: &Options) -> Result<String, me
150150
/// use markdown::{to_mdast, ParseOptions};
151151
/// # fn main() -> Result<(), markdown::message::Message> {
152152
///
153-
/// let tree = to_mdast("# Hey, *you*!", &ParseOptions::default())?;
153+
/// let tree = to_mdast("# Hi *Earth*!", &ParseOptions::default())?;
154154
///
155155
/// println!("{:?}", tree);
156-
/// // => Root { children: [Heading { children: [Text { value: "Hey, ", position: Some(1:3-1:8 (2-7)) }, Emphasis { children: [Text { value: "you", position: Some(1:9-1:12 (8-11)) }], position: Some(1:8-1:13 (7-12)) }, Text { value: "!", position: Some(1:13-1:14 (12-13)) }], position: Some(1:1-1:14 (0-13)), depth: 1 }], position: Some(1:1-1:14 (0-13)) }
156+
/// // => Root { children: [Heading { children: [Text { value: "Hi ", position: Some(1:3-1:6 (2-5)) }, Emphasis { children: [Text { value: "Earth", position: Some(1:7-1:12 (6-11)) }], position: Some(1:6-1:13 (5-12)) }, Text { value: "!", position: Some(1:13-1:14 (12-13)) }], position: Some(1:1-1:14 (0-13)), depth: 1 }], position: Some(1:1-1:14 (0-13)) }
157157
/// # Ok(())
158158
/// # }
159159
/// ```

0 commit comments

Comments
 (0)