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

feat: add 'no-copy-extra-files' flag to html renderer, preventing external files from being copied to the dest dir #2247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions guide/src/format/configuration/renderers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ pub struct HtmlConfig {
/// The mapping from old pages to new pages/URLs to use when generating
/// redirects.
pub redirect: HashMap<String, String>,
/// Don't copy extra files from the src folder to the output folder
pub no_copy_extra_files: bool,
}

impl Default for HtmlConfig {
Expand Down Expand Up @@ -610,6 +612,7 @@ impl Default for HtmlConfig {
cname: None,
live_reload_endpoint: None,
redirect: HashMap::new(),
no_copy_extra_files: false,
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down