Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Mar 22, 2018
1 parent f5a4add commit 62b697a
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,86 @@ <h1>Marked.js Documentation</h1>
<script>
var content = document.querySelector('#content');
var body = document.querySelector('html');
var currentPage = '';
var currentHash = '';

content.addEventListener('click', function (e) {
var a = e.target;
if (a.tagName.toLowerCase() === 'a' && a.href.indexOf(location.origin) === 0) {
var page = a.href.slice(location.origin.length + location.pathname.length);
if (page.slice(-3) === '.md') {
if (/\.md$/.test(page)) {
e.preventDefault();
fetchPage(page);
location.hash = page;
}
}
}, false);

function fetchPage(page) {
fetch(page)
.then(res => res.text())
.then(text => {
content.innerHTML = marked(text);
body.scrollTop = 0;
}).catch(e => {
content.innerHTML = '<p>Oops! There was a problem rendering the page.</p>'
+ '<p>' + e.message + '</p>';
function fetchHtml(page) {
if (page === currentPage) {
return Promise.resolve(content.innerHTML);
}

return fetch(page)
.then(function(res) { return res.text(); })
.then(function(text) { return marked(text); })
.catch(function(e) {
return '<p>Oops! There was a problem rendering the page.</p>'
+ '<p>' + e.message + '</p>';
});
}

fetchPage('README.md');

// TODO: -> ''
// TODO: -> '#installation'
// TODO: -> '#README.md'
// TODO: -> '#README.md/installation'
// TODO: '#installation' -> ''
// TODO: '#installation' -> '#README.md'
// TODO: '#installation' -> '#README.md/installation'
// TODO: '#README.md' -> ''
// TODO: '#README.md' -> '#installation'
// TODO: '#README.md' -> '#README.md/installation'
// TODO: '#README.md/installation' -> ''
// TODO: '#README.md/installation' -> '#installation'
// TODO: '#README.md/installation' -> '#README.md'

function hashChange(e) {
if (e) {
e.preventDefault();
}

var hash = location.hash.slice(1);

if (!hash) {
location.hash = "README.md";
return;
}

var path = hash.split('/');

if (/\.md$/.test(path[0])) {

fetchHtml(path[0])
.then(function (html) {
currentPage = path[0];
content.innerHTML = html;
if (path.length > 1) {
location.hash = path[1];
} else {
body.scrollTop = 0;
}
});
} else {
if (currentPage) {
location.hash = currentPage + "/" + path[0];
} else {
location.hash = "";
}
}
}

window.addEventListener("hashchange", hashChange);

hashChange();
</script>
</body>
</html>
</html>

0 comments on commit 62b697a

Please # to comment.