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(helpers, tag plugins): encode url by default #3710

Merged
merged 14 commits into from Oct 7, 2019

Conversation

@curbengh
Copy link
Contributor

@curbengh curbengh commented Sep 11, 2019

What does it do?

Continuation of #3708, this PR deals with helpers and tag plugins.

How to test

git clone -b encode-url-helper https://github.com/curbengh/hexo.git
cd hexo
npm install
npm test

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.
@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 11, 2019

Depending on use case, asset_path tag plugin might not need to encode url.

@coveralls
Copy link

@coveralls coveralls commented Sep 11, 2019

Coverage Status

Coverage decreased (-0.01%) to 97.237% when pulling 382b0de on curbengh:encode-url-helper into ffe4eaa on hexojs:master.

@curbengh curbengh changed the title fix(helpers, tag plugins): encode url by default WIP fix(helpers, tag plugins): encode url by default Sep 11, 2019
@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 11, 2019

url_for pending hexojs/hexo-util#82

@SukkaW
Copy link
Member

@SukkaW SukkaW commented Sep 14, 2019

Should we re-implement those helper using hexo-util?

For example:

// url_for.js
'use strict';

module.exports = function (path = '/', options) {
  const url_for = require('hexo-util').url_for.bind(this);
  return url_for(path, options)
};

@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 15, 2019

Should we re-implement those helper using hexo-util?

Definitely, once hexo-util v1.2.0 is published. To be refactored (in a separate PR):

  • relative_url
  • gravatar
  • full_url_for

@curbengh curbengh changed the title WIP fix(helpers, tag plugins): encode url by default fix(helpers, tag plugins): encode url by default Sep 15, 2019
@curbengh curbengh requested a review from Sep 15, 2019
@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 15, 2019

Ready to review.


Just realized path is encoded twice if it's already encode (/foo/bar%20baz -> /foo/bar%25%20baz). Pending hexojs/hexo-util#92

lib/plugins/tag/asset_link.js Outdated Show resolved Hide resolved
@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 16, 2019

WIP, needs to utilize url_for() more.

Edit: refactored.

@curbengh curbengh changed the title fix(helpers, tag plugins): encode url by default WIP fix(helpers, tag plugins): encode url by default Sep 16, 2019
@curbengh curbengh changed the title WIP fix(helpers, tag plugins): encode url by default fix(helpers, tag plugins): encode url by default Sep 18, 2019
@curbengh curbengh requested a review from tomap Sep 18, 2019
lib/plugins/helper/relative_url.js Show resolved Hide resolved
lib/plugins/helper/url_for.js Show resolved Hide resolved
@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 19, 2019

Pending hexojs/hexo-util#92 & hexojs/hexo-util#94 to avoid encoding path twice.

@curbengh curbengh mentioned this pull request Sep 19, 2019
2 tasks
lib/plugins/tag/asset_link.js Outdated Show resolved Hide resolved
lib/plugins/tag/asset_path.js Outdated Show resolved Hide resolved
lib/plugins/tag/asset_path.js Outdated Show resolved Hide resolved
lib/plugins/tag/post_link.js Outdated Show resolved Hide resolved
lib/plugins/tag/post_path.js Outdated Show resolved Hide resolved
@curbengh curbengh requested a review from SukkaW Sep 20, 2019
@curbengh curbengh force-pushed the encode-url-helper branch from c5e60b4 to cc23ba1 Sep 20, 2019
@SukkaW
Copy link
Member

@SukkaW SukkaW commented Sep 20, 2019

I have no idea why the test on AppVeyor failed while the test on Travis CI successed.

@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 21, 2019

I have no idea why the test on AppVeyor failed while the test on Travis CI successed.

The test failure shows the actual is "/foo%5Cbar" where %5C is a backslash, so it's Windows path.

This PR replaces url.resolve('/', asset.path); with encodeURL('/' + asset.path), the removal of url.resolve() could be the cause. I'll try putting it back, in combination with encodeURL.


Seems to resolve it, now left double-encoding issue.
Pending hexojs/hexo-util#92 & hexojs/hexo-util#99


Looks like full_url_for() and url_for() currently don't support Windows path. Perhaps adding url.resolve() in those functions can help.

Edit: url.resolve() in full_url_for() wouldn't help

const { resolve } = require('url')
console.log(resolve('http://example.com/blog/', 'abc\hey\index.html'))
// http://example.com/blog/abcheyindex.html

Anyhow, Windows path may not be applicable for full_url_for() / url_for().

@curbengh curbengh force-pushed the encode-url-helper branch from 8553da7 to 00f6141 Sep 21, 2019
@curbengh
Copy link
Contributor Author

@curbengh curbengh commented Sep 22, 2019

html_tag of hexo-util needs to be updated for mail_to helper compatibility.


Located the issue:

const { parse } = require('url')
console.log(parse('mailto:abc@example.com'))
/*
Url {
  protocol: 'mailto:',
  slashes: null,
  auth: 'abc',
  host: 'example.com',
  port: null,
  hostname: 'example.com',
  hash: null,
  search: null,
  query: null,
  pathname: null,
  path: null,
  href: 'mail:abc@example.com'
}
*/

null path is appended, resulting in mailto:abc@example.comnull.


Fix created hexojs/hexo-util#102

@curbengh curbengh force-pushed the encode-url-helper branch from 9cee028 to 382b0de Sep 27, 2019
@curbengh curbengh added this to In progress in v4.0.0 via automation Oct 1, 2019
@curbengh curbengh moved this from In progress to To do in v4.0.0 Oct 1, 2019
@curbengh curbengh requested a review from SukkaW Oct 7, 2019
SukkaW
SukkaW approved these changes Oct 7, 2019
v4.0.0 automation moved this from To do to In progress Oct 7, 2019
@curbengh curbengh merged commit 18d459f into hexojs:master Oct 7, 2019
3 of 6 checks passed
v4.0.0 automation moved this from In progress to Done Oct 7, 2019
@curbengh curbengh deleted the encode-url-helper branch Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v4.0.0
  
Done
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants