htmlpreview / htmlpreview.github.com

HTML Preview for GitHub Repositories
htmlpreview.github.com
1.55k stars 312 forks source link

htmlpreview rewrites links that use htmlpreview #48

Open timmmooney opened 7 years ago

timmmooney commented 7 years ago

The following html preview:

http://htmlpreview.github.io/?https://github.com/epics-modules/softGlueZynq/blob/master/documentation/softGlueZynqDoc.html

contains a link (in the first paragraph, the word "autosave") that reads, in softGlueZynqDoc.html, as follows:

https://htmlpreview.github.io/?https://github.com/epics-modules/autosave/blob/R5-7-1/documentation/autoSaveRestore.html#configMenu

but if you click on it you get this:

https://htmlpreview.github.io/?https://github.com/epics-modules/softGlueZynq/blob/master/documentation/softGlueZynqDoc.html#configMenu

mcarbonneaux commented 2 years ago

i've the same problem!

for exemple with the page: https://htmlpreview.github.io/?https://raw.githubusercontent.com/FastCGI-Archives/fcgi2/master/doc/fastcgi-prog-guide/ap_guide.htm

they rewrite all the anchor as local page anchor (of ap_guide.htm) event if is relative href to the same git repository (for exemple ch1intro.htm)...

i think the problem are her: https://github.com/htmlpreview/htmlpreview.github.com/blob/787b673198b8f13ac16384325cd380c5c68cf219/htmlpreview.js#L24

they not support relative link (local to the git repository) with anchor, because they rewrite it systematicly with the base page name.

is because if detect # they consider as an anchor, and rewrite the anchor systematicly by removing the href page name and keep only the anchor part:

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

they need to check if is relative url before rewrite anchor like this...

like that :

var pat = /^(?:[a-z+]+:)?(\/\/|\/)/i;
if (href.indexOf('#') > 0&&pat.text(href)) { //Check if it's an anchor and absolute url

this are absolute:

http://example.com
HTTP://EXAMPLE.COM
https://www.exmaple.com
ftp://example.com/file.txt
//cdn.example.com/lib.js
git+ssh://example.con/itemscheme
/myfolder/test.txt

this are relative.

test
relative/test
./test
SergeyMosin commented 1 year ago

Same here all links with fragment part (# + text) are broken