Skip to content

Instantly share code, notes, and snippets.

@JixunMoe JixunMoe/typecho.gist.js
Last active Sep 2, 2015

Embed
What would you like to do?
Gist Support for Typecho (Modern browser only)
/*! MIT License: (C)opyright Jixun<http://jixun.org/> */
// Usage:
// gist:JixunMoe/cd168a63b7c5a1cc3d93
(function (window, document) {
addEventListener('DOMContentLoaded', main, false);
function createGistLink (gist) {
return create('a', {
href: 'https://gist.github.com/' + gist,
target: '_blank',
textContent: 'gist:' + gist
});
}
function isParentCodeOrPre (ref, bNoMoreCheck) {
var parent = getParent(ref);
var tagName = parent.tagName;
if (tagName == 'CODE' || tagName == 'PRE')
return true;
if (bNoMoreCheck)
return false;
return isParentCodeOrPre(parent, true);
}
function getParent(ref) {
return ref.parentNode || ref;
}
function insertAfter(node, ref) {
ref.parentNode.insertBefore(node, ref.nextSibling);
}
function create(tag, attr) {
var el = document.createElement(tag);
for(var x in attr) {
if (attr.hasOwnProperty(x)) {
el[x] = attr[x];
}
}
return el;
}
function appendHead (ref) {
document.head.appendChild(ref);
}
var gistHandlers = { };
function handleGistAppend (ref, gist) {
var name = '__GIST_' + gist.replace(/\W/g, '_');
var doInsert;
if (!gistHandlers[gist]) {
gistHandlers[gist] = [];
doInsert = 1;
}
gistHandlers[gist].push(ref);
window[name] = function (data) {
delete window[name];
gistHandlers[gist].forEach(function (ref) {
insertAfter(create('section', {
innerHTML: data.div
}), ref);
});
appendHead(create('link', {
rel: 'stylesheet',
href: data.stylesheet
}));
};
if (doInsert) {
appendHead(create('script', {
src: 'https://gist.github.com/' + gist + '.json?callback=' + name
}));
}
}
function main () {
var treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{ acceptNode: function(node) {
// return node.textContent.trim().length ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
return node.textContent.trim().length ? 1 : 2;
} },
false
);
var nodes = [];
while(node = treeWalker.nextNode()) {
nodes.push(node);
}
nodes.forEach(function (node) {
var text = node.textContent;
if (text)
procMatch(node, text, text.match(/gist:(\w+\/\w+)/i));
});
function procMatch (node, text, matchData) {
if (!matchData || isParentCodeOrPre(node))
return ;
var parent = getParent(node);
var lastPos = 0;
var lastRef = node;
text.replace(/gist:(\w+\/\w+)/ig, function (z, gist, pos) {
var ref = lastRef.splitText(pos - lastPos);
lastRef = ref.splitText(z.length);
parent.insertBefore(createGistLink(gist), lastRef);
parent.removeChild(ref);
lastPos = pos + z.length;
});
// 全文
if ('P' == parent.tagName && text == matchData[0]) {
handleGistAppend(parent, matchData[1]);
}
}
}
})(window, document);
/*! MIT License: (C)opyright Jixun<http://jixun.org/> */
!function(t,e){function n(t){return a("a",{href:"https://gist.github.com/"+t,target:"_blank",textContent:"gist:"+t})}function r(t,e){var n=i(t),o=n.tagName;return"CODE"==o||"PRE"==o?!0:e?!1:r(n,!0)}function i(t){return t.parentNode||t}function o(t,e){e.parentNode.insertBefore(t,e.nextSibling)}function a(t,n){var r=e.createElement(t);for(var i in n)n.hasOwnProperty(i)&&(r[i]=n[i]);return r}function c(t){e.head.appendChild(t)}function u(e,n){var r,i="__GIST_"+n.replace(/\W/g,"_");s[n]||(s[n]=[],r=1),s[n].push(e),t[i]=function(e){delete t[i],s[n].forEach(function(t){o(a("section",{innerHTML:e.div}),t)}),c(a("link",{rel:"stylesheet",href:e.stylesheet}))},r&&c(a("script",{src:"https://gist.github.com/"+n+".json?callback="+i}))}function f(){function t(t,e,o){if(o&&!r(t)){var a=i(t),c=0,f=t;e.replace(/gist:(\w+\/\w+)/gi,function(t,e,r){var i=f.splitText(r-c);f=i.splitText(t.length),a.insertBefore(n(e),f),a.removeChild(i),c=r+t.length}),"P"==a.tagName&&e==o[0]&&u(a,o[1])}}for(var o=e.createTreeWalker(e.body,NodeFilter.SHOW_TEXT,{acceptNode:function(t){return t.textContent.trim().length?1:2}},!1),a=[];node=o.nextNode();)a.push(node);a.forEach(function(e){var n=e.textContent;n&&t(e,n,n.match(/gist:(\w+\/\w+)/i))})}addEventListener("DOMContentLoaded",f,!1);var s={}}(window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.