From 5340381d2835d79ccf6f13783a51408bdbdcdc6b Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 27 Mar 2018 10:34:52 -0500 Subject: [PATCH] fix initial hash not .md --- docs/index.html | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/docs/index.html b/docs/index.html index ebb3ccab..23c81d76 100644 --- a/docs/index.html +++ b/docs/index.html @@ -98,10 +98,11 @@ var content = document.querySelector('#content'); var body = document.querySelector('html'); - var currentPage = ''; + var currentPage = 'README.md'; var currentHash = ''; + var renderedPage = ''; - function hashChange(e) { + function hashChange() { var hash = location.hash.slice(1); if (!hash) { hash = 'README.md'; @@ -116,29 +117,36 @@ } else { currentHash = ''; } - e && e.preventDefault(); - fetchPage(uri[0]) - .then(function () { - if(currentHash) { - var hashElement = document.getElementById(currentHash); - if (hashElement) { - hashElement.scrollIntoView(); - } - } - }); } else { currentHash = uri[0]; } + fetchPage(currentPage) + .then(function () { + if(currentHash) { + var hashElement = document.getElementById(currentHash); + if (hashElement) { + hashElement.scrollIntoView(); + } + } + }); + history.replaceState('', document.title, '#' + currentPage + (currentHash ? '#' + currentHash : '')); } - window.addEventListener('hashchange', hashChange); + window.addEventListener('hashchange', function (e) { + e.preventDefault(); + hashChange(); + }); function fetchPage(page) { + if (page === renderedPage) { + return Promise.resolve(); + } return fetch(page) .then(function (res) { return res.text(); }) .then(function (text) { + renderedPage = page; content.innerHTML = marked(text); body.scrollTop = 0; }).catch(function (e) {