Skip to content

Commit f27c3ae

Browse files
authored
Merge pull request #2915 from ehuss/fontawesome-warning
Add a warning when a Font Awesome font is missing
2 parents 8670bcc + b3bd103 commit f27c3ae

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

crates/mdbook-html/src/html/tree.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -977,19 +977,29 @@ where
977977
new_classes += class;
978978
}
979979
}
980+
if icon.is_empty() {
981+
continue;
982+
}
980983

981-
if !icon.is_empty()
982-
&& let Ok(svg) = fa::svg(type_, &icon)
983-
{
984-
let mut span = Element::new("span");
985-
span.insert_attr("class", new_classes.into());
986-
for (name, value) in &i_el.attrs {
987-
if *name != attr_qual_name!("class") {
988-
span.attrs.insert(name.clone(), value.clone());
984+
match fa::svg(type_, &icon) {
985+
Ok(svg) => {
986+
let mut span = Element::new("span");
987+
span.insert_attr("class", new_classes.into());
988+
for (name, value) in &i_el.attrs {
989+
if *name != attr_qual_name!("class") {
990+
span.attrs.insert(name.clone(), value.clone());
991+
}
989992
}
993+
*node.value() = Node::Element(span);
994+
node.append(Node::RawData(svg.into()));
995+
}
996+
Err(e) => {
997+
warn!(
998+
"failed to find Font Awesome icon for icon `{icon}` \
999+
with type `{type_}` in `{}`: {e}",
1000+
self.options.path.display()
1001+
);
9901002
}
991-
*node.value() = Node::Element(span);
992-
node.append(Node::RawData(svg.into()));
9931003
}
9941004
}
9951005
}

tests/testsuite/rendering.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,17 @@ fn first_chapter_is_copied_as_index_even_if_not_first_elem() {
4747
// Fontawesome `<i>` tag support.
4848
#[test]
4949
fn fontawesome() {
50-
BookTest::from_dir("rendering/fontawesome").check_all_main_files();
50+
BookTest::from_dir("rendering/fontawesome")
51+
.run("build", |cmd| {
52+
cmd.expect_stderr(str![[r#"
53+
INFO Book building has started
54+
INFO Running the html backend
55+
WARN failed to find Font Awesome icon for icon `does-not-exist` with type `regular` in `fa.md`: Invalid Font Awesome icon name: visit https://fontawesome.com/icons?d=gallery&m=free to see valid names
56+
INFO HTML book written to `[ROOT]/book`
57+
58+
"#]]);
59+
})
60+
.check_all_main_files();
5161
}
5262

5363
// Tests the rendering when setting the default rust edition.

tests/testsuite/rendering/fontawesome/expected/fa.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ <h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>
22
<p><span class="fa-svg extra-class" id="example1"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M47.6 300.4L228.3 469.1c7.5 7 17.4 10.9 27.7 10.9s20.2-3.9 27.7-10.9L464.4 300.4c30.4-28.3 47.6-68 47.6-109.5v-5.8c0-69.9-50.5-129.5-119.4-141C347 36.5 300.6 51.4 268 84L256 96 244 84c-32.6-32.6-79-47.5-124.6-39.9C50.5 55.6 0 115.2 0 185.1v5.8c0 41.5 17.2 81.2 47.6 109.5z"/></svg></span></p>
33
<p><span class="fa-svg"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M272 304h-96C78.8 304 0 382.8 0 480c0 17.67 14.33 32 32 32h384c17.67 0 32-14.33 32-32C448 382.8 369.2 304 272 304zM48.99 464C56.89 400.9 110.8 352 176 352h96c65.16 0 119.1 48.95 127 112H48.99zM224 256c70.69 0 128-57.31 128-128c0-70.69-57.31-128-128-128S96 57.31 96 128C96 198.7 153.3 256 224 256zM224 48c44.11 0 80 35.89 80 80c0 44.11-35.89 80-80 80S144 172.1 144 128C144 83.89 179.9 48 224 48z"/></svg></span></p>
44
<p><span class="fa-svg"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M448 48V384C385 407 366 416 329 416C266 416 242 384 179 384C159 384 143 388 128 392V328C143 324 159 320 179 320C242 320 266 352 329 352C349 352 364 349 384 343V135C364 141 349 144 329 144C266 144 242 112 179 112C128 112 104 133 64 141V448C64 466 50 480 32 480S0 466 0 448V64C0 46 14 32 32 32S64 46 64 64V77C104 69 128 48 179 48C242 48 266 80 329 80C366 80 385 71 448 48Z"/></svg></span></p>
5-
<p><i class="fas fa-heart">Some text here.</i></p>
5+
<p><i class="fas fa-heart">Text prevents translation.</i></p>
6+
<p><i class="fa fa-does-not-exist"></i></p>

tests/testsuite/rendering/fontawesome/src/fa.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66

77
<i class="fab fa-font-awesome"></i>
88

9-
<i class="fas fa-heart">Some text here.</i>
9+
<i class="fas fa-heart">Text prevents translation.</i>
10+
11+
<i class="fa fa-does-not-exist"></i>

0 commit comments

Comments
 (0)