(function () { if (typeof Prism === 'undefined' || typeof document === 'undefined') { return; } /** * @callback Adapter * @param {any} response * @param {HTMLPreElement} [pre] * @returns {string | null} */ /** * The list of adapter which will be used if `data-adapter` is not specified. * * @type {Array<{adapter: Adapter, name: string}>} */ var adapters = []; /** * Adds a new function to the list of adapters. * * If the given adapter is already registered or not a function or there is an adapter with the given name already, * nothing will happen. * * @param {Adapter} adapter The adapter to be registered. * @param {string} [name] The name of the adapter. Defaults to the function name of `adapter`. */ function registerAdapter(adapter, name) { name = name || adapter.name; if (typeof adapter === 'function' && !getAdapter(adapter) && !getAdapter(name)) { adapters.push({ adapter: adapter, name: name }); } } /** * Returns the given adapter itself, if registered, or a registered adapter with the given name. * * If no fitting adapter is registered, `null` will be returned. * * @param {string|Function} adapter The adapter itself or the name of an adapter. * @returns {Adapter} A registered adapter or `null`. */ function getAdapter(adapter) { if (typeof adapter === 'function') { for (var i = 0, item; (item = adapters[i++]);) { if (item.adapter.valueOf() === adapter.valueOf()) { return item.adapter; } } } else if (typeof adapter === 'string') { // eslint-disable-next-line no-redeclare for (var i = 0, item; (item = adapters[i++]);) { if (item.name === adapter) { return item.adapter; } } } return null; } /** * Remove the given adapter or the first registered adapter with the given name from the list of * registered adapters. * * @param {string|Function} adapter The adapter itself or the name of an adapter. */ function removeAdapter(adapter) { if (typeof adapter === 'string') { adapter = getAdapter(adapter); } if (typeof adapter === 'function') { var index = adapters.findIndex(function (item) { return item.adapter === adapter; }); if (index >= 0) { adapters.splice(index, 1); } } } registerAdapter(function github(rsp) { 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' ? atob(rsp.data.content.replace(/\s/g, '')) : 'Your browser cannot decode base64'; } } return null; }, 'github'); 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); } var files = rsp.data.files; var filename = el.getAttribute('data-filename'); if (filename == null) { // Maybe in the future we can somehow render all files // But the standard