diff --git a/htmlpreview.js b/htmlpreview.js index 44629b0..2e64963 100644 --- a/htmlpreview.js +++ b/htmlpreview.js @@ -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() { @@ -68,7 +80,7 @@ var HTMLPreview = { && data.query.results.resources.status == 200) { HTMLPreview.content = data.query.results.resources.content.replace(//i, '').replace(//i, '').replace(/<\/head>\s* just after and inject + diff --git a/readme.md b/readme.md index 3270148..2618bd6 100644 --- a/readme.md +++ b/readme.md @@ -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)