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

fix(url_for): some improvement for url_for #307

Merged
merged 12 commits into from
Jan 12, 2024
70 changes: 50 additions & 20 deletions lib/url_for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,73 @@ import prettyUrls from './pretty_urls';
import Cache from './cache';
const cache = new Cache<string>();

function urlForHelper(path = '/', options) {
/**
* url_for options type
* @example
* // to call this type
* type urlOpt = Parameters<typef import('hexo-util')['url_for']>[1];
*/
interface url_for_options {
uiolee marked this conversation as resolved.
Show resolved Hide resolved
relative?: boolean;
}

/**
* get url relative to base URL (config_yml.url)
* @param path relative path inside `source` folder (config_yml.source_dir)
* @param options
* @returns
* @example
* // global `hexo` must be exist when used this function inside plugin
* const Hutil = require('hexo-util')
* console.log(Hutil.url_for.bind(hexo)('path/to/file/inside/source.css')); // https://example.com/path/to/file/inside/source.css
*/
function urlForHelper(path = '/', options: url_for_options | null = {}) {
if (/^(#|\/\/|http(s)?:)/.test(path)) return path;

const { config } = this;

options = Object.assign({
relative: config.relative_link
}, options);
options = Object.assign(
{
relative: config.relative_link
},
// fallback empty object when options filled with NULL
options || {}
);

// Resolve relative url
if (options.relative) {
return relative_url(this.path, path);
}

const { root } = config;
const prettyUrlsOptions = Object.assign({
trailing_index: true,
trailing_html: true
}, config.pretty_urls);
const prettyUrlsOptions = Object.assign(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the original code style here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like he used prettier. If eslint doesn't fail, I guess it doesn't matter.

{
trailing_index: true,
trailing_html: true
},
config.pretty_urls
);

// cacheId is designed to works across different hexo.config & options
return cache.apply(`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${path}`, () => {
const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);
return cache.apply(
`${config.url}-${root}-${prettyUrlsOptions.trailing_index}-${prettyUrlsOptions.trailing_html}-${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;
}
// Exit if input is an external link or a data url
if (data.hostname !== sitehost || data.origin === 'null') {
return path;
}

// Prepend root path
path = encodeURL((root + path).replace(/\/{2,}/g, '/'));
// Prepend root path
path = encodeURL((root + path).replace(/\/{2,}/g, '/'));

path = prettyUrls(path, prettyUrlsOptions);
path = prettyUrls(path, prettyUrlsOptions);

return path;
});
return path;
}
);
}

export = urlForHelper;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "./dist/index.d.ts",
"scripts": {
"prepublishOnly": "npm install && npm run clean && npm run build",
"build": "tsc -b",
"build": "npm run build:highlight && tsc -b",
dimaslanjaka marked this conversation as resolved.
Show resolved Hide resolved
"clean": "tsc -b --clean",
"preeslint": "npm run clean && npm run build",
"eslint": "eslint lib test",
Expand Down