1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | const url_1 = require("url");
|
6 | const encode_url_1 = __importDefault(require("./encode_url"));
|
7 | const relative_url_1 = __importDefault(require("./relative_url"));
|
8 | const pretty_urls_1 = __importDefault(require("./pretty_urls"));
|
9 | const cache_1 = __importDefault(require("./cache"));
|
10 | const cache = new cache_1.default();
|
11 | function 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 |
|
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 |
|
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 |
|
32 | if (data.hostname !== sitehost || data.origin === 'null') {
|
33 | return path;
|
34 | }
|
35 |
|
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 | }
|
41 | module.exports = urlForHelper;
|
42 |
|
\ | No newline at end of file |