htmlpreview / htmlpreview.github.com

HTML Preview for GitHub Repositories

Home Page:htmlpreview.github.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

External links with # gets broken.

Somya545 opened this issue · comments

Hi, I am trying to preview a link with # the link doesn't open and seems to be broken.

https://htmlpreview.github.io/?https://github.com/userID/blob/master/vignettes/Testing.html#
Attaching an image below for reference.

image

But, once I remove the "#" from the same link and try to access, the link opens.

image

I ran into this bug as well, and it's pretty surprising and frustrating.

When viewing my page:

https://htmlpreview.github.io/?https://github.com/ZimbiX/brendan-weibrecht-resume/blob/master/build/brendan-weibrecht-resume.html

where I had a link to this URL:

https://github.com/docker/for-linux/issues/1015#issuecomment-841915668

The bug replaces it with:

https://htmlpreview.github.io/?https://github.com/ZimbiX/brendan-weibrecht-resume/blob/master/build/brendan-weibrecht-resume.html#issuecomment-841915668

The problem code is here:

if (href.indexOf('#') > 0) { //Check if it's an anchor
a[i].href = '//' + location.hostname + location.pathname + location.search + '#' + a[i].hash.substring(1); //Then rewrite URL with support for empty anchor

I don't really know what the usecase of this URL replacement is. The condition seems to assume the link is to an anchor within the current page; but in my case, it's on an external URL.

I was going to attempt a fix myself, but without a test suite, I don't know what functionality I'd be breaking 😕

For now, I've had to work around the bug, using a URL shortener to preserve the anchor in the link: ZimbiX/brendan-weibrecht-resume@4616a44

@ZimbiX That line 25 is for cases when the anchor is targeting a section on the same page, i.e. <a href="#section"> - we don't want to rewrite such URLs using htmlpreview.github.io.
However, there should be an added check: if there is an anchor and the URL before # is not the same as the original, then add the rewrite.

@niutech Thanks for your response. I think I could have expessed myself clearer. I understood that links like <a href="#section"> are sensibly left alone (href.indexOf('#') > 0 is false). But what kind of link is supposed to be matched and adjusted by line 25? Just links where the URL, minus the anchor, is the same as the current page? But can this app actually even display such a page where that kind of link would've worked in the original page? Like, we're fetching a HTML file from GitHub and it has no URL that can actually display the page, right? Hence the reason for creating this app?

I can't yet see a valid usecase for this particular replacement code; if there isn't one, we should probably remove it.

See the new fork of this project mentioned in #139.

@agmt's fix is already included there. Should further fixes be necessary, feel free to propose or report them there.