Skip to content

Instantly share code, notes, and snippets.

@riccardobl
Created July 26, 2019 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardobl/28e53e57bbfa62fde69b9f9decea1346 to your computer and use it in GitHub Desktop.
Save riccardobl/28e53e57bbfa62fde69b9f9decea1346 to your computer and use it in GitHub Desktop.
<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