Created
July 26, 2019 07:12
-
-
Save riccardobl/28e53e57bbfa62fde69b9f9decea1346 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head></head> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.7.0/marked.min.js" integrity="sha256-0Ed5s/n37LIeAWApZmZUhY9icm932KvYkTVdJzUBiI4=" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/1.0.11/purify.min.js" integrity="sha256-80n5dmervCuGISioxGlsumrwgMc8LJKT0wZybkLtkLM=" crossorigin="anonymous"></script> | |
<script> | |
function loadGithubReadme(repo,branch,callback){ | |
if(repo.endsWith("/"))repo=repo.substring(0,repo.length-1); | |
console.log("Repo:",repo); | |
console.log("Branch:",branch); | |
var readmeLink=repo+"/"+branch+"/README.md"; | |
if(readmeLink.startsWith("https://github.com/")) readmeLink=readmeLink.substring("https://github.com/".length); | |
readmeLink="https://raw.githubusercontent.com/"+readmeLink | |
console.log("Raw README.md",readmeLink); | |
var xhttp=new XMLHttpRequest(); | |
xhttp.open("GET",readmeLink, true); | |
xhttp.send(); | |
xhttp.onload = function() { | |
if (xhttp.status === 200) { | |
var resp=xhttp.responseText; | |
// Render markdown | |
resp=marked(resp); | |
// Sanitize | |
resp=DOMPurify.sanitize(resp, { | |
ALLOWED_TAGS: ['h1','h2','h3','h4','h5','h6','h7','h8', | |
'b','i','del','strike','strong','em','sup','sub','ins','summary', | |
'p','div','span','ol','ul','li', 'code','pre','a','blockquote','img', | |
'table','thead','tbody','td','th','tr' | |
], | |
ALLOWED_ATTR: ['href','src','width','height','align'], | |
RETURN_DOM_FRAGMENT: true | |
}); | |
// Make links relative to the github repo and not to this page. | |
resp.querySelectorAll("a").forEach(function(el){ | |
el.setAttribute("target","_blank"); // Force links to open on new tab | |
var href=el.getAttribute("href"); | |
if(!href.startsWith("http://")&&!href.startsWith("https://")){ | |
el.href=repo+"/blob/"+branch+"/"+href; | |
console.log("Replace link with",href); | |
} | |
}); | |
callback(resp); | |
}else callback(null); | |
} | |
} | |
</script> | |
<script> | |
loadGithubReadme("https://github.com/jmePhonon/jmePhonon","master",function(result){ | |
if(!result){ | |
alert("Can't load readme"); | |
}else{ | |
document.body.append(result) | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment