diff --git a/_layouts/website/summary.html b/_layouts/website/summary.html index 0c6f9678cc66b55e7f98fb787bd7e6557923c9e6..e43c954ec443d3a3c991667e60a31fdda64c6687 100644 --- a/_layouts/website/summary.html +++ b/_layouts/website/summary.html @@ -1,7 +1,7 @@ {% macro articles(_articles) %} {% for article in _articles %}
  • - {% if article.path %} + {% if article.path and getPageByPath(article.path) %} {% elif article.url %} diff --git a/package.json b/package.json index 48efc036c808ce3a3abeb8c83a5d7b213ba3d66c..bd9d4670c8251ffb4d3f1f4ee1c8b5aa0db4e65c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "gitbook-plugin-theme-default", "description": "Default theme for GitBook", "main": "./index.js", - "version": "1.0.1", + "version": "1.0.3", "engines": { "gitbook": ">=3.0.0" }, diff --git a/src/js/theme/navigation.js b/src/js/theme/navigation.js index f02e6bc96c0ac1496e90bf8999ffaf056603e274..e9032d30a8036d4a00e54f2f35181a2718927607 100644 --- a/src/js/theme/navigation.js +++ b/src/js/theme/navigation.js @@ -194,72 +194,94 @@ function handleNavigation(relativeUrl, push) { prevUri = uri; - return loading.show($.get(uri) - .then(function (html) { - // Replace html content - html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){ - return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>'; + var promise = $.Deferred(function(deferred) { + $.ajax({ + type: 'GET', + url: uri, + cache: true, + headers:{ + 'Access-Control-Expose-Headers': 'X-Current-Location' + }, + success: function(html, status, xhr) { + // For GitBook.com, we handle redirection signaled by the server + var responseURL = xhr.getResponseHeader('X-Current-Location') || uri; + + // Replace html content + html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){ + return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>'; + }); + + var $page = $(html), + $pageBody = $page.find('.book'), + $pageHead; + + // We only use history.pushState for pages generated with GitBook + if ($pageBody.length === 0) { + var err = new Error('Invalid gitbook page, redirecting...'); + return deferred.reject(err); + } + + // Push url to history + if (push) { + history.pushState({ + path: responseURL + }, null, responseURL); + } + + // Force reparsing HTML to prevent wrong URLs in Safari + $page = $(html); + $pageHead = $page.find('[data-element=head]'); + $pageBody = $page.find('.book'); + + // Merge heads + // !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...) + + // Update title + document.title = $pageHead.find('title').text(); + + // Reference to $('head'); + var $head = $('head'); + + // Update next & prev tags + // Remove old + $head.find('link[rel=prev]').remove(); + $head.find('link[rel=next]').remove(); + + // Add new next * prev tags + $head.append($pageHead.find('link[rel=prev]')); + $head.append($pageHead.find('link[rel=next]')); + + // Merge body + var bodyClass = $('.book').attr('class'); + var scrollPosition = $('.book-summary').scrollTop(); + + $pageBody.toggleClass('with-summary', $('.book').hasClass('with-summary')); + + $('.book').replaceWith($pageBody); + $('.book').attr('class', bodyClass); + $('.book-summary').scrollTop(scrollPosition); + + // Update state + gitbook.state.$book = $('.book'); + preparePage(!hash); + + // Scroll to hashtag position + if (hash) { + scrollToHash(hash); + } + + deferred.resolve(); + } }); - - var $page = $(html); - var $pageHead = $page.find('[data-element=head]'); - var $pageBody = $page.find('.book'); - - // We only use history.pushState for pages generated with GitBook - if ($pageBody.length === 0) { - return $.Deferred(function (deferred) { - var err = new Error('Invalid gitbook page, redirecting...'); - deferred.reject(err); - }).promise(); - } - - // Push url to history - if (push) { - history.pushState({ - path: uri - }, null, uri); - } - - // Merge heads - // !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...) - - // Update title - document.title = $pageHead.find('title').text(); - - // Reference to $('head'); - var $head = $('head'); - - // Update next & prev tags - // Remove old - $head.find('link[rel=prev]').remove(); - $head.find('link[rel=next]').remove(); - - // Add new next * prev tags - $head.append($pageHead.find('link[rel=prev]')); - $head.append($pageHead.find('link[rel=next]')); - - // Merge body - var bodyClass = $('.book').attr('class'); - var scrollPosition = $('.book-summary').scrollTop(); - - $pageBody.toggleClass('with-summary', $('.book').hasClass('with-summary')); - - $('.book').replaceWith($pageBody); - $('.book').attr('class', bodyClass); - $('.book-summary').scrollTop(scrollPosition); - - // Update state - gitbook.state.$book = $('.book'); - preparePage(!hash); - - // Scroll to hashtag position - if (hash) { - scrollToHash(hash); - } - }) - .fail(function (e) { - location.href = relativeUrl; - })); + }).promise(); + + return loading.show( + promise + .fail(function (e) { + console.log(e); // eslint-disable-line no-console + // location.href = relativeUrl; + }) + ); } function updateNavigationPosition() {