Few days ago I found mdBook. Looking around for an easy, small, not oveloaded static site generator, I'm excited. It's easy to use, simple, fast and completely well designed.
In spite of an easy an fast setup, details hold problems inside. I'd like to tweak the font in a customized theme.
In the mdBook manual font configuration possibilities are only mentioned with the HTML renderer options: A 'copy-fonts' config option copies 'fonts.css' and respective font files to the output directory to be used in the default theme. But reading a few bullet points above 'default-theme' is defined as the theme color scheme to select by default in the 'Change Theme' dropdown.
How does that fit together? In my configuration the automatic copying of font files does not work. I wrote a little bash script that does the copying after building book output.
So I'd like to describe the configuration steps I did:
set up a new book with copying the default theme's basic files into a separate directory:
mdbook init testBook --themerename the 'theme' directory to 'peters-theme'
create new directory 'peters-theme/fonts
copy the Libertinus TeX font into this directory
create a new css file 'peters-theme/fonts/libertinus-sans-font.css'
@font-face { font-family: libertinus-sans; font-style: normal; font-weight: normal; src: url('LibertinusSans-Regular.otf') format('opentype'); } @font-face { font-family: libertinus-sans; font-style: italic; font-weight: normal; src: url('LibertinusSans-Italic.otf') format('opentype'); } @font-face { font-family: libertinus-sans; font-style: normal; font-weight: bold; src: url('LibertinusSans-Bold.otf') format('opentype'); }
- tweak file 'peters-theme/css/general.css'
add additional css import rule
@import '../fonts/libertinus-sans-font.css'; /* added individually: use 'libertinus sans' fonts */change HTML selector
html { font-family: libertinus-sans; /* added individually: use 'libertinus sans' fonts */ color: var(--fg); background-color: var(--bg); text-size-adjust: none; }
create file 'peters-theme/fonts/libertinus-sans-font.css'
@font-face { font-family: libertinus-sans; font-style: normal; font-weight: normal; src: url('LibertinusSans-Regular.otf') format('opentype'); } @font-face { font-family: libertinus-sans; font-style: italic; font-weight: normal; src: url('LibertinusSans-Italic.otf') format('opentype'); } @font-face { font-family: libertinus-sans; font-style: normal; font-weight: bold; src: url('LibertinusSans-Bold.otf') format('opentype'); }place font files into 'peters-theme/fonts' directory
- 'peters-theme/fonts/LibertinusSans-Bold.otf'
- 'peters-theme/fonts/LibertinusSans-Italic.otf'
- 'peters-theme/fonts/LibertinusSans-Regular.otf'
tweak file 'peters-theme/index.hbs'
<!-- Fonts --> <link rel="stylesheet" href="{{ path_to_root }}FontAwesome/css/font-awesome.css"> {{#if copy_fonts}} <!-- added additionally to include custom font CSS --> <link rel="stylesheet" href="{{ path_to_root }}fonts/libertinus-sans-font.css"> {{/if}}create bash script 'build-book.sh' (with copying as workaround)
#!/bin/bash # # Author: Peter # Date: 2020-11-20 # ROOTFOLDER='/home/peter/Documents/Peter/Notes/mdBook/testBook/' # # change to book directory cd $ROOTFOLDER # # clean up old book files mdbook clean # # build the book mdbook build # # copy fonts for custom theme cp -r ./peters-theme/fonts/ ./book/ # # display book in browser mdbook serve --open
Greetings
Peter