mdBook
mdBook copied to clipboard
Add copy-custom-fonts parameter to HtmlConfig to enable custom fonts
The first PR #1799 has been accidentially closed.
This PR introduces a new parameter to the HtmlConfig struct named copy_custom_fonts. When set to true, the fonts/fonts.css file is scanned for @font-face rules that contain a src: url(font-file.name) attribute. The files that have been found are copied into the book folder on build/serve.
If copy_fonts is also enabled, the build in fonts/fonts.css is extended by the custom one. In addition to that, the build in font files are also copied to the output folder.
Example usage:
Create a fonts/ folder in your book root. Then copy the fonts you want to use into this folder (in this example we are assuming Lato-Regular.ttf)
Create a custom fonts file fonts/fonts.css
@font-face {
font-family: "Lato";
font-style: normal;
font-weight: normal;
src: url('Lato-Regular.ttf');
}
Setup your book.toml that it contains the following parameters:
[output.html]
copy-fonts = true
copy-custom-fonts = true
Adjust your theme/css/general.css file according to your needs, for example:
html {
font-family: "Lato", sans-serif;
}
It adds a new dependency lewp-css to Cargo.toml for simple parsing of the fonts/fonts.css file.
It also updates the msrv to 1.56.0 because lewp-css requires edition 2021.
Furthermore, the clap dependency gets updated.
AIUI, non-processed files under src/ get copied as-is, so couldn't you simply create src/fonts/Lato-Regular.ttf and reference that (fonts/Lato-Regular.ttf)?
Thanks for your response! You are right, this works indeed. But if it comes to project structure, the font files in my opinion do not belong to the source code of the project. As a result, they should go into a separate folder to have clean separation. Would that make sense?
Thanks for the PR! I think I would like to go with a different solution posted in #1987. My goal is to keep the config as simple as possible. Any feedback on that approach is welcome, though.