From 8622d058d52533313b3565a73c044fdfaef5f0bb Mon Sep 17 00:00:00 2001 From: "Sean P. Madden" Date: Thu, 23 Nov 2023 23:46:04 -0500 Subject: [PATCH] feat: add 'no-copy-extra-files' flag to html renderer, preventing extra files from being copied to the dest dir --- guide/src/format/configuration/renderers.md | 6 ++++++ src/config.rs | 3 +++ src/renderer/html_handlebars/hbs_renderer.rs | 13 ++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/guide/src/format/configuration/renderers.md b/guide/src/format/configuration/renderers.md index 7e7430141a..e56484a4f9 100644 --- a/guide/src/format/configuration/renderers.md +++ b/guide/src/format/configuration/renderers.md @@ -109,6 +109,7 @@ edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path site-url = "/example-book/" cname = "myproject.rs" input-404 = "not-found.md" +no-copy-extra-files = false ``` The following configuration options are available: @@ -166,6 +167,11 @@ The following configuration options are available: This string will be written to a file named CNAME in the root of your site, as required by GitHub Pages (see [*Managing a custom domain for your GitHub Pages site*][custom domain]). +- **no-copy-extra-files:** Defaults to `false`. Do not copy any extra files found in the `src` directory to the dest + directory, only copy those files generated by or interacted with by mdBook through the `SUMMARY.md` file. This + setting does not affect files specified in `additional-js` nor `additional-css`. Defaults to `false`. Enabling this + setting allows you to mix book-files alongside non-book files in the `src` directory without them getting copied to + the dest folder automatically. [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site diff --git a/src/config.rs b/src/config.rs index 7f56e797ab..c8ca1b67c4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -582,6 +582,8 @@ pub struct HtmlConfig { /// The mapping from old pages to new pages/URLs to use when generating /// redirects. pub redirect: HashMap, + /// Don't copy extra files from the src folder to the output folder + pub no_copy_extra_files: bool, } impl Default for HtmlConfig { @@ -610,6 +612,7 @@ impl Default for HtmlConfig { cname: None, live_reload_endpoint: None, redirect: HashMap::new(), + no_copy_extra_files: false, } } } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 8ea2f49efc..b5c513bd6d 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -627,9 +627,16 @@ impl Renderer for HtmlHandlebars { self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect) .context("Unable to emit redirects")?; - // Copy all remaining files, avoid a recursive copy from/to the book build dir - utils::fs::copy_files_except_ext(&src_dir, destination, true, Some(&build_dir), &["md"])?; - + if !html_config.no_copy_extra_files { + // Copy all remaining files, avoid a recursive copy from/to the book build dir + utils::fs::copy_files_except_ext( + &src_dir, + destination, + true, + Some(&build_dir), + &["md"], + )?; + } Ok(()) } }