Skip to content
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

Add additional-hljs-packages option #2156

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mathjax-support = false
copy-fonts = true
additional-css = ["custom.css", "custom2.css"]
additional-js = ["custom.js"]
additional-hljs-packages = ["custom.min.js"]
no-section-label = false
git-repository-url = "https://github.com/rust-lang/mdBook"
git-repository-icon = "fa-github"
Expand Down Expand Up @@ -139,6 +140,7 @@ The following configuration options are available:
- **additional-js:** If you need to add some behaviour to your book without
removing the current behaviour, you can specify a set of JavaScript files that
will be loaded alongside the default one.
- **additional-hljs-packages:** If you need to add 3rd party highlight.js language packages, you can specify a set of JavaScript files that will be loaded.
- **no-section-label:** mdBook by defaults adds numeric section labels in the table of
contents column. For example, "1.", "2.1". Set this option to true to disable
those labels. Defaults to `false`.
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ pub struct HtmlConfig {
/// Additional JS scripts to include at the bottom of the rendered page's
/// `<body>`.
pub additional_js: Vec<PathBuf>,
/// Additional 3rd party language packages for highlight.js.
pub additional_hljs_packages: Vec<PathBuf>,
/// Fold settings.
pub fold: Fold,
/// Playground settings.
Expand Down Expand Up @@ -556,6 +558,7 @@ impl Default for HtmlConfig {
google_analytics: None,
additional_css: Vec::new(),
additional_js: Vec::new(),
additional_hljs_packages: Vec::new(),
fold: Fold::default(),
playground: Playground::default(),
code: Code::default(),
Expand Down Expand Up @@ -988,6 +991,7 @@ mod tests {
google-analytics = "123456"
additional-css = ["custom.css", "custom2.css"]
additional-js = ["custom.js"]
additional-hljs-packages = ["custom.min.js"]
"#;

let book_should_be = BookConfig {
Expand All @@ -1013,6 +1017,7 @@ mod tests {
google_analytics: Some(String::from("123456")),
additional_css: vec![PathBuf::from("custom.css"), PathBuf::from("custom2.css")],
additional_js: vec![PathBuf::from("custom.js")],
additional_hljs_packages: vec![PathBuf::from("custom.min.js")],
..Default::default()
};

Expand Down
19 changes: 17 additions & 2 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,19 @@ impl HtmlHandlebars {
handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option));
}

/// Copy across any additional CSS and JavaScript files which the book
/// Copy across any additional CSS, JavaScript and hljs package files which the book
/// has been configured to use.
fn copy_additional_css_and_js(
&self,
html: &HtmlConfig,
root: &Path,
destination: &Path,
) -> Result<()> {
let custom_files = html.additional_css.iter().chain(html.additional_js.iter());
let custom_files = html
.additional_css
.iter()
.chain(html.additional_js.iter())
.chain(html.additional_hljs_packages.iter());

debug!("Copying additional CSS and JS");

Expand Down Expand Up @@ -723,6 +727,17 @@ fn make_data(
data.insert("additional_js".to_owned(), json!(js));
}

if !html_config.additional_hljs_packages.is_empty() {
let mut hljs_package = Vec::new();
for package in &html_config.additional_hljs_packages {
match package.strip_prefix(root) {
Ok(p) => hljs_package.push(p.to_str().expect("Could not convert to str")),
Err(_) => hljs_package.push(package.to_str().expect("Could not convert to str")),
}
}
data.insert("additional_hljs_packages".to_owned(), json!(hljs_package));
}

if html_config.playground.editable && html_config.playground.copy_js {
data.insert("playground_js".to_owned(), json!(true));
if html_config.playground.line_numbers {
Expand Down
4 changes: 4 additions & 0 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@

<script src="{{ path_to_root }}clipboard.min.js"></script>
<script src="{{ path_to_root }}highlight.js"></script>
<!-- Custom 3rd highlight.js language packages -->
{{#each additional_hljs_packages}}
<script src="{{ ../path_to_root }}{{this}}"></script>
{{/each}}
<script src="{{ path_to_root }}book.js"></script>

<!-- Custom JS scripts -->
Expand Down