-
-
Notifications
You must be signed in to change notification settings - Fork 5k
refactor: replace url.resolve() with String.replace() #4479
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
Conversation
|
Should we bring up a |
| // resolve `path`'s absolute path relative to current page's url | ||
| // `path` can be both absolute (starts with `/`) or relative. | ||
| // `path` can be both absolute (starts with `http://`) or relative (starts with `/`). | ||
| return resolve(url || config.url, path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we bring up a resolveURL in hexo-util?
L173 could be replaced with:
const urlVal = url || config.url;
return urlVal.replace(/\/$/, '') + '/' + path.replace(urlVal, '').replace(/\\/g, '/').replace(/^\//, '')but it only works when both urlVal and path have the same protocol scheme (e.g. both start with http:// or both start with //).
url.resolve() is (surprisingly) robust, I tested:
http://+////+http://http://+https://
and the output is still accurate.
in this "open_graph.js", url.resolve() is even robust to when path variable is an external link, in which it returns external link instead.
Other plugins (of this PR) only deal with relative path, not absolute url, so (a + b).replace(/\\/g, '/').replace(/\/{2,}/g, '/') works fine. Perhaps can implement joinPath()?
Also, in other plugins (of this PR), url.resolve() is actually not used according to its purpose, currently it's used as path.join() plus converting backward to forward slash, whereas this L123 does properly utilise url.resolve().
| if (escape === 'true') title = attrTitle; | ||
|
|
||
| const link = encodeURL(resolve(ctx.config.root, asset.path)); | ||
| const link = encodeURL((ctx.config.root + asset.path).replace(/\\/g, '/').replace(/\/{2,}/g, '/')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
url_for() currently cannot be used here because it doesn't convert backward to forward slash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to testgit clone -b resolve-path-string-replace https://github.com/curbengh/hexo.git
cd hexo
npm install
npm test |
What does it do?
url.resolve()is a legacy API, so it's best to avoid it if possible,String.replace()is also 6x faster.How to test
Screenshots
Pull request tasks