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

baseUrl with relative prefix not working as expected. #1466

Closed
mjhenkes opened this issue Apr 9, 2019 · 15 comments · Fixed by #1526
Closed

baseUrl with relative prefix not working as expected. #1466

mjhenkes opened this issue Apr 9, 2019 · 15 comments · Fixed by #1526

Comments

@mjhenkes
Copy link

mjhenkes commented Apr 9, 2019

Describe the bug
For usage in github pages I'd like to use the baseUrl option to add a prefix of the project name for relative links. For instance with a github pages site like matt.github.io/derp with derp being the project name. I'd like relative links to change from /link to /derp/link.

Marked options

const options = {
    baseUrl: '/derp/',
}

Markdown

![alt text](/favicon.ico "relative")'

Output html

<p><img src="/derp/favicon.ico" alt="alt text" title="relative"></p>

What is actually happening is:

<p><img src="/derp//favicon.ico" alt="alt text" title="relative"></p>

Maybe it is as simple as not inverting rtrim here:
https://github.com/markedjs/marked/blob/master/lib/marked.js#L1417

To Reproduce
This code sandbox should show the issue. Check the console
https://codesandbox.io/s/9ql03o5114

I was able to reproduce this in both 0.5.0 and 0.6.2.

Expected behavior
I expect html output to look like this:

<p><img src="/derp/favicon.ico" alt="alt text" title="relative"></p>
@UziTech
Copy link
Member

UziTech commented Apr 9, 2019

could you remove the slash from the url?

![alt text](favicon.ico "relative")

@mjhenkes
Copy link
Author

mjhenkes commented Apr 9, 2019

Yeah, I think it would work as long as you never use the slash at the start of relative route. Do you know if there is a standard expectation on how to define the relative routes? Is the slash frowned upon? I noticed in my demo it would display the favicon with or without the slash.

@UziTech
Copy link
Member

UziTech commented Apr 9, 2019

I believe most servers treat consecutive slashes as a single slash so it shouldn't affect anything.

@mjhenkes
Copy link
Author

mjhenkes commented Apr 9, 2019

Good enough for me. Thanks for the response!

@mjhenkes mjhenkes closed this as completed Apr 9, 2019
@VitaliyR
Copy link

VitaliyR commented Jul 1, 2019

@UziTech not if the slashes are at the beginning of the line.

E.g.:
http://test.com//something/else.jpg -> http://something/else.jpg

@UziTech
Copy link
Member

UziTech commented Jul 1, 2019

@VitaliyR Where does http://test.com//something/else.jpg change to http://something/else.jpg?

@VitaliyR
Copy link

VitaliyR commented Jul 1, 2019

@UziTech it would not change if you will just request the website with this URL (like in address bar). But it will change if you will have a resource pointing to such url (e.g. img tag with src containing this url)

@UziTech
Copy link
Member

UziTech commented Jul 1, 2019

@VitaliyR It seems to work for me. If I enter http://test.com//something/else.jpg into the address bar it goes to http://test.com//something/else.jpg which shows the same content as http://test.com/something/else.jpg

@VitaliyR
Copy link

VitaliyR commented Jul 1, 2019

@UziTech please, try to use it in the actual HTML, not in the address bar. Like having a page with img tag with this url

@UziTech
Copy link
Member

UziTech commented Jul 1, 2019

are you talking about <img src="//something/else.jpg" /> going to http://something/else.jpg?

that doesn't seems to be the issue. marked only adds the extra slash after the base url so it will never starts with two slashes unless two slashes are specified without a base url like ![alt text](//favicon.ico "relative")

@VitaliyR
Copy link

VitaliyR commented Jul 1, 2019

const markdown = '![An Image](/images/pic01.jpg)';
const html = marked(markdown, { baseUrl: '/' }); // would be url with two slashes here

Doesn't it looks like bug? Isn't it supposed to do something like path.join instead concat?

@UziTech
Copy link
Member

UziTech commented Jul 1, 2019

I see. I guess in that case we should treat baseUrl: '/' the same as baseUrl: null

@VitaliyR
Copy link

VitaliyR commented Jul 1, 2019

@UziTech Yeah, great! And also baseUrls could be like '/something/' (please, notice the last slash). I think it would make the issue as well (due to concatenating baseUrl + url, not sure - not checked this one)

@UziTech
Copy link
Member

UziTech commented Jul 1, 2019

Ya in that case ![An Image](/images/pic01.jpg) would be /something//images/pic01.jpg which the browser interprets the same as /something/images/pic01.jpg.

I guess we could remove the extra slash.

@VitaliyR If you want to create a PR that fixes it I would be happy to review it.

@VitaliyR
Copy link

VitaliyR commented Jul 1, 2019

@UziTech #1513

@UziTech UziTech mentioned this issue Jul 18, 2019
5 tasks
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 a pull request may close this issue.

3 participants