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 to use root to export Permalink #3604

Closed
wants to merge 2 commits into from
Closed

Fix to use root to export Permalink #3604

wants to merge 2 commits into from

Conversation

sKawashima
Copy link

What does it do?

Fix to use root to export permalink.
It fix #3601 .

[Page,Category,Tag].virtual('permalink').get(function() {
// return `${ctx.config.url}/${this.path}`; 
// to
  return `${ctx.config.url}${ctx.config.root}${this.path}`;
});

How to test

git clone -b BRANCH https://github.com/USER/hexo.git
cd hexo
npm install
npm test

@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.148% when pulling a423068 on sKawashima:20190629fixPermalink_addRoot into 7fba3c1 on hexojs:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.148% when pulling a423068 on sKawashima:20190629fixPermalink_addRoot into 7fba3c1 on hexojs:master.

@sKawashima
Copy link
Author

Codeclimate is not passed, but it was so before I touched it.

Please check.

@yoshinorin
Copy link
Member

Sorry, We have many repositories and need to maintain them.
Would you please give us time? Thanks 😃

@yoshinorin yoshinorin added the bug Something isn't working label Jul 2, 2019
@yoshinorin yoshinorin self-assigned this Jul 2, 2019
@curbengh
Copy link
Contributor

curbengh commented Jul 10, 2019

@#2610 (comment)

In this case, why is root parameter for? Just for curiosity...

It's used in several tag plugins like include code to link to a local path via <a href="/root_value/filename/"> instead of <a href="http://yourweb.com/root_value/filename/">,

const caption = `<span>${title}</span><a href="${ctx.config.root}${codeDir}${path}">view raw</a>`;

Post model replace the config.root with '/',

return config.url + partial_url.replace(config.root, '/');

The above works because the docs requires root value in both url and root.
https://github.com/hexojs/hexo-starter/blob/e5e9b59a5b66d4468467af4f1e931988aaa7047b/_config.yml#L14-L16


Alternatively, we can specify just one root value:

url: http://example.com
root: /blog/

The issue with this approach is that <%= config.url %> would be invalid, the proper one should be <%= config.url + config.root %>. This can be fixed by additional processing to append config.root to config.url, so config.url variable includes config.root. However, this also means that config.url no longer corresponds to the value in _config.yml.

I'm not so worry about the breaking change of the above approach, since v4 is on the horizon.


@yoshinorin
The docs mention The URL of current page without root URL. We usually use url_for(page.path) in theme.. Shouldn't page.path includes config.root? If that's the case, then it should fix the issue.

@curbengh
Copy link
Contributor

Currently the config requires root value in both url and root,
https://github.com/hexojs/hexo-starter/blob/e5e9b59a5b66d4468467af4f1e931988aaa7047b/_config.yml#L14-L16

For example,

url: http://yoursite.com/child
root: /child/

then

return `${ctx.config.url}${ctx.config.root}${this.path}`;

will return http://yoursite.com/child/child/path.

@yoshinorin
Copy link
Member

yoshinorin commented Jul 10, 2019

@weyusi
Thank you for your support :)

url & root will be fixed following lines.

config.root = config.root.replace(/\/*$/, '/');
config.url = config.url.replace(/\/+$/, '');

I think almost certainly this PR is okay. I'm going to approve and merge this PR. But, I'm not yet completed check current hexo source code. So, I haven't got confirmation of that yet.

@curbengh
Copy link
Contributor

curbengh commented Jul 10, 2019

@yoshinorin
I just tested the regex,

config.root.replace(/\/*$/, '/');

this make sure the value ends with a slash, '/child' -> '/child/'

config.url.replace(/\/+$/, '');

this make sure the url does not ends with a slash,

Anyhow, 'child' value is still there. I just tested this PR and it definitely results in http://yoursite.com/child/child/path.

For this PR to work, '/child' needs to be removed from config.url (either ask user to update their _config.yml or programmatically via load_config.js).

@yoshinorin
Copy link
Member

yoshinorin commented Jul 10, 2019

@weyusi
Just to confirm...

NOTE: I do not confirm it on the current master source code. Just confirmed on a console.

Current hexo config & result

The URL of current page without root URL. We usually use url_for(page.path) in theme...

My understanding its means...

Pattern 1: OK

Settings

url: http://yoursite.com
root: /

Result

const urlOne = "http://example.com".replace(/\/+$/, '');
const rootOne = "/".replace(/\/*$/, '/');

console.log('urlOne: ' + urlOne);
console.log('rootOne: ' + rootOne);
console.log('pattern1: ' + urlOne + rootOne + "\n");

// Result
urlOne: http://example.com
rootOne: /
pattern1: http://example.com/

Pattern 2: OK

Settings

url: http://yoursite.com
root: /child

Result

const urlTwo = "http://example.com".replace(/\/+$/, '');
const rootTwo = "/child".replace(/\/*$/, '/');

console.log('urlTwo: ' + urlTwo);
console.log('rootTwo: ' + rootTwo);
console.log('pattern2: ' + urlTwo + rootTwo + "\n");

// Result
urlTwo: http://example.com
rootTwo: /child/
pattern2: http://example.com/child/

Pattern 3: OK

Settings

url: http://yoursite.com/
root: /child

Result

const urlThree = "http://yoursite.com/".replace(/\/+$/, '');
const rootThree = "/child".replace(/\/*$/, '/');

console.log('urlThree: ' + urlThree);
console.log('rootThree: ' + rootThree);
console.log('pattern3: ' + urlThree + rootThree + "\n");

// Result
urlThree: http://yoursite.com
rootThree: /child/
pattern3: http://yoursite.com/child/

Pattern 4: NG

Settings

url: http://yoursite.com/
root: child

Result

const urlFour = "http://yoursite.com/".replace(/\/+$/, '');
const rootFour = "child".replace(/\/*$/, '/');

console.log('urlFour: ' + urlFour);
console.log('rootFour: ' + rootThree);
console.log('pattern4: ' + urlFour + rootFour + "\n");

// Result
urlFour: http://yoursite.com
rootFour: /child/
pattern4: http://yoursite.comchild/ <- it's wrong. But not relate this PR

My understanding is above. Is it okay??

About this PR

This PR is not change load_config.js.
Maybe this PR source code will not affect url + root path.

So, I think maybe this PR is not breaking change & merge safely.

How do you think? :)

@curbengh
Copy link
Contributor

url: http://yoursite.com/child
root: /child/

is the current approach. While I don't necessarily agree with that approach, unless the current approach is updated, this PR is a breaking change.


My suggestion is to switch to the following approach (a breaking change),

url: http://yoursite.com
root: /child

Then change,

config.url = config.url.replace(/\/+$/, '');

to

    config.url = config.url.replace(/\/+$/, '').concat(config.root);

This will also fix #3601, without this PR.

@curbengh
Copy link
Contributor

Maybe @sKawashima @yoshinorin haven't realize,
#3601 can also be easily fixed by,

url: http://yoursite.com/child
root: /child/

which is (I iterate again) the current approach. I understand @sKawashima might not like that approach, me neither. Most users either has "/" as root: value or grown accustomed to having "/child" in both url: and root:.

The rationale of using url: http://yoursite.com/child instead of url: http://yoursite.com/ (when root: is other than "/") is so that users can use <%= config.url %> to add a link to their home page, instead of using <%= config.url + config.root %>. I'm iterating my previous comment here.

If user wants to use,

url: http://yoursite.com/
root: /child/

without using <%= config.url + config.root %>, then hexo needs

config.url = config.url.replace(/\/+$/, '').concat(config.root);

in hexo/lib/hexo/load_config.js.


So, the real question is, should

url: http://yoursite.com/child
root: /child/

be deprecated, in favor of

url: http://yoursite.com/
root: /child/

?

@sKawashima
Copy link
Author

sKawashima commented Jul 12, 2019

@weyusi
So why does $ {ctx.config.url}, $ {ctx.config.root} and $ {this.path} exist in this source code?

I have tried it once, but I sent this PR because I had a few problems.
There are the behavior of link generation for share, the file arrangement at the time of generate, and the behavior of link at the time of rss generation in hexo-generator-feed.

I think that it is not consistent about url extraction of permalink and $ hexo generate.

@yoshinorin
Copy link
Member

yoshinorin commented Jul 13, 2019

@weyusi @sKawashima
It's very sorry. I perfectly misunderstood.
I completely thought _config.yml setting is already as following.

url: http://yoursite.com/
root: /child/

Sorry for the confusion...
I will read related issue & current source code again.

@curbengh
Copy link
Contributor

curbengh commented Jul 15, 2019

So why does $ {ctx.config.url}, $ {ctx.config.root} and $ {this.path} exist in this source code?

Good question,
when you have the following config,

url: http://yoursite.com/child
root: /child/

and your page is located at source/examplepage/index.md,

Variable Value
ctx.config.url http://yoursite.com/child
ctx.config.root /child/
page.path examplepage/index.html
page.permalink ctx.config.url + / + page.path =
http://yoursite.com/child/examplepage/index.html

As for the purpose of ctx.config.root, as mentioned in my previous comment, it serves as another way to link to a same-origin page or assets.

Instead of using permalink which results in <a href="http://yoursite.com/child/examplepage/index.html">, hexo also uses ctx.config.root + page.path which results in <a href="/child/examplepage/index.html">. I can't confirm the exact rationale, but my guess is this approach allows distinguish of local and third-party links.


If you can pinpoint the exact issue (e.g. expected urls vs actual urls), it can help us to troubleshoot.

@sKawashima
Copy link
Author

I'm sorry for the late reply.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Permalink not including root
4 participants