1
1
( function ( ) {
2
2
if ( ! self . Prism || ! self . document || ! document . querySelectorAll || ! [ ] . filter ) return ;
3
3
4
+ /**
5
+ * @callback Adapter
6
+ * @param {any } response
7
+ * @param {HTMLPreElement } [pre]
8
+ * @returns {string }
9
+ */
10
+
4
11
/**
5
12
* The list of adapter which will be used if `data-adapter` is not specified.
6
13
*
7
- * @type {Array.<(response: any, pre?: HTMLPreElement) => string> }
14
+ * @type {Array.<{adapter: Adapter, name: string} > }
8
15
*/
9
16
var adapters = [ ] ;
10
17
11
18
/**
12
19
* Adds a new function to the list of adapters.
13
20
*
14
- * If the given adapter is already registered or not a function, nothing will happen.
21
+ * If the given adapter is already registered or not a function or there is an adapter with the given name already,
22
+ * nothing will happen.
15
23
*
16
- * @param {(response: any, pre?: HTMLPreElement) => string } adapter The adapter to be registered.
24
+ * @param {Adapter } adapter The adapter to be registered.
25
+ * @param {string } [name] The name of the adapter. Defaults to the function name of `adapter`.
17
26
*/
18
- function registerAdapter ( adapter ) {
19
- if ( typeof adapter === "function" && ! getAdapter ( adapter ) ) {
20
- adapters . push ( adapter ) ;
27
+ function registerAdapter ( adapter , name ) {
28
+ name = name || adapter . name ;
29
+ if ( typeof adapter === "function" && ! getAdapter ( adapter ) && ! getAdapter ( name ) ) {
30
+ adapters . push ( { adapter : adapter , name : name } ) ;
21
31
}
22
32
}
23
33
/**
24
- * Returns the given adapter itself, if registered, or a registered adapter with the given function name.
34
+ * Returns the given adapter itself, if registered, or a registered adapter with the given name.
25
35
*
26
36
* If no fitting adapter is registered, `null` will be returned.
27
37
*
28
- * @param {string|Function } adapter The adapter itself or the function name of an adapter.
29
- * @returns {(response: any, pre?: HTMLPreElement) => string } A registered adapter or `null`.
38
+ * @param {string|Function } adapter The adapter itself or the name of an adapter.
39
+ * @returns {Adapter } A registered adapter or `null`.
30
40
*/
31
41
function getAdapter ( adapter ) {
32
42
if ( typeof adapter === "function" ) {
33
- return adapters . filter ( function ( fn ) { return fn . valueOf ( ) === adapter . valueOf ( ) ; } ) [ 0 ] ;
43
+ for ( var i = 0 , item ; item = adapters [ i ++ ] ; ) {
44
+ if ( item . adapter . valueOf ( ) === adapter . valueOf ( ) ) {
45
+ return item . adapter ;
46
+ }
47
+ }
34
48
}
35
- else if ( typeof adapter === "string" && adapter . length > 0 ) {
36
- return adapters . filter ( function ( fn ) { return fn . name === adapter ; } ) [ 0 ] ;
49
+ else if ( typeof adapter === "string" ) {
50
+ for ( var i = 0 , item ; item = adapters [ i ++ ] ; ) {
51
+ if ( item . name === adapter ) {
52
+ return item . adapter ;
53
+ }
54
+ }
37
55
}
38
56
return null ;
39
57
}
40
58
/**
41
- * Remove the given adapter or the first registered adapter with the given function name from the list of
59
+ * Remove the given adapter or the first registered adapter with the given name from the list of
42
60
* registered adapters.
43
61
*
44
- * @param {string|Function } adapter The adapter itself or the function name of an adapter.
62
+ * @param {string|Function } adapter The adapter itself or the name of an adapter.
45
63
*/
46
64
function removeAdapter ( adapter ) {
47
65
if ( typeof adapter === "string" ) {
48
66
adapter = getAdapter ( adapter ) ;
49
67
}
50
68
if ( typeof adapter === "function" ) {
51
- var index = adapters . indexOf ( <
6D4E
span class="pl-s1">adapter) ;
69
+ var index = adapters . map ( function ( item ) { return item . adapter ; } ) . indexOf ( adapter ) ;
52
70
if ( index >= 0 ) {
53
71
adapters . splice ( index , 1 ) ;
54
72
}
67
85
}
68
86
}
69
87
return null ;
70
- } ) ;
88
+ } , 'github' ) ;
71
89
registerAdapter ( function gist ( rsp , el ) {
72
90
if ( rsp && rsp . meta && rsp . data && rsp . data . files ) {
73
91
if ( rsp . meta . status && rsp . meta . status >= 400 ) {
94
112
return "Error: unknown or missing gist file " + filename ;
95
113
}
96
114
return null ;
97
- } ) ;
115
+ } , 'gist' ) ;
98
116
registerAdapter ( function bitbucket ( rsp , el ) {
99
117
if ( rsp && rsp . node && typeof ( rsp . data ) === "string" ) {
100
118
return rsp . data ;
101
119
}
102
120
return null ;
103
- } ) ;
121
+ } , 'bitbucket' ) ;
104
122
105
123
var jsonpcb = 0 ,
106
124
loadMsg = "Loading\u2026" ;
158
176
}
159
177
else {
160
178
for ( var p in adapters ) {
161
- data = adapters [ p ] ( rsp , pre ) ;
179
+ data = adapters [ p ] . adapter ( rsp , pre ) ;
162
180
if ( data !== null ) {
163
181
break ;
164
182
}
185
203
} ;
186
204
187
205
highlight ( ) ;
188
- } ) ( ) ;
206
+ } ) ( ) ;
0 commit comments