Skip to content

Commit 66336c3

Browse files
committed
Move admonition div definition to a common spot
This avoids duplicating some of the HTML code.
1 parent e45f7f0 commit 66336c3

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

mdbook-spec/src/admonitions.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ static ADMONITION_RE: LazyLock<Regex> = LazyLock::new(|| {
1111
Regex::new(r"(?m)^ *> \[!(?<admon>[^]]+)\]\n(?<blockquote>(?: *>.*\n)+)").unwrap()
1212
});
1313

14-
1514
// This icon is from GitHub, MIT License, see https://github.com/primer/octicons
1615
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>"#;
1716

@@ -38,16 +37,28 @@ pub fn admonitions(chapter: &Chapter, diag: &mut Diagnostics) -> String {
3837
let blockquote = &caps["blockquote"];
3938
let initial_spaces = blockquote.chars().position(|ch| ch != ' ').unwrap_or(0);
4039
let space = &blockquote[..initial_spaces];
41-
if lower.starts_with("edition-") {
42-
let edition = &lower[8..];
43-
return format!("{space}<div class=\"alert alert-edition\">\n\
40+
41+
let format_div = |class, content| {
42+
format!(
43+
"{space}<div class=\"alert alert-{class}\">\n\
4444
\n\
4545
{space}> <p class=\"alert-title\">\
46-
<span class=\"alert-title-edition\">{edition}</span> Edition differences</p>\n\
46+
{content}</p>\n\
4747
{space} >\n\
4848
{blockquote}\n\
4949
\n\
50-
{space}</div>\n");
50+
{space}</div>\n",
51+
)
52+
};
53+
54+
if lower.starts_with("edition-") {
55+
let edition = &lower[8..];
56+
return format_div(
57+
"edition",
58+
format!(
59+
"<span class=\"alert-title-edition\">{edition}</span> Edition differences"
60+
),
61+
);
5162
}
5263

5364
let svg = match lower.as_str() {
@@ -62,17 +73,13 @@ pub fn admonitions(chapter: &Chapter, diag: &mut Diagnostics) -> String {
6273
""
6374
}
6475
};
65-
format!(
66-
"{space}<div class=\"alert alert-{lower}\">\n\
67-
\n\
68-
{space}> <p class=\"alert-title\">\
69-
<svg viewBox=\"0 0 16 16\" width=\"18\" height=\"18\">\
76+
format_div(
77+
&lower,
78+
format!(
79+
"<svg viewBox=\"0 0 16 16\" width=\"18\" height=\"18\">\
7080
{svg}\
71-
</svg>{term}</p>\n\
72-
{space} >\n\
73-
{blockquote}\n\
74-
\n\
75-
{space}</div>\n",
81+
</svg>{term}"
82+
),
7683
)
7784
})
7885
.to_string()

0 commit comments

Comments
 (0)