Skip to content

Support for relative urls #53

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions htmlpreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ var HTMLPreview = {
previewform: document.getElementById('previewform'),

file: function() {
return location.search.substring(1); //Get everything after the ?
var url = location.search.substring(1); //Get everything after the ?
var referrer = document.referrer;
if ((url.startsWith('./') || url.startsWith('../')) && referrer) {
if (referrer.toLowerCase().endsWith('.md')) {
referrer = referrer.substring(0, referrer.lastIndexOf('/')+1);
}
if (! referrer.endsWith('/')) {
referrer = referrer + '/';
}
return referrer + url;
} else {
return url;
}
},

raw: function() {
Expand Down Expand Up @@ -68,7 +80,7 @@ var HTMLPreview = {
&& data.query.results.resources.status == 200) {
HTMLPreview.content = data.query.results.resources.content.replace(/<head>/i, '<head><base href="' + HTMLPreview.raw() + '">').replace(/<script( type=["'](text|application)\/javascript["'])?/gi, '<script type="text/htmlpreview"').replace(/<\/body>/i, '<script src="//' + location.hostname + '/htmlpreview.min.js"></script><script>HTMLPreview.replaceAssets();</script></body>').replace(/<\/head>\s*<frameset/gi, '<script src="//' + location.hostname + '/htmlpreview.min.js"></script><script>document.addEventListener("DOMContentLoaded",HTMLPreview.replaceAssets,false);</script></head><frameset'); //Add <base> just after <head> and inject <script> just before </body> or </head> if <frameset>
setTimeout(function() {
document.open();
document.open("text/html", "replace");
document.write(HTMLPreview.content);
document.close();
}, 50); //Delay updating document to have it cleared before
Expand Down
2 changes: 1 addition & 1 deletion htmlpreview.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
color: #666;
}
form {
padding: 20px;
padding: 20px;
text-align: center;
}
strong {
Expand All @@ -34,11 +34,10 @@
<form id="previewform" action="">
<h1>GitHub &amp; BitBucket HTML Preview</h1>
<p>Enter URL of the HTML file to preview: <input type="url" id="file" value="" placeholder="e.g. https://github.com/user/repo/blob/master/index.html" size="60" autofocus> <input type="submit" value="&raquo;"></p>
<p>or prepend to the URL: <strong>http://htmlpreview.github.io/?</strong>https://github.com/twbs/bootstrap/blob/gh-pages/2.3.2/index.html</p>
<p>or use this bookmarklet while browsing GitHub or BitBucket: <a href="javascript:void('http://htmlpreview.github.io/'==window.location?alert('Drag me to your bookmarks bar!'):window.location='http://htmlpreview.github.io/?'+window.location)"><strong>HTMLPreview</strong></a></p>
<p>or prepend to the URL: <strong>http://htmlpreviewer.github.io/?</strong>https://github.com/twbs/bootstrap/blob/gh-pages/2.3.2/index.html</p>
<p id="footer">Developed by <a href="https://github.com/niutech">niu tech</a> | Contribute on <a href="https://github.com/htmlpreview/htmlpreview.github.com">GitHub</a></p>
</form>
<script src="/htmlpreview.min.js"></script>
<script src="htmlpreview.min.js"></script>
<script>HTMLPreview.init();</script>
</body>
</html>
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ In order to use it, just prepend this fragment to the URL of any HTML file: **[h
- http://htmlpreview.github.io/?https://github.com/twbs/bootstrap/gh-pages/2.3.2/index.html
- http://htmlpreview.github.io/?https://github.com/documentcloud/backbone/blob/master/examples/todos/index.html

When used from a markdown file on github, it is also possible to use relative urls within the same repository. Very useful for linking from .md files to html files relative to the same revision in the repository. e.g.:

- https://htmlpreview.github.io/?../../raml/api.html#nodes__nodeid__gws_post

What it does is load HTML using YQL, then process all links, frames, scripts and styles, and load each of them using YQL, so they can be evaluted in the browser. Here is the workflow:
```
HTMLPreview.init() -> HTMLPreview.send(HTML) -> YQL fetch HTML -> HTMLPreview.loadHTML(data) -> HTMLPreview.replaceAssets() -> HTMLPreview.send(CSS) -> YQL fetch CSS -> HTMLPreview.loadCSS(data) -> HTMLPreview.send(JS) -> YQL fetch JS -> HTMLPreview.loadJS(data)
Expand Down