Skip to content

Commit 1b18740

Browse files
authored
Merge pull request #1311 from dtolnay/cname
Support emitting CNAME file for publishing at a custom domain
2 parents 6fed9e5 + 1acf23f commit 1b18740

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

book-example/src/format/config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ The following configuration options are available:
210210
- **site-url:** The url where the book will be hosted. This is required to ensure
211211
navigation links and script/css imports in the 404 file work correctly, even when accessing
212212
urls in subdirectories. Defaults to `/`.
213+
- **cname:** The DNS subdomain or apex domain at which your book will be hosted.
214+
This string will be written to a file named CNAME in the root of your site, as
215+
required by GitHub Pages (see [*Managing a custom domain for your GitHub Pages
216+
site*][custom domain]).
217+
218+
[custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
213219

214220
Available configuration options for the `[output.html.fold]` table:
215221

@@ -273,6 +279,7 @@ no-section-label = false
273279
git-repository-url = "https://github.com/rust-lang/mdBook"
274280
git-repository-icon = "fa-github"
275281
site-url = "/example-book/"
282+
cname = "myproject.rs"
276283
input-404 = "not-found.md"
277284

278285
[output.html.fold]

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,13 @@ pub struct HtmlConfig {
508508
pub input_404: Option<String>,
509509
/// Absolute url to site, used to emit correct paths for the 404 page, which might be accessed in a deeply nested directory
510510
pub site_url: Option<String>,
511+
/// The DNS subdomain or apex domain at which your book will be hosted. This
512+
/// string will be written to a file named CNAME in the root of your site,
513+
/// as required by GitHub Pages (see [*Managing a custom domain for your
514+
/// GitHub Pages site*][custom domain]).
515+
///
516+
/// [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
517+
pub cname: Option<String>,
511518
/// This is used as a bit of a workaround for the `mdbook serve` command.
512519
/// Basically, because you set the websocket port from the command line, the
513520
/// `mdbook serve` command needs a way to let the HTML renderer know where
@@ -541,6 +548,7 @@ impl Default for HtmlConfig {
541548
git_repository_icon: None,
542549
input_404: None,
543550
site_url: None,
551+
cname: None,
544552
livereload_url: None,
545553
redirect: HashMap::new(),
546554
}

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ impl HtmlHandlebars {
187187
b"This file makes sure that Github Pages doesn't process mdBook's output.",
188188
)?;
189189

190+
if let Some(cname) = &html_config.cname {
191+
write_file(destination, "CNAME", format!("{}\n", cname).as_bytes())?;
192+
}
193+
190194
write_file(destination, "book.js", &theme.js)?;
191195
write_file(destination, "css/general.css", &theme.general_css)?;
192196
write_file(destination, "css/chrome.css", &theme.chrome_css)?;

0 commit comments

Comments
 (0)