提交 405a33c9 编写于 作者: J Johan Preynat

Merge branch 'master' into summary/scrolling

{% macro articles(_articles) %} {% macro articles(_articles) %}
{% for article in _articles %} {% for article in _articles %}
<li class="chapter {% if article.path == file.path and not article.anchor %}active{% endif %}" data-level="{{ article.level }}" {% if article.path %}data-path="{{ article.path|resolveFile }}"{% endif %}> <li class="chapter {% if article.path == file.path and not article.anchor %}active{% endif %}" data-level="{{ article.level }}" {% if article.path %}data-path="{{ article.path|resolveFile }}"{% endif %}>
{% if article.path %} {% if article.path and getPageByPath(article.path) %}
<a href="{{ article.path|resolveFile }}{{ article.anchor }}"> <a href="{{ article.path|resolveFile }}{{ article.anchor }}">
{% elif article.url %} {% elif article.url %}
<a target="_blank" href="{{ article.url }}"> <a target="_blank" href="{{ article.url }}">
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"name": "gitbook-plugin-theme-default", "name": "gitbook-plugin-theme-default",
"description": "Default theme for GitBook", "description": "Default theme for GitBook",
"main": "./index.js", "main": "./index.js",
"version": "1.0.1", "version": "1.0.3",
"engines": { "engines": {
"gitbook": ">=3.0.0" "gitbook": ">=3.0.0"
}, },
......
...@@ -194,32 +194,45 @@ function handleNavigation(relativeUrl, push) { ...@@ -194,32 +194,45 @@ function handleNavigation(relativeUrl, push) {
prevUri = uri; prevUri = uri;
return loading.show($.get(uri) var promise = $.Deferred(function(deferred) {
.then(function (html) { $.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 // Replace html content
html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){ html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){
return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>'; return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>';
}); });
var $page = $(html); var $page = $(html),
var $pageHead = $page.find('[data-element=head]'); $pageBody = $page.find('.book'),
var $pageBody = $page.find('.book'); $pageHead;
// We only use history.pushState for pages generated with GitBook // We only use history.pushState for pages generated with GitBook
if ($pageBody.length === 0) { if ($pageBody.length === 0) {
return $.Deferred(function (deferred) {
var err = new Error('Invalid gitbook page, redirecting...'); var err = new Error('Invalid gitbook page, redirecting...');
deferred.reject(err); return deferred.reject(err);
}).promise();
} }
// Push url to history // Push url to history
if (push) { if (push) {
history.pushState({ history.pushState({
path: uri path: responseURL
}, null, uri); }, 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 // Merge heads
// !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...) // !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...)
...@@ -256,10 +269,19 @@ function handleNavigation(relativeUrl, push) { ...@@ -256,10 +269,19 @@ function handleNavigation(relativeUrl, push) {
if (hash) { if (hash) {
scrollToHash(hash); scrollToHash(hash);
} }
})
deferred.resolve();
}
});
}).promise();
return loading.show(
promise
.fail(function (e) { .fail(function (e) {
location.href = relativeUrl; console.log(e); // eslint-disable-line no-console
})); // location.href = relativeUrl;
})
);
} }
function updateNavigationPosition() { function updateNavigationPosition() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册