Skip to content

Commit

Permalink
Allows generation of redirects.json to be disabled (#207)
Browse files Browse the repository at this point in the history
Merge pull request 207
  • Loading branch information
dmalan authored and jekyllbot committed Jan 26, 2020
1 parent 26d6e61 commit b708d48
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ Your layout will get the following variables:
* `page.redirect.from` - the relative path to the redirect page
* `page.redirect.to` - the absolute URL (where available) to the target page

## Configuration

You can configure this plugin in `_config.yml` by adding to the `redirect_from` key.

### Disabling `redirects.json`

By default, a file called `redirects.json`, which can be used for automated testing or to implement server-side redirects, will be included in the output. To exclude it from the output, set the `json` key to `false`:

```yml
redirect_from:
json: false
```

## Contributing

1. Fork it
Expand Down
6 changes: 5 additions & 1 deletion lib/jekyll-redirect-from/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def generate(site)
generate_redirect_to(doc)
end

generate_redirects_json
generate_redirects_json if generate_redirects_json?
end

private
Expand Down Expand Up @@ -53,5 +53,9 @@ def generate_redirects_json
page.data["layout"] = nil
site.pages << page
end

def generate_redirects_json?
site.config.dig("redirect_from", "json") != false
end
end
end
12 changes: 10 additions & 2 deletions spec/jekyll_redirect_from/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
let(:redirects) { JSON.parse(contents) }
let(:domain) { "http://jekyllrb.com" }

it "creates the redirets file" do
it "creates the redirects file" do
expect(path).to exist
end

Expand Down Expand Up @@ -140,10 +140,18 @@
FileUtils.rm_f source_path
end

it "doesn't overwrite redirets.json" do
it "doesn't overwrite redirects.json" do
expect(path).to exist
expect(redirects).to eql("foo" => "bar")
end
end

context "when explicitly disabled" do
let(:site) { Jekyll::Site.new(config.merge("redirect_from" => { "json" => false })) }

it "does not create the redirects file" do
expect(path).to_not exist
end
end
end
end

0 comments on commit b708d48

Please sign in to comment.