add external-script plugin

This commit is contained in:
Leopoldthecoder 2017-03-06 23:57:28 +08:00
commit 1aa6636ba8
6 changed files with 72 additions and 1 deletions

View file

@ -0,0 +1,30 @@
this.D = this.D || {};
(function () {
'use strict';
var scriptReg = /<script[^>]*src=["|'](.*)["|']>[^\w]*<\/script>/;
var asyncReg = /<script[^>]*\s+async/;
var deferReg = /<script[^>]*\s+defer/;
function handleExternalScript (html) {
var scriptMatch = html.match(scriptReg);
if (scriptMatch && scriptMatch.length > 1) {
var script = document.createElement('script');
script.src = scriptMatch[1];
if (asyncReg.test(scriptMatch[0])) { script.setAttribute('async', ''); }
if (deferReg.test(scriptMatch[0])) { script.setAttribute('defer', ''); }
var target = document.querySelector('#main');
target.appendChild(script);
}
}
var install = function (hook) {
hook.afterEach(handleExternalScript);
};
window.$docsify.plugins = [].concat(install, window.$docsify.plugins);
}());

1
lib/plugins/external-script.min.js vendored Normal file
View file

@ -0,0 +1 @@
this.D=this.D||{},function(){"use strict";function t(t){var i=t.match(c);if(i&&i.length>1){var r=document.createElement("script");r.src=i[1],e.test(i[0])&&r.setAttribute("async",""),s.test(i[0])&&r.setAttribute("defer","");var n=document.querySelector("#main");n.appendChild(r)}}var c=/<script[^>]*src=["|'](.*)["|']>[^\w]*<\/script>/,e=/<script[^>]*\s+async/,s=/<script[^>]*\s+defer/,i=function(c){c.afterEach(t)};window.$docsify.plugins=[].concat(i,window.$docsify.plugins)}();