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

Provide redirect maps in the form of a seperate yml file #57

Open
wants to merge 3 commits 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ plugins:
'some_file.md': 'http://external.url.com/foobar'
```

Alternatively, get the redirects from a seperate `yml` file with `map_file`.

```yaml
plugins:
- redirects:
map_file: 'redirect_map.yml'
```

Where the `map_file` uses the same syntax as above.

> **Note**
> Don't forget that specifying the `plugins` setting will override the defaults if you didn't already have it set! See [this page](https://www.mkdocs.org/user-guide/configuration/#plugins) for more information.

Expand Down
13 changes: 12 additions & 1 deletion mkdocs_redirects/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,22 @@ class RedirectPlugin(BasePlugin):
# Any options that this plugin supplies should go here.
config_scheme = (
("redirect_maps", config_options.Type(dict, default={})), # note the trailing comma
("map_file", config_options.Type(str)),
)

# Build a list of redirects on file generation
def on_files(self, files, config, **kwargs):
self.redirects = self.config.get("redirect_maps", {})
if self.config.get('map_file'):
filename = self.config.get('map_file')
if os.path.isfile(filename):
with open(filename) as f:
self.redirects = utils.yaml_load(f)
log.debug("Loading yaml file: '%s'", filename)
else:
log.warning("yaml configuration file '%s' was not found!", filename)
# If no mapfile, fall back to regular method.
if not hasattr(self, 'redirects'):
self.redirects = self.config.get('redirect_maps', {})

# Validate user-provided redirect "old files"
for page_old in self.redirects.keys():
Expand Down