forked from tiddlyweb/twikifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webtwik.js
93 lines (86 loc) · 2.99 KB
/
webtwik.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
(function() {
var wikifiers = {};
var formatTiddler = function(jqtiddler) {
var server_host = jqtiddler.attr('server.host');
var host = server_host ? server_host : '/';
var recipe = jqtiddler.attr('server.recipe');
var bag = jqtiddler.attr('server.bag');
var title = jqtiddler.attr('title');
var collection_uri;
var tiddler_uri;
if (recipe) {
collection_uri = host
+ 'recipes/'
+ encodeURIComponent(recipe)
+ '/tiddlers';
tiddler_uri = host
+ 'recipes/'
+ encodeURIComponent(recipe)
+ '/tiddlers/'
+ encodeURIComponent(title)
+ '.json';
} else {
collection_uri = host
+ 'bags/'
+ encodeURIComponent(bag)
+ '/tiddlers';
tiddler_uri = host
+ 'bags/'
+ encodeURIComponent(bag)
+ '/tiddlers/'
+ encodeURIComponent(title)
+ '.json';
}
var useCache = false;
var globals, wikify, store, Tiddler;
if (wikifiers[collection_uri] === undefined) {
globals = createWikifier(window, $);
wikifiers[collection_uri] = globals;
} else {
globals = wikifiers[collection_uri];
useCache = true;
}
wikify = globals[0];
store = globals[1];
Tiddler = globals[2];
var loadTiddlerText = function(tiddler_div, tiddler_uri, title) {
pre_element = $(tiddler_div).find('pre').first();
text = unescape(pre_element.text());
pre_element.remove()
console.log('text', text);
if (text) {
wikify(text, tiddler_div, null, null);
} else {
$.ajax({
url: tiddler_uri,
type: 'GET',
success: function(data ,status, xhr) {
wikify(data.text, tiddler_div, null, null);
},
errror: function(xhr, error, exc) {
tiddler_div.innherHTML = xhr.statusText;
},
});
}
};
if (useCache) {
loadTiddlerText(jqtiddler[0], tiddler_uri, title);
} else {
$.ajax({
url: collection_uri + '.json?fat=1',
type: 'GET',
success: function(data, status, xhr) {
twik.loadRemoteTiddlers(store, Tiddler, collection_uri, data);
loadTiddlerText(jqtiddler[0], tiddler_uri, title);
},
error: function(xhr, error, exc) {
jqtiddler[0].innherHTML = xhr.statusText;
},
dataType: 'text',
});
}
};
$('.tiddler').each(function(index) {
formatTiddler($(this));
});
})();