diff --git a/README.md b/README.md index 2c96ccc..2096094 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/generator.js b/lib/generator.js index 0e62b30..d4f69a3 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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 '' - + '' - + '' - + '' - + 'Redirecting...' - + '' - + '' - + '' - + ''; -}); +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 '' + + '' + + '' + + '' + + 'Redirecting...' + + '' + + '' + + '' + + ''; + }) +}; function redirGenerator(data) { const { alias, aliases } = data;