This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
page.js
75 lines (63 loc) · 2.02 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var $ = require('jquery');
var url = require('url');
var path = require('path');
var events = require('./events');
var started = false;
var state = {};
/*
Signal that page has changed, this function must be called by
themes after page is loaded and when navigation changed
*/
function hasChanged(ctx) {
console.log('page has changed', ctx); // eslint-disable-line no-console
setState(ctx);
if (!started) {
// Notify that gitbook is ready
started = true;
events.trigger('start', ctx.config.pluginsConfig);
}
events.trigger('page.change');
}
/*
Update current state
data-level="{{ page.level }}"
data-chapter-title="{{ page.title }}"
data-filepath="{{ file.path }}"
data-basepath="{{ './'|resolveFile }}"
data-revision="{{ gitbook.time }}"
data-innerlanguage="{{ innerlanguage }}">
*/
function setState(newState) {
// API since GitBook v3
state.page = newState.page;
state.file = newState.file;
state.gitbook = newState.gitbook;
state.config = newState.config;
state.basePath = newState.basePath;
state.book = newState.book;
// Deprecated
state.$book = $('.book');
state.revision = state.gitbook.time;
state.level = state.page.level;
state.filepath = state.file.path;
state.chapterTitle = state.page.title;
state.innerLanguage = state.book.language || '';
// Absolute url to the root of the book (inner book)
state.root = url.resolve(
location.protocol+'//'+location.host,
path.dirname(path.resolve(location.pathname.replace(/\/$/, '/index.html'), state.basePath))
).replace(/\/?$/, '/');
// Absolute root to the language (for multilingual book)
state.bookRoot = state.innerLanguage? url.resolve(state.root, '..') : state.root;
}
/*
Return state of current page
*/
function getState() {
return state;
}
module.exports = {
hasChanged: hasChanged,
setState: setState,
getState: getState
};