Skip to content

Commit 5e9d3d8

Browse files
committed
Auto merge of rust-lang#106561 - GuillaumeGomez:warning-block, r=rustdoc
Add warning block support in rustdoc Fixes rust-lang#79710. You can test it [here](https://rustdoc.crud.net/imperio/warning-block/foo/struct.Foo.html). It currently looks like this: ![image](https://user-images.githubusercontent.com/3050060/211413494-e1cf04e4-c081-4a9d-97db-27329405cfa7.png) So a few things to note: * Since it's a new add and it's changing the UI, we'll need to go through an FCP. * Does the UI looks good? * Is the way picked to add a warning block ok for everyone? The discussion on the issue seemed to be in favour of this solution but it doesn't hurt to double-check. cc `@rust-lang/rustdoc`
2 parents c40cfcf + 1d42913 commit 5e9d3d8

File tree

7 files changed

+96
-3
lines changed

7 files changed

+96
-3
lines changed

src/doc/rustdoc/src/how-to-write-documentation.md

+13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ characters:
254254

255255
So, no need to manually enter those Unicode characters!
256256

257+
### Adding a warning block
258+
259+
If you want to make a warning or similar note stand out in the documentation,
260+
you can wrap it like this:
261+
262+
```md
263+
/// documentation
264+
///
265+
/// <div class="warning">A big warning!</div>
266+
///
267+
/// more documentation
268+
```
269+
257270
[`backtrace`]: https://docs.rs/backtrace/0.3.50/backtrace/
258271
[commonmark markdown specification]: https://commonmark.org/
259272
[commonmark quick reference]: https://commonmark.org/help/

src/librustdoc/html/static/css/rustdoc.css

+23-3
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ ul ul, ol ul, ul ol, ol ol {
270270
margin-bottom: .625em;
271271
}
272272

273-
p {
273+
p, .docblock > .warning {
274274
/* Paragraph spacing at least 1.5 times line spacing per Web Content Accessibility Guidelines.
275275
Line-height is 1.5rem, so line spacing is .5rem; .75em is 1.5 times that.
276276
https://www.w3.org/WAI/WCAG21/Understanding/visual-presentation.html */
277277
margin: 0 0 .75em 0;
278278
}
279279
/* For the last child of a div, the margin will be taken care of
280280
by the margin-top of the next item. */
281-
p:last-child {
281+
p:last-child, .docblock > .warning:last-child {
282282
margin: 0;
283283
}
284284

@@ -1096,7 +1096,7 @@ pre.rust .doccomment {
10961096
}
10971097

10981098
.example-wrap.ignore .tooltip {
1099-
color: var(--codeblock-ignore-color);
1099+
color: var(--codeblock-ignore-color);
11001100
}
11011101

11021102
.example-wrap.compile_fail:hover .tooltip,
@@ -1124,6 +1124,26 @@ pre.rust .doccomment {
11241124
font-size: 1.25rem;
11251125
}
11261126

1127+
/* This class only exists for users who want to draw attention to a particular element in their
1128+
documentation. */
1129+
.content .docblock .warning {
1130+
border-left: 2px solid var(--warning-border-color);
1131+
padding: 14px;
1132+
position: relative;
1133+
/* The "!important" part is required because the rule is otherwise overruled in this CSS
1134+
selector: ".docblock > :not(.more-examples-toggle):not(.example-wrap)" */
1135+
overflow-x: visible !important;
1136+
}
1137+
.content .docblock .warning::before {
1138+
color: var(--warning-border-color);
1139+
content: "ⓘ";
1140+
position: absolute;
1141+
left: -25px;
1142+
top: 5px;
1143+
font-weight: bold;
1144+
font-size: 1.25rem;
1145+
}
1146+
11271147
a.test-arrow {
11281148
visibility: hidden;
11291149
position: absolute;

src/librustdoc/html/static/css/themes/ayu.css

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Original by Dempfi (https://github.com/dempfi/ayu)
3131
--codeblock-error-color: rgba(255, 0, 0, .5);
3232
--codeblock-ignore-hover-color: rgb(255, 142, 0);
3333
--codeblock-ignore-color: rgba(255, 142, 0, .6);
34+
--warning-border-color: rgb(255, 142, 0);
3435
--type-link-color: #ffa0a5;
3536
--trait-link-color: #39afd7;
3637
--assoc-item-link-color: #39afd7;

src/librustdoc/html/static/css/themes/dark.css

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
--codeblock-error-color: rgba(255, 0, 0, .5);
2727
--codeblock-ignore-hover-color: rgb(255, 142, 0);
2828
--codeblock-ignore-color: rgba(255, 142, 0, .6);
29+
--warning-border-color: rgb(255, 142, 0);
2930
--type-link-color: #2dbfb8;
3031
--trait-link-color: #b78cf2;
3132
--assoc-item-link-color: #d2991d;

src/librustdoc/html/static/css/themes/light.css

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
--codeblock-error-color: rgba(255, 0, 0, .5);
2727
--codeblock-ignore-hover-color: rgb(255, 142, 0);
2828
--codeblock-ignore-color: rgba(255, 142, 0, .6);
29+
--warning-border-color: rgb(255, 142, 0);
2930
--type-link-color: #ad378a;
3031
--trait-link-color: #6e4fc9;
3132
--assoc-item-link-color: #3873ad;

tests/rustdoc-gui/src/test_docs/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ impl Foo {
6565
pub fn must_use(&self) -> bool {
6666
true
6767
}
68+
69+
/// hello
70+
///
71+
/// <div id="doc-warning-1" class="warning">this is a warning</div>
72+
///
73+
/// done
74+
pub fn warning1() {}
75+
76+
/// Checking there is no bottom margin if "warning" is the last element.
77+
///
78+
/// <div id="doc-warning-2" class="warning">this is a warning</div>
79+
pub fn warning2() {}
6880
}
6981

7082
impl AsRef<str> for Foo {

tests/rustdoc-gui/warning-block.goml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test to check that the "warning blocks" are displayed as expected.
2+
go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
3+
show-text: true
4+
5+
define-function: (
6+
"check-warning",
7+
(theme, color, border_color, background_color),
8+
block {
9+
set-local-storage: {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}
10+
reload:
11+
12+
// The IDs are added directly into the DOM to make writing this test easier.
13+
assert-css: ("#doc-warning-1", {
14+
"margin-bottom": "12px",
15+
"color": |color|,
16+
"border-left": "2px solid " + |border_color|,
17+
"background-color": |background_color|,
18+
})
19+
assert-css: ("#doc-warning-2", {
20+
"margin-bottom": "0px",
21+
"color": |color|,
22+
"border-left": "2px solid " + |border_color|,
23+
"background-color": |background_color|,
24+
})
25+
},
26+
)
27+
28+
call-function: ("check-warning", {
29+
"theme": "ayu",
30+
"color": "rgb(197, 197, 197)",
31+
"border_color": "rgb(255, 142, 0)",
32+
"background_color": "rgba(0, 0, 0, 0)",
33+
})
34+
call-function: ("check-warning", {
35+
"theme": "dark",
36+
"color": "rgb(221, 221, 221)",
37+
"border_color": "rgb(255, 142, 0)",
38+
"background_color": "rgba(0, 0, 0, 0)",
39+
})
40+
call-function: ("check-warning", {
41+
"theme": "light",
42+
"color": "rgb(0, 0, 0)",
43+
"border_color": "rgb(255, 142, 0)",
44+
"background_color": "rgba(0, 0, 0, 0)",
45+
})

0 commit comments

Comments
 (0)