-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinline_helper.js
More file actions
169 lines (147 loc) · 4.75 KB
/
Copy pathinline_helper.js
File metadata and controls
169 lines (147 loc) · 4.75 KB
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
164
165
166
167
168
169
/* eslint-disable */
/* global smartdown */
var defaultHome = 'Home';
var baseURL = 'https://smartdown.site/';
if (typeof smartdownBaseURL === 'string') {
baseURL = smartdownBaseURL;
}
if (typeof smartdownDefaultHome === 'string') {
defaultHome = smartdownDefaultHome;
}
/* Stuff needed for inline-mode */
var scriptMap;
var gistPathPrefix = '';
var gistHashPrefix = 'gist/';
function cardLoaded(sourceText, cardKey, cardURL) {
document.body.scrollTop = 0; // For Chrome, Safari and Opera
document.documentElement.scrollTop = 0; // For IE and Firefox
/* eslint no-invalid-this: 0 */
multiparts = smartdown.partitionMultipart(sourceText);
var output = document.getElementById('smartdown-output');
smartdown.setHome(multiparts._default_, output, function() {
smartdown.startAutoplay(output);
inhibitHash = '#' + cardKey;
window.location.hash = inhibitHash;
});
}
function loadAsyncCard(cardKey, cardURL) {
// console.log('loadAsyncCard', cardKey, cardURL);
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
cardLoaded(this.responseText, cardKey, cardURL);
});
oReq.open("GET", cardURL);
oReq.send();
}
function relativeCardLoaderInline(cardKey) {
var re = `^/?(${gistPathPrefix})?${gistHashPrefix}([^/]+)/([^/]+)(/(\\w*))?$`;
var gistRE = new RegExp(re, 'g');
var match = gistRE.exec(cardKey);
if (match) {
gistOrg = match[2];
gistID = match[3];
var newCardKey = match[5] || 'Home';
// console.log('cardKey', cardKey, gistOrg, gistID, newCardKey);
cardKey = newCardKey;
// console.log('match', match, gistOrg, gistID, cardKey);
}
var part = multiparts[cardKey];
if (part) {
var output = document.getElementById('smartdown-output');
smartdown.setHome(part, output, function() {
smartdown.startAutoplay(output);
});
}
else {
var cardURL = window.location.origin + window.location.pathname + cardKey + '.md';
if (cardKey.indexOf('http') === 0) {
gistOrg = '';
gistID = '';
cardURL = cardKey;
loadAsyncCard(cardKey, cardURL);
}
else if (cardKey.indexOf('/') === 0) {
gistOrg = '';
gistID = '';
cardURL = cardKey;
loadAsyncCard(cardKey, cardURL);
}
else if (gistOrg !== '' && gistID !== '') {
var gistAPIBase = `https://api.github.com/gists/${gistID}`;
// console.log('gistAPIBase', gistAPIBase);
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
var gistResponse = JSON.parse(this.responseText);
// console.log('gist Response', gistResponse);
var gistFile = gistResponse.files[cardKey + '.md'];
// console.log('gistFile', gistFile);
var gistFileURL = gistFile.raw_url;
cardKey = `${gistHashPrefix}${gistOrg}/${gistID}/${cardKey}`;
loadAsyncCard(cardKey, gistFileURL);
});
oReq.open("GET", gistAPIBase);
oReq.send();
}
else {
gistOrg = '';
gistID = '';
if (!cardKey.endsWith('.md')) {
cardKey += '.md';
}
if (cardKey.indexOf('./') === 0) {
cardKey = cardKey.slice(2);
}
var text = scriptMap[cardKey] || '';
text = smartdown.decodeInlineScript(text);
cardLoaded(text, cardKey, cardURL);
}
}
}
function buildScriptMapThenLoadHome() {
smartdown.loadCardsFromDocumentScripts();
scriptMap = {};
smartdown.smartdownScripts.forEach(function(s) {
var text = s.text;
if (false) {
if (s.id === 'README.md') {
text += '\n\n---\n\n[Back to Home](:@Home)\n\n';
}
}
scriptMap[s.id] = text;
});
loadHome();
}
/* EndStuff needed for inline-mode */
var svgIcons = {
'hypercube': '/gallery/resources/Hypercube.svg',
'Hypercube': '/gallery/resources/Hypercube.svg',
'badge': '/gallery/resources/badge.svg',
'StalactiteStalagmite': '/gallery/resources/StalactiteStalagmite.svg',
'church': '/gallery/resources/church.svg',
'lighthouse': '/gallery/resources/lighthouse.svg',
'barn': '/gallery/resources/barn.svg',
'medieval-gate': '/gallery/resources/medieval-gate.svg'
};
var multiparts = {};
var inhibitHash = '';
var gistOrg = '';
var gistID = '';
function loadHome() {
var hash = window.parent.location.hash || window.location.hash;
if (hash === '') {
hash = defaultHome;
}
hash = hash.replace(/#/g, '');
relativeCardLoaderInline(hash);
}
var calcHandlers = smartdown.defaultCalcHandlers;
const replace = window.location.origin + window.location.pathname;
const linkRules = [
];
function locationHashChanged() {
if (inhibitHash !== location.hash) {
// relativeCardLoaderInline(location.hash.slice(1));
}
}
window.onhashchange = locationHashChanged;
smartdown.initialize(svgIcons, baseURL, buildScriptMapThenLoadHome, relativeCardLoaderInline, calcHandlers, linkRules);