Skip to content

Conversation

@curbengh
Copy link
Contributor

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

git clone -b resolve-path-string-replace https://github.com/curbengh/hexo.git
cd hexo
npm install
npm test

Screenshots

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@coveralls
Copy link

coveralls commented Aug 15, 2020

Coverage Status

Coverage decreased (-0.0009%) to 98.294% when pulling 27384cc on curbengh:resolve-path-string-replace into 463025c on hexojs:master.

@curbengh curbengh marked this pull request as draft August 15, 2020 10:38
@SukkaW
Copy link
Member

SukkaW commented Aug 15, 2020

Should we bring up a resolveURL in hexo-util?

// 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);
Copy link
Contributor Author

@curbengh curbengh Aug 15, 2020

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:

  1. http:// + //
  2. // + http://
  3. 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, '/'));
Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@github-actions
Copy link

How to test

git clone -b resolve-path-string-replace https://github.com/curbengh/hexo.git
cd hexo
npm install
npm test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants