Skip to content

Commit ff34b15

Browse files
Use upstream mdBook admonitions where possible
Should have identical visual output as before
1 parent 18d4246 commit ff34b15

2 files changed

Lines changed: 30 additions & 48 deletions

File tree

theme/reference.css

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
--railroad-rect-fill: hsl(-290, 70%, 90%);
1212
}
1313
.light {
14-
--alert-note-color: #0969da;
15-
--alert-warning-color: #9a6700;
1614
--alert-edition-color: #1a7f37;
1715
--alert-example-color: #8250df;
1816
--grammar-literal-bg: #fafafa;
1917
}
2018
.rust {
21-
--alert-note-color: #023b95;
22-
--alert-warning-color: #603700;
2319
--alert-edition-color: #008200;
2420
--alert-example-color: #8250df;
2521
--grammar-literal-bg: #dedede;
@@ -29,15 +25,11 @@
2925
--inline-code-color: var(--grammar-comment-color);
3026
}
3127
.coal, .navy {
32-
--alert-note-color: #4493f8;
33-
--alert-warning-color: #d29922;
3428
--alert-edition-color: #3fb950;
3529
--alert-example-color: #ab7df8;
3630
--grammar-literal-bg: #1d1f21;
3731
}
3832
.ayu {
39-
--alert-note-color: #74b9ff;
40-
--alert-warning-color: #f0b72f;
4133
--alert-edition-color: #2bd853;
4234
--alert-example-color: #d3abff;
4335
--grammar-literal-bg: #191f26;
@@ -68,12 +60,18 @@ the parenthetical. So for this example, you'd use
6860
/*
6961
Admonitions are defined with blockquotes:
7062
71-
> [!WARNING]
63+
> [!EXAMPLE]
7264
> This is bad!
7365
74-
See tools/mdbook-spec/src/admonitions.rs.
66+
The admonitions from tools/mdbook-spec/src/admonitions.rs are custom ones for
67+
the reference (e.g. examples), upstream mdbook also provides some (like for
68+
`[!WARNING]`, use consistent styles for both).
69+
70+
Some of the styles specified below for the upstream admonitions already match
71+
the upstream styles but are included for consistency.
7572
*/
76-
.alert blockquote {
73+
.alert blockquote,
74+
blockquote.blockquote-tag {
7775
/* Add some padding to make the vertical bar a little taller than the text.*/
7876
padding: 8px 0px 8px 20px;
7977
/* Add a solid color bar on the left side. */
@@ -84,54 +82,40 @@ See tools/mdbook-spec/src/admonitions.rs.
8482
/* Disable border blocks from mdbook. */
8583
border-block-start: none;
8684
border-block-end: none;
87-
/* Reduce margin from mdbook, it uses a lot of space. */
88-
margin: 10px 0;
85+
/* Reduce margin from mdbook, it uses a lot of space.
86+
Important to override `main > ul > li > *:last-child` rule. */
87+
margin: 10px 0 !important;
8988
}
9089

91-
.alert-title {
92-
/* Slightly increase the weight for more emphasis. */
93-
font-weight: 600;
94-
/* Vertically center the icon with the text. */
95-
display: flex;
96-
align-items: center;
90+
.blockquote-tag-title {
9791
/* Remove default large margins for a more compact display.
9892
Important to override .alert p rule. */
9993
margin: 0 0 8px 0 !important;
10094
}
101-
.alert blockquote > :nth-child(2) {
95+
.alert blockquote > :nth-child(2),
96+
blockquote.blockquote-tag > :nth-child(2) {
10297
/* Default margins of content just below the label add too much space. */
10398
margin-top: 0;
10499
}
105-
.alert blockquote > :last-child {
100+
.alert blockquote > :last-child,
101+
blockquote.blockquote-tag > :last-child {
106102
/* Default margins of content add too much space. */
107103
margin-bottom: 0;
108104
}
109105

110-
.alert-title svg {
106+
.blockquote-tag-title svg {
111107
fill: currentColor;
112108
/* Add some space between the icon and the text. */
113109
margin-right: 8px;
114110
}
115111

116-
.alert-note blockquote {
117-
border-inline-start-color: var(--alert-note-color);
118-
}
119-
.alert-warning blockquote {
120-
border-inline-start-color: var(--alert-warning-color);
121-
}
122112
.alert-edition blockquote {
123113
border-inline-start-color: var(--alert-edition-color);
124114
}
125115
.alert-example blockquote {
126116
border-inline-start-color: var(--alert-example-color);
127117
}
128-
.alert-note .alert-title {
129-
color: var(--alert-note-color);
130-
}
131-
.alert-warning .alert-title {
132-
color: var(--alert-warning-color);
133-
}
134-
.alert-edition .alert-title {
118+
.alert-edition .blockquote-tag-title {
135119
color: var(--alert-edition-color);
136120
}
137121
/* Puts a rounded rectangle around the edition date. */
@@ -143,7 +127,7 @@ See tools/mdbook-spec/src/admonitions.rs.
143127
font-weight: bold;
144128
color: var(--alert-edition-color);
145129
}
146-
.alert-example .alert-title {
130+
.alert-example .blockquote-tag-title {
147131
color: var(--alert-example-color);
148132
}
149133

tools/mdbook-spec/src/admonitions.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@ use regex::{Captures, Regex};
1515
use std::sync::LazyLock;
1616

1717
/// The Regex for the syntax for blockquotes that have a specific CSS class,
18-
/// like `> [!WARNING]`.
18+
/// like `> [!EXAMPLE]`.
1919
static ADMONITION_RE: LazyLock<Regex> = LazyLock::new(|| {
2020
Regex::new(r"(?m)^ *> \[!(?<admon>[^]]+)\]\n(?<blockquote>(?: *>.*\n)+)").unwrap()
2121
});
2222

23-
// This icon is from GitHub, MIT License, see https://github.com/primer/octicons
24-
const ICON_NOTE: &str = r#"<path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path>"#;
25-
26-
// This icon is from GitHub, MIT License, see https://github.com/primer/octicons
27-
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>"#;
28-
2923
// This icon is from GitHub, MIT License, see https://github.com/primer/octicons
3024
const ICON_EXAMPLE: &str = r#"<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z"></path>"#;
3125

@@ -34,11 +28,11 @@ const ICON_EXAMPLE: &str = r#"<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a
3428
/// The blockquote should look something like:
3529
///
3630
/// ```markdown
37-
/// > [!WARNING]
31+
/// > [!EXAMPLE]
3832
/// > ...
3933
/// ```
4034
///
41-
/// This will add a `<div class="alert alert-warning">` around the
35+
/// This will add a `<div class="alert alert-example">` around the
4236
/// blockquote so that it can be styled differently, and injects an icon.
4337
/// The actual styling needs to be added in the `reference.css` CSS file.
4438
pub fn admonitions(chapter: &Chapter, diag: &mut Diagnostics) -> String {
@@ -54,7 +48,7 @@ pub fn admonitions(chapter: &Chapter, diag: &mut Diagnostics) -> String {
5448
format!(
5549
"{space}<div class=\"alert alert-{class}\">\n\
5650
\n\
57-
{space}> <p class=\"alert-title\">\
51+
{space}> <p class=\"blockquote-tag-title\">\
5852
{content}</p>\n\
5953
{space} >\n\
6054
{blockquote}\n\
@@ -73,9 +67,13 @@ pub fn admonitions(chapter: &Chapter, diag: &mut Diagnostics) -> String {
7367
);
7468
}
7569

70+
match lower.as_str() {
71+
"note" => return caps[0].to_string(),
72+
"warning" => return caps[0].to_string(),
73+
_ => (),
74+
};
75+
7676
let svg = match lower.as_str() {
77-
"note" => ICON_NOTE,
78-
"warning" => ICON_WARNING,
7977
"example" => ICON_EXAMPLE,
8078
_ => {
8179
warn_or_err!(

0 commit comments

Comments
 (0)