diff --git a/plugins/jsonp-highlight/prism-jsonp-highlight.js b/plugins/jsonp-highlight/prism-jsonp-highlight.js index 4ce5ca5081..14ac205ad5 100644 --- a/plugins/jsonp-highlight/prism-jsonp-highlight.js +++ b/plugins/jsonp-highlight/prism-jsonp-highlight.js @@ -1,44 +1,67 @@ -(function() { - if ( !self.Prism || !self.document || !document.querySelectorAll || ![].filter) return; +(function () { + if (!self.Prism || !self.document || !document.querySelectorAll || ![].filter) return; + /** + * The list of adapter which will be used if `data-adapter` is not specified. + * + * @type {Array.<(response: any, pre?: HTMLPreElement) => string>} + */ var adapters = []; + + /** + * Adds a new function to the list of adapters. + * + * If the given adapter is already registered or not a function, nothing will happen. + * + * @param {(response: any, pre?: HTMLPreElement) => string} adapter The adapter to be registered. + */ function registerAdapter(adapter) { if (typeof adapter === "function" && !getAdapter(adapter)) { adapters.push(adapter); } } + /** + * Returns the given adapter itself, if registered, or a registered adapter with the given function name. + * + * If no fitting adapter is registered, `null` will be returned. + * + * @param {string|Function} adapter The adapter itself or the function name of an adapter. + * @returns {(response: any, pre?: HTMLPreElement) => string} A registered adapter or `null`. + */ function getAdapter(adapter) { if (typeof adapter === "function") { - return adapters.filter(function(fn) { return fn.valueOf() === adapter.valueOf()})[0]; + return adapters.filter(function (fn) { return fn.valueOf() === adapter.valueOf(); })[0]; } else if (typeof adapter === "string" && adapter.length > 0) { - return adapters.filter(function(fn) { return fn.name === adapter})[0]; + return adapters.filter(function (fn) { return fn.name === adapter; })[0]; } return null; } + /** + * Remove the given adapter or the first registered adapter with the given function name from the list of + * registered adapters. + * + * @param {string|Function} adapter The adapter itself or the function name of an adapter. + */ function removeAdapter(adapter) { - if (typeof adapter === "string") + if (typeof adapter === "string") { adapter = getAdapter(adapter); + } if (typeof adapter === "function") { var index = adapters.indexOf(adapter); - if (index >=0) { - adapters.splice(index,1); + if (index >= 0) { + adapters.splice(index, 1); } } } - Prism.plugins.jsonphighlight = { - registerAdapter: registerAdapter, - removeAdapter: removeAdapter, - highlight: highlight - }; registerAdapter(function github(rsp, el) { - if ( rsp && rsp.meta && rsp.data ) { - if ( rsp.meta.status && rsp.meta.status >= 400 ) { - return "Error: " + ( rsp.data.message || rsp.meta.status ); + if (rsp && rsp.meta && rsp.data) { + if (rsp.meta.status && rsp.meta.status >= 400) { + return "Error: " + (rsp.data.message || rsp.meta.status); } - else if ( typeof(rsp.data.content) === "string" ) { - return typeof(atob) === "function" + else if (typeof (rsp.data.content) === "string") { + return typeof (atob) === "function" ? atob(rsp.data.content.replace(/\s/g, "")) : "Your browser cannot decode base64"; } @@ -46,91 +69,99 @@ return null; }); registerAdapter(function gist(rsp, el) { - if ( rsp && rsp.meta && rsp.data && rsp.data.files ) { - if ( rsp.meta.status && rsp.meta.status >= 400 ) { - return "Error: " + ( rsp.data.message || rsp.meta.status ); + if (rsp && rsp.meta && rsp.data && rsp.data.files) { + if (rsp.meta.status && rsp.meta.status >= 400) { + return "Error: " + (rsp.data.message || rsp.meta.status); } - else { - var filename = el.getAttribute("data-filename"); - if (filename == null) { - // Maybe in the future we can somehow render all files - // But the standard