UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const url_1 = require("url");
6const encode_url_1 = __importDefault(require("./encode_url"));
7const relative_url_1 = __importDefault(require("./relative_url"));
8const pretty_urls_1 = __importDefault(require("./pretty_urls"));
9const cache_1 = __importDefault(require("./cache"));
10const cache = new cache_1.default();
11function urlForHelper(path = '/', options) {
12 if (/^(#|\/\/|http(s)?:)/.test(path))
13 return path;
14 const { config } = this;
15 options = Object.assign({
16 relative: config.relative_link
17 }, options);
18 // Resolve relative url
19 if (options.relative) {
20 return (0, relative_url_1.default)(this.path, path);
21 }
22 const { root } = config;
23 const prettyUrlsOptions = Object.assign({
24 trailing_index: true,
25 trailing_html: true
26 }, config.pretty_urls);
27 // cacheId is designed to works across different hexo.config & options
28 return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
29 const sitehost = (0, url_1.parse)(config.url).hostname || config.url;
30 const data = new URL(path, `http://${sitehost}`);
31 // Exit if input is an external link or a data url
32 if (data.hostname !== sitehost || data.origin === 'null') {
33 return path;
34 }
35 // Prepend root path
36 path = (0, encode_url_1.default)((root + path).replace(/\/{2,}/g, '/'));
37 path = (0, pretty_urls_1.default)(path, prettyUrlsOptions);
38 return path;
39 });
40}
41module.exports = urlForHelper;
42//# sourceMappingURL=url_for.js.map
\No newline at end of file