htmlpreview / htmlpreview.github.com

HTML Preview for GitHub Repositories
htmlpreview.github.com
1.51k stars 303 forks source link

External links with # gets broken. #133

Open Somya545 opened 1 year ago

Somya545 commented 1 year ago

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 commented 12 months ago

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:

https://github.com/htmlpreview/htmlpreview.github.com/blob/787b673198b8f13ac16384325cd380c5c68cf219/htmlpreview.js#L24-L25

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 :confused:

For now, I've had to work around the bug, using a URL shortener to preserve the anchor in the link: https://github.com/ZimbiX/brendan-weibrecht-resume/commit/4616a44089de5090586fa9d4dc34123919471fba

niutech commented 11 months ago

@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 commented 11 months ago

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

hoijui commented 5 months ago

See the new fork of this project mentioned in https://github.com/htmlpreview/htmlpreview.github.com/issues/139.

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