11( function ( ) {
22 if ( ! self . Prism || ! self . document || ! document . querySelectorAll || ! [ ] . filter ) return ;
33
4+ /**
5+ * @callback Adapter
6+ * @param {any } response
7+ * @param {HTMLPreElement } [pre]
8+ * @returns {string }
9+ */
10+
411 /**
512 * The list of adapter which will be used if `data-adapter` is not specified.
613 *
7- * @type {Array.<(response: any, pre?: HTMLPreElement) => string> }
14+ * @type {Array.<{adapter: Adapter, name: string} > }
815 */
916 var adapters = [ ] ;
1017
1118 /**
1219 * Adds a new function to the list of adapters.
1320 *
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.
1523 *
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`.
1726 */
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 } ) ;
2131 }
2232 }
2333 /**
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.
2535 *
2636 * If no fitting adapter is registered, `null` will be returned.
2737 *
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`.
3040 */
3141 function getAdapter ( adapter ) {
3242 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+ }
3448 }
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+ }
3755 }
3856 return null ;
3957 }
4058 /**
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
4260 * registered adapters.
4361 *
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.
4563 */
4664 function removeAdapter ( adapter ) {
4765 if ( typeof adapter === "string" ) {
4866 adapter = getAdapter ( adapter ) ;
4967 }
5068 if ( typeof adapter === "function" ) {
51- var index = adapters . indexOf ( adapter ) ;
69+ var index = adapters . map ( function ( item ) { return item . adapter ; } ) . indexOf ( adapter ) ;
5270 if ( index >= 0 ) {
5371 adapters . splice ( index , 1 ) ;
5472 }
6785 }
6886 }
6987 return null ;
70- } ) ;
88+ } , 'github' ) ;
7189 registerAdapter ( function gist ( rsp , el ) {
7290 if ( rsp && rsp . meta && rsp . data && rsp . data . files ) {
7391 if ( rsp . meta . status && rsp . meta . status >= 400 ) {
94112 return "Error: unknown or missing gist file " + filename ;
95113 }
96114 return null ;
97- } ) ;
115+ } , 'gist' ) ;
98116 registerAdapter ( function bitbucket ( rsp , el ) {
99117 if ( rsp && rsp . node && typeof ( rsp . data ) === "string" ) {
100118 return rsp . data ;
101119 }
102120 return null ;
103- } ) ;
121+ } , 'bitbucket' ) ;
104122
105123 var jsonpcb = 0 ,
106124 loadMsg = "Loading\u2026" ;
158176 }
159177 else {
160178 for ( var p in adapters ) {
161- data = adapters [ p ] ( rsp , pre ) ;
179+ data = adapters [ p ] . adapter ( rsp , pre ) ;
162180 if ( data !== null ) {
163181 break ;
164182 }
185203 } ;
186204
187205 highlight ( ) ;
188- } ) ( ) ;
206+ } ) ( ) ;
0 commit comments