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

External links with # gets broken. #133

Open
Somya545 opened this issue Jul 5, 2023 · 4 comments
Open

External links with # gets broken. #133

Somya545 opened this issue Jul 5, 2023 · 4 comments

Comments

@Somya545
Copy link

Somya545 commented Jul 5, 2023

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

@ZimbiX
Copy link

ZimbiX commented Aug 9, 2023

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

@niutech
Copy link
Collaborator

niutech commented Aug 11, 2023

@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.

@ZimbiX
Copy link

ZimbiX commented Aug 11, 2023

@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.

agmt added a commit to agmt/htmlpreview.github.com that referenced this issue Aug 24, 2023
agmt added a commit to agmt/htmlpreview.github.com that referenced this issue Aug 24, 2023
@hoijui
Copy link

hoijui commented Feb 21, 2024

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.

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

No branches or pull requests

4 participants