-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.js
163 lines (133 loc) · 4.5 KB
/
docs.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const $ = document.querySelector.bind(document);
this.tmpl = function tmpl(element) {
return new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
"with(obj){p.push('" +
element.innerHTML
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%>").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
};
class MarkdownRenderer {
static render(content) {
if(content == null) return "";
return content
// Convert newlines to distinct paragraphs.
.split('\n').map(function(paragraph) {
return "<p>" + paragraph + "</p>";
}).join('')
// Convert backtick spans to code fragments.
.replace(/\`([^\`]*)\`/g, "<code>$1</code>");
}
}
class MystDoc {
constructor(sidebar_container, display_container) {
this.sidebar_container = sidebar_container;
this.display_container = display_container;
this.sidebar_generator = tmpl($('#sidebar_template'));
this.display_generator = tmpl($('#docs_template'));
this.content = {};
this.previous_content = null;
}
load(content) {
this.previous_content = this.content;
this.content = content;
}
// Lookup the given path in the doc structure
navigate_to(path) {
let content = this.content_for_path(path);
let page_has_changed = content != this.previous_content;
if(page_has_changed) {
this.sidebar_container.innerHTML = this.sidebar_generator({
root: this.content,
content: content,
misc: {
parent: this.parent_of_path(content.full_name),
page_has_changed: page_has_changed
},
});
}
this.display_container.innerHTML = this.display_generator({
root: this.content,
content: content,
misc: {
parent: this.parent_of_path(content.full_name),
page_has_changed: page_has_changed
},
});
this.set_active_elements(path);
this.previous_content = content;
}
set_active_elements(path) {
this.sidebar_container.querySelectorAll('.--active-element').forEach(function(element) {
element.classList.remove('--active-element');
});
let sidebar_element = this.sidebar_container.querySelector(`[id="${path}"]`);
let display_element = this.display_container.querySelector(`[id="${path}"]`);
if(sidebar_element) {
sidebar_element.classList.add('--active-element');
}
if(display_element) {
display_element.classList.add('--active-element');
}
}
content_for_path(path) {
// If the path is empty, just use the root.
if(path == "") {
return this.content;
}
let content = this.content;
const path_components = this.path_components_for(path);
for(var i = 0; i < path_components.length; i++) {
let path_component = path_components[i];
// Only modules and types have their own page. Constants, methods, etc.
// use their containing type/module as the content, then skip directly to
// that entry on the page.
let container = content.submodules && content.submodules[path_component] ||
content.subtypes && content.subtypes[path_component];
if(container) {
content = container;
}
}
return content;
}
path_components_for(path) {
return path.split(/\.|\#/);
}
parent_of_path(path) {
if(!path || path.length == 0) {
return null;
} else {
return path.split(/(?=\.|\#)/g).slice(0, -1).join('');
}
}
navigate_from_href() {
let full_name = decodeURIComponent(window.location.hash.replace(/^\#/, ''));
this.navigate_to(full_name);
let active_element = this.display_container.querySelector(`[id="${full_name}"] .hash_anchor`);
if(active_element) {
active_element.scrollIntoView();
}
};
};
const $docs = new MystDoc($('#sidebar'), $('#display'));
$render = {
constant: tmpl($('#constant_template')),
method: tmpl($('#method_template')),
sidebar_section: tmpl($('#sidebar_section_template')),
sidebar: tmpl($('#sidebar_template')),
sorted_list: tmpl($('#sorted_list_template'))
}
fetch('myst.json')
.then(function(response) { return response.json(); })
.then(function(data) {
$docs.load(data);
$docs.navigate_from_href();
});
window.addEventListener("hashchange", $docs.navigate_from_href.bind($docs), true);
window.addEventListener("popstate", $docs.navigate_from_href.bind($docs), true);