Skip to content

Commit e7db18f

Browse files
committed
Rework admonition rendering.
This changes the admonitions to render pretty much the same way that GitHub does it.
1 parent 9bbea41 commit e7db18f

File tree

3 files changed

+89
-36
lines changed

3 files changed

+89
-36
lines changed

docs/authoring.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ Admonitions use a style similar to GitHub-flavored markdown, where the style nam
167167
```markdown
168168
> [!WARNING]
169169
> This is a warning.
170+
171+
> [!NOTE]
172+
> This is a note.
170173
```
171174

172-
All this does is apply a CSS class to the blockquote. You should define the color or style of the rule in the [`theme/reference.css`](https://github.com/rust-lang/reference/blob/master/theme/reference.css) file if it isn't already defined.
175+
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/lib.rs`](https://github.com/rust-lang/reference/blob/HEAD/mdbook-spec/src/lib.rs).
173176

174177
## Style
175178

mdbook-spec/src/lib.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,38 @@ impl Spec {
144144
/// > ...
145145
/// ```
146146
///
147-
/// This will add a `<div class="warning">` around the blockquote so that
148-
/// it can be styled differently. Any text between the brackets that can
149-
/// be a CSS class is valid. The actual styling needs to be added in a CSS
150-
/// file.
151-
fn admonitions(&self, chapter: &Chapter) -> String {
147+
/// This will add a `<div class="alert alert-warning">` around the
148+
/// blockquote so that it can be styled differently, and injects an icon.
149+
/// The actual styling needs to be added in the `reference.css` CSS file.
150+
fn admonitions(&self, chapter: &Chapter, diag: &mut Diagnostics) -> String {
152151
ADMONITION_RE
153152
.replace_all(&chapter.content, |caps: &Captures<'_>| {
154153
let lower = caps["admon"].to_lowercase();
155154
let term = to_initial_case(&caps["admon"]);
156155
let blockquote = &caps["blockquote"];
157156
let initial_spaces = blockquote.chars().position(|ch| ch != ' ').unwrap_or(0);
158157
let space = &blockquote[..initial_spaces];
158+
// These icons are from GitHub, MIT License, see https://github.com/primer/octicons
159+
let svg = match lower.as_str() {
160+
"note" => "<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>",
161+
"warning" => "<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>",
162+
_ => {
163+
warn_or_err!(
164+
diag,
165+
"admonition `{lower}` in {:?} is incorrect or not yet supported",
166+
chapter.path.as_ref().unwrap()
167+
);
168+
""
169+
}
170+
};
159171
format!(
160-
"{space}<div class=\"{lower}\">\n\
172+
"{space}<div class=\"alert alert-{lower}\">\n\
161173
\n\
162-
{space}> ***{term}:***\n\
174+
{space}> <p class=\"alert-title\">\
175+
<svg viewBox=\"0 0 16 16\" width=\"18\" height=\"18\">\
176+
{svg}\
177+
</svg>{term}</p>\n\
178+
{space} >\n\
163179
{blockquote}\n\
164180
\n\
165181
{space}</div>\n",
@@ -226,7 +242,7 @@ impl Preprocessor for Spec {
226242
if ch.is_draft_chapter() {
227243
return;
228244
}
229-
ch.content = self.admonitions(&ch);
245+
ch.content = self.admonitions(&ch, &mut diag);
230246
ch.content = self.auto_link_references(&ch, &rules);
231247
ch.content = self.render_rule_definitions(&ch.content, &tests, &git_ref);
232248
if ch.name == "Test summary" {

theme/reference.css

+61-27
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,81 @@ the parenthetical. So for this example, you'd use
1111
}
1212

1313
/*
14-
Warnings are defined using admonitions in blockquotes:
14+
Admonitions are defined with blockquotes:
1515
1616
> [!WARNING]
1717
> This is bad!
1818
19+
See mdbook-spec/src/lib.rs.
1920
*/
20-
main .warning blockquote {
21-
padding: 0px;
22-
}
23-
24-
main .warning blockquote p {
25-
padding: 0px 20px;
21+
.alert blockquote {
22+
/* Add some padding to make the vertical bar a little taller than the text.*/
23+
padding: 8px 0px 8px 20px;
24+
/* Add a solid color bar on the left side. */
25+
border-inline-start-style: solid;
26+
border-inline-start-width: 4px;
27+
/* Disable the background color from mdbook for a cleaner look. */
28+
background-color: inherit;
29+
/* Disable border blocks from mdbook. */
30+
border-block-start: none;
31+
border-block-end: none;
32+
/* Reduce margin from mdbook, it uses a lot of space. */
2633
margin: 10px 0;
2734
}
2835

29-
main .warning blockquote p:first-child::before {
30-
content: "⚠️ ";
36+
.alert-title {
37+
/* Slightly increase the weight for more emphasis. */
38+
font-weight: 600;
39+
/* Vertically center the icon with the text. */
40+
display: flex;
41+
align-items: center;
42+
/* Remove default large margins for a more compact display.
43+
Important to override .alert p rule. */
44+
margin: 0 0 8px 0 !important;
3145
}
32-
33-
.light main .warning blockquote,
34-
.rust main .warning blockquote {
35-
border: 2px solid red;
36-
background: #ffcece;
46+
.alert blockquote > :nth-child(2) {
47+
/* Default margins of content just below the label add too much space. */
48+
margin-top: 0;
3749
}
38-
39-
.rust main .warning blockquote {
40-
/* overrides previous declaration */
41-
border-color: #961717;
50+
.alert blockquote > :last-child {
51+
/* Default margins of content add too much space. */
52+
margin-bottom: 0;
4253
}
4354

44-
.coal main .warning blockquote,
45-
.navy main .warning blockquote,
46-
.ayu main .warning blockquote {
47-
background: #542626;
55+
.alert-title svg {
56+
fill: currentColor;
57+
/* Add some space between the icon and the text. */
58+
margin-right: 8px;
4859
}
4960

50-
/* Make the links higher contrast on dark themes */
51-
.coal main .warning blockquote p a,
52-
.navy main .warning blockquote p a,
53-
.ayu main .warning blockquote p a {
54-
color: #80d0d0;
61+
.light .alert {
62+
--alert-note-color: #0969da;
63+
--alert-warning-color: #9a6700;
64+
}
65+
.ayu .alert {
66+
--alert-note-color: #74b9ff;
67+
--alert-warning-color: #f0b72f;
68+
}
69+
.rust .alert {
70+
--alert-note-color: #023b95;
71+
--alert-warning-color: #603700;
72+
}
73+
.coal .alert,
74+
.navy .alert {
75+
--alert-note-color: #4493f8;
76+
--alert-warning-color: #d29922;
77+
}
78+
.alert-note blockquote {
79+
border-inline-start-color: var(--alert-note-color);
80+
}
81+
.alert-warning blockquote {
82+
border-inline-start-color: var(--alert-warning-color);
83+
}
84+
.alert-note .alert-title {
85+
color: var(--alert-note-color);
86+
}
87+
.alert-warning .alert-title {
88+
color: var(--alert-warning-color);
5589
}
5690

5791
/* <kbd> tags can be used to highlight specific character elements. */

0 commit comments

Comments
 (0)