Skip to content

Instantly share code, notes, and snippets.

@nauzilus
Last active September 10, 2015 01:35
Show Gist options
  • Save nauzilus/a216f6a5e5e74eec0fde to your computer and use it in GitHub Desktop.
Save nauzilus/a216f6a5e5e74eec0fde to your computer and use it in GitHub Desktop.
Prism Custom Themes
javascript:(function(t){t("https://api.github.com/gists/a216f6a5e5e74eec0fde","script.js","customthemes")}(function(t){return function(n,e,c){if(!t.getElementById(c)){var i=function(n){var e=t.createElement("script");n&&n(e),t.head.appendChild(e)},o=c+Math.floor(Date.now()/864e5);window[o]=function(t){i(function(n){n.id=c,n.innerHTML=t.data.files[e].content})},i(function(t){t.src=n+"?callback="+o})}}}(document)));
javascript:(function(gistJs){
gistJs("https://api.github.com/gists/a216f6a5e5e74eec0fde","script.js","customthemes")
})((function(d){
return function(url,file,id){
if (d.getElementById(id))
return;
var injectSript=function(callback){
var script=d.createElement("script");
callback && callback(script);
d.head.appendChild(script);
};
var callbackName=id+Math.floor(Date.now()/86400000);
window[callbackName]=function(data){
injectSript(function(script) {
script.id=id;
script.innerHTML=data.data.files[file].content
})
};
injectSript(function(script){
script.src=url+"?callback="+callbackName
});
}
})(document));
(function() {
var re = /^themes\/(.*)\.css$/;
var themesURL = "https://api.github.com/repos/PrismJS/prism-themes/git/trees/master?recursive=1";
var xhrJsonPromise = function(url) {
return new Promise(function(resolve) {
$u.xhr({
url: url,
callback: function(xhr) {
if (xhr.status < 400) {
resolve(JSON.parse(xhr.responseText));
}
}
});
});
}
var customThemes = {};
var themesPromise = xhrJsonPromise(themesURL).then(function(rsp) {
return Promise.all(
rsp.tree.filter(
function(file) { return re.exec(file.path) }
).map(function(theme) {
var m = re.exec(theme.path);
customThemes[theme.sha] = { id: m[1], css: null, title: m[1].replace("prism-","") };
return xhrJsonPromise(theme.url);
})
);
});
themesPromise.then(function(themes) {
var container = theme;
var customStyle = $u.element.create("style", { inside: document.head });
var prismStyle = $("link[href^='themes/prism']");
container.onclick = function() {
// wipe out our custom CSS in case a standard prism theme is selected
customStyle.textContent = "";
};
themes.forEach(function(theme) {
var def = customThemes[theme.sha];
def.content = atob(theme.content);
$u.element.create('input', {
properties: {
type: "radio",
name: "theme",
id: "theme=" + def.id,
value: def.id,
onclick: function (e) {
e.stopPropagation();
// set our custom CSS and standard prism theme to nothing (but in such a way code.js can still work)
customStyle.textContent = def.content;
prismStyle.href = "themes/prism-nothingtoseehere.css";
}
},
inside: container
});
$u.element.create('label', {
properties: {
htmlFor: 'theme=' + def.id
},
contents: def.title,
inside: container
});
});
}).catch(function(error) {
console.error(error);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment