-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use embedded Font Awesome SVG icons #2484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9004849
Use embedded Font Awesome SVG icons
uncenter 39d7615
Add docs changes, fix fontawesome i tag parsing, remove extra templat…
uncenter 2ca9f04
Add tests for convert_fontawesome
uncenter 26a2d3a
Use brands type as fallback for older books
uncenter 905cce2
Accept arg to or_else closures
uncenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use font_awesome_as_a_crate as fa; | ||
use handlebars::{ | ||
Context, Handlebars, Helper, Output, RenderContext, RenderError, RenderErrorReason, | ||
}; | ||
use log::trace; | ||
use std::str::FromStr; | ||
|
||
pub fn fa_helper( | ||
h: &Helper<'_>, | ||
_r: &Handlebars<'_>, | ||
_ctx: &Context, | ||
_rc: &mut RenderContext<'_, '_>, | ||
out: &mut dyn Output, | ||
) -> Result<(), RenderError> { | ||
trace!("fa_helper (handlebars helper)"); | ||
|
||
let type_ = h | ||
.param(0) | ||
.and_then(|v| v.value().as_str()) | ||
.and_then(|v| fa::Type::from_str(v).ok()) | ||
.ok_or_else(|| { | ||
RenderErrorReason::Other( | ||
"Param 0 with String type is required for fontawesome helper.".to_owned(), | ||
) | ||
})?; | ||
|
||
let name = h.param(1).and_then(|v| v.value().as_str()).ok_or_else(|| { | ||
RenderErrorReason::Other( | ||
"Param 1 with String type is required for fontawesome helper.".to_owned(), | ||
) | ||
})?; | ||
|
||
trace!("fa_helper: {} {}", type_, name); | ||
|
||
let name = name | ||
.strip_prefix("fa-") | ||
.or_else(|| name.strip_prefix("fab-")) | ||
.or_else(|| name.strip_prefix("fas-")) | ||
.unwrap_or(name); | ||
|
||
if let Some(id) = h.param(2).and_then(|v| v.value().as_str()) { | ||
out.write(&format!("<span class=\"fa-svg\" id=\"{}\">", id))?; | ||
} else { | ||
out.write("<span class=\"fa-svg\">")?; | ||
} | ||
out.write( | ||
fa::svg(type_, name) | ||
uncenter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.or_else(|_| fa::svg(fa::Type::Brands, name)) | ||
.map_err(|_| RenderErrorReason::Other(format!("Missing font {}", name)))?, | ||
)?; | ||
out.write("</span>")?; | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod fontawesome; | ||
pub mod navigation; | ||
pub mod theme; | ||
pub mod toc; |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better instead of generating the SVG in the DOM directly to rely on CSS instead? It brings multiple advantages:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to build a giant stylesheet with every icon in it unless we have to. That file would be immense, and most of it wouldn't get used. But mdBook exposes an API that lets the user include any icon they want, so we'd have to?
Besides, fontawesome SVGs almost all have a single
<path>
in them. The DOM is tiny.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FontAwesome is a font. Meaning that as long as we have the font, we have easily use their icons without needing to declare them ourselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then you must include the entirety of the font (which is quite large iirc), unless you want to deal with subsetting? And then that is a whole other ordeal.