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

Merge branch 'master' into summary/scrolling

{% macro articles(_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 %}>
{% if article.path %}
{% if article.path and getPageByPath(article.path) %}
<a href="{{ article.path|resolveFile }}{{ article.anchor }}">
{% elif article.url %}
<a target="_blank" href="{{ article.url }}">
......
......@@ -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"
},
......
......@@ -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 <link> tags
// Remove old
$head.find('link[rel=prev]').remove();
$head.find('link[rel=next]').remove();
// Add new next * prev <link> 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 <link> tags
// Remove old
$head.find('link[rel=prev]').remove();
$head.find('link[rel=next]').remove();
// Add new next * prev <link> 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() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册