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: use relative url as redirect target #57

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ title: baz
http://example.com/baz-page ⇒ http://example.com/2020/01/02/foo-post/

_If a post could not be located (due to incorrect value), the article will be redirected to http://example.com/foo-post_

## Options

```yaml
alias_generator:
relative_url: false
```

- **relative_url**: Use relative url as redirect target.
36 changes: 23 additions & 13 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
'use strict';

const { Cache, full_url_for } = require('hexo-util');
const { Cache, full_url_for, url_for } = require('hexo-util');

const templateCache = new Cache();
const template = hexo => path => templateCache.apply(path, () => {
const target = full_url_for.call(hexo, path);
return '<!DOCTYPE html>'
+ '<html>'
+ '<head>'
+ '<meta charset="utf-8">'
+ '<title>Redirecting...</title>'
+ '<link rel="canonical" href="' + target + '">'
+ '<meta http-equiv="refresh" content="0; url=' + target + '">'
+ '</head>'
+ '</html>';
});
const template = hexo => {
let targetUrl = path => full_url_for.call(hexo, path);
const aliasConfig = hexo.config.alias_generator;
if (typeof aliasConfig === 'object') {
if (aliasConfig.relative_url) {
targetUrl = path => url_for.call(hexo, path);
}
}

return path => templateCache.apply(path, () => {
const target = targetUrl(path);
return '<!DOCTYPE html>'
+ '<html>'
+ '<head>'
+ '<meta charset="utf-8">'
+ '<title>Redirecting...</title>'
+ '<link rel="canonical" href="' + target + '">'
+ '<meta http-equiv="refresh" content="0; url=' + target + '">'
+ '</head>'
+ '</html>';
})
};

function redirGenerator(data) {
const { alias, aliases } = data;
Expand Down