hexo/node_modules/hexo-util/lib/full_url_for.js

35 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
'use strict';
const { parse } = require('url');
const encodeURL = require('./encode_url');
const prettyUrls = require('./pretty_urls');
const Cache = require('./cache');
const cache = new Cache();
function fullUrlForHelper(path = '/') {
const { config } = this;
const prettyUrlsOptions = Object.assign({
trailing_index: true,
trailing_html: true
}, config.pretty_urls);
// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
if (/^(\/\/|http(s)?:)/.test(path)) return path;
const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
// Exit if input is an external link or a data url
if (data.hostname !== sitehost || data.origin === 'null') return path;
path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/'));
path = prettyUrls(path, prettyUrlsOptions);
return path;
});
}
module.exports = fullUrlForHelper;