Skip to content

Commit f4736b5

Browse files
committed
Add an example admonition
This allows writing `> [!EXAMPLE]` to place an example in a block.
1 parent 66336c3 commit f4736b5

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

docs/authoring.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ Admonitions use a style similar to GitHub-flavored markdown, where the style nam
180180

181181
> [!NOTE]
182182
> This is a note.
183+
184+
> [!EXAMPLE]
185+
> This is an example.
183186
```
184187

185188
The color and styling is defined in [`theme/reference.css`](https://github.com/rust-lang/reference/blob/master/theme/reference.css) and the transformation and icons are in [`mdbook-spec/src/admonitions.rs`](https://github.com/rust-lang/reference/blob/HEAD/mdbook-spec/src/admonitions.rs).

mdbook-spec/src/admonitions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const ICON_NOTE: &str = r#"<path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5
1717
// This icon is from GitHub, MIT License, see https://github.com/primer/octicons
1818
const ICON_WARNING: &str = r#"<path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>"#;
1919

20+
// This icon is from GitHub, MIT License, see https://github.com/primer/octicons
21+
const ICON_EXAMPLE: &str = r#"<path d="M 5.0693987,1.2383101 11.496127,7.665038 a 0.47311013,0.47311013 0 0 1 0,0.669924 L 5.0693987,14.76169 A 0.47311013,0.47311013 0 0 1 4.2613266,14.426728 V 1.573272 A 0.47311013,0.47311013 0 0 1 5.0693987,1.2383101 Z"></path>"#;
22+
2023
/// Converts blockquotes with special headers into admonitions.
2124
///
2225
/// The blockquote should look something like:
@@ -64,6 +67,7 @@ pub fn admonitions(chapter: &Chapter, diag: &mut Diagnostics) -> String {
6467
let svg = match lower.as_str() {
6568
"note" => ICON_NOTE,
6669
"warning" => ICON_WARNING,
70+
"example" => ICON_EXAMPLE,
6771
_ => {
6872
warn_or_err!(
6973
diag,

src/introduction.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ These conventions are documented here.
7878
> [!NOTE]
7979
> This is an example note.
8080
81+
* Example blocks show an example that demonstrates some rule or points out some interesting aspect. Some examples may have hidden lines which can be viewed by clicking the eye icon that appears when hovering or tapping the example.
82+
83+
> [!EXAMPLE]
84+
> This is a code example.
85+
> ```rust
86+
> println!("hello world");
87+
> ```
88+
8189
* Warnings that show unsound behavior in the language or possibly confusing interactions of language features are in a special warning box.
8290
8391
> [!WARNING]

theme/reference.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,26 @@ See mdbook-spec/src/admonitions.rs.
6262
--alert-note-color: #0969da;
6363
--alert-warning-color: #9a6700;
6464
--alert-edition-color: #1a7f37;
65+
--alert-example-color: #8250df;
6566
}
6667
.ayu .alert {
6768
--alert-note-color: #74b9ff;
6869
--alert-warning-color: #f0b72f;
6970
--alert-edition-color: #2bd853;
71+
--alert-example-color: #d3abff;
7072
}
7173
.rust .alert {
7274
--alert-note-color: #023b95;
7375
--alert-warning-color: #603700;
7476
--alert-edition-color: #008200;
77+
--alert-example-color: #8250df;
7578
}
7679
.coal .alert,
7780
.navy .alert {
7881
--alert-note-color: #4493f8;
7982
--alert-warning-color: #d29922;
8083
--alert-edition-color: #3fb950;
84+
--alert-example-color: #ab7df8;
8185
}
8286
.alert-note blockquote {
8387
border-inline-start-color: var(--alert-note-color);
@@ -88,6 +92,9 @@ See mdbook-spec/src/admonitions.rs.
8892
.alert-edition blockquote {
8993
border-inline-start-color: var(--alert-edition-color);
9094
}
95+
.alert-example blockquote {
96+
border-inline-start-color: var(--alert-example-color);
97+
}
9198
.alert-note .alert-title {
9299
color: var(--alert-note-color);
93100
}
@@ -106,6 +113,9 @@ See mdbook-spec/src/admonitions.rs.
106113
font-weight: bold;
107114
color: var(--alert-edition-color);
108115
}
116+
.alert-example .alert-title {
117+
color: var(--alert-example-color);
118+
}
109119

110120
/* <kbd> tags can be used to highlight specific character elements. */
111121
kbd {

0 commit comments

Comments
 (0)