From 23bf2447ec420545f008cbb5127efedab01dfa6c Mon Sep 17 00:00:00 2001 From: Geert Audenaert Date: Fri, 2 Jun 2017 14:10:07 +0200 Subject: [PATCH 1/3] Support for relative urls Added url rewriting to support linking to html files relative to .md documentation in the same repository of a specific revision. --- htmlpreview.js | 11 ++++++++++- htmlpreview.min.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/htmlpreview.js b/htmlpreview.js index 44629b0..f74eda6 100644 --- a/htmlpreview.js +++ b/htmlpreview.js @@ -5,7 +5,16 @@ 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); + } + return referrer + url; + } else { + return url; + } }, raw: function() { diff --git a/htmlpreview.min.js b/htmlpreview.min.js index 63f4d20..e85cfca 100644 --- a/htmlpreview.min.js +++ b/htmlpreview.min.js @@ -1 +1 @@ -var HTMLPreview={content:"",previewform:document.getElementById("previewform"),file:function(){return location.search.substring(1)},raw:function(){return HTMLPreview.file().replace(/\/\/github\.com/,"//raw.githubusercontent.com").replace(/\/blob\//,"/")},replaceAssets:function(){var a,b,c;a=document.querySelectorAll("iframe[src],frame[src]");for(b=0;b/i,'').replace(//i,' + 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) From 59f042eda992cd1c16a4b8ee0e1bece94dab63e4 Mon Sep 17 00:00:00 2001 From: Geert Audenaert Date: Fri, 2 Jun 2017 18:26:56 +0200 Subject: [PATCH 3/3] Fixed back button --- htmlpreview.js | 5 ++++- htmlpreview.min.js | 2 +- index.html | 3 +-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/htmlpreview.js b/htmlpreview.js index f74eda6..2e64963 100644 --- a/htmlpreview.js +++ b/htmlpreview.js @@ -11,6 +11,9 @@ var HTMLPreview = { if (referrer.toLowerCase().endsWith('.md')) { referrer = referrer.substring(0, referrer.lastIndexOf('/')+1); } + if (! referrer.endsWith('/')) { + referrer = referrer + '/'; + } return referrer + url; } else { return url; @@ -77,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