From fec58054b481201abde278f0833d36e213c8dd00 Mon Sep 17 00:00:00 2001 From: Greizgh Date: Tue, 2 Oct 2018 08:15:26 +0200 Subject: [PATCH] Inject config in robots.txt context This allow to use base_url in robots.txt, to reference a sitemap for example. --- components/site/src/lib.rs | 4 +++- components/site/tests/site.rs | 1 + docs/content/documentation/templates/robots.md | 4 ++-- test_site/templates/robots.txt | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index e7de7da..bfd0eba 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -670,9 +670,11 @@ impl Site { /// Renders robots.txt pub fn render_robots(&self) -> Result<()> { ensure_directory_exists(&self.output_path)?; + let mut context = Context::new(); + context.insert("config", &self.config); create_file( &self.output_path.join("robots.txt"), - &render_template("robots.txt", &self.tera, &Context::new(), &self.config.theme)?, + &render_template("robots.txt", &self.tera, &context, &self.config.theme)?, ) } diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 98b4bc0..de7d177 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -168,6 +168,7 @@ fn can_build_site_without_live_reload() { // robots.txt has been rendered from the template assert!(file_contains!(public, "robots.txt", "User-agent: gutenberg")); + assert!(file_contains!(public, "robots.txt", "Sitemap: https://replace-this-with-your-url.com/sitemap.xml")); } #[test] diff --git a/docs/content/documentation/templates/robots.md b/docs/content/documentation/templates/robots.md index 8cc5855..811a37c 100644 --- a/docs/content/documentation/templates/robots.md +++ b/docs/content/documentation/templates/robots.md @@ -6,8 +6,8 @@ weight = 70 Gutenberg will look for a `robots.txt` file in the `templates` directory or use the built-in one. -Robots.txt is the simplest of all templates: it doesn't take any variables -and the default is what most site want. +Robots.txt is the simplest of all templates: it only gets the config +and the default is what most site want: ```jinja2 User-agent: * diff --git a/test_site/templates/robots.txt b/test_site/templates/robots.txt index 75e6189..49571e4 100644 --- a/test_site/templates/robots.txt +++ b/test_site/templates/robots.txt @@ -1,2 +1,3 @@ User-agent: gutenberg Allow: / +Sitemap: {{config.base_url}}/sitemap.xml