switch to doneEach and cache #main dom

This commit is contained in:
Leopoldthecoder 2017-03-07 11:30:09 +08:00
parent 1aa6636ba8
commit 9c54bafae4
8 changed files with 27 additions and 33 deletions

View File

@ -306,3 +306,4 @@ window.$docsify = {
```
Note that if you are running an external script, e.g. an embedded jsfiddle demo, make sure to include the [external-script](plugins?id=external-script) plugin.

View File

@ -304,3 +304,4 @@ window.$docsify = {
```
注意如果执行的是一个外链脚本,比如 jsfiddle 的内嵌 demo请确保引入 [external-script](zh-cn/plugins?id=外链脚本-external-script) 插件。

View File

@ -161,7 +161,7 @@ function getNode (el, noCache) {
if ( noCache === void 0 ) noCache = false;
if (typeof el === 'string') {
el = noCache ? find(el) : (cacheNode[el] || find(el));
el = noCache ? find(el) : (cacheNode[el] || (cacheNode[el] = find(el)));
}
return el

2
lib/docsify.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -2,27 +2,22 @@ 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 () {
var container = Docsify.dom.getNode('#main');
var script = Docsify.dom.find(container, 'script');
function handleExternalScript (html) {
var scriptMatch = html.match(scriptReg);
if (script && script.src) {
var newScript = document.createElement('script');['src', 'async', 'defer'].forEach(function (attribute) {
newScript[attribute] = script[attribute];
});
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);
script.parentNode.removeChild(script);
container.appendChild(newScript);
}
}
var install = function (hook) {
hook.afterEach(handleExternalScript);
hook.doneEach(handleExternalScript);
};
window.$docsify.plugins = [].concat(install, window.$docsify.plugins);

View File

@ -1 +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)}();
this.D=this.D||{},function(){"use strict";function i(){var i=Docsify.dom.getNode("#main"),n=Docsify.dom.find(i,"script");if(n && n.src){var c=document.createElement("script");["src","async","defer"].forEach(function(i){c[i]=n[i]}),n.parentNode.removeChild(n),i.appendChild(c)}}var n=function(n){n.doneEach(i)};window.$docsify.plugins=[].concat(n,window.$docsify.plugins)}();

View File

@ -10,7 +10,7 @@ const cacheNode = {}
*/
export function getNode (el, noCache = false) {
if (typeof el === 'string') {
el = noCache ? find(el) : (cacheNode[el] || find(el))
el = noCache ? find(el) : (cacheNode[el] || (cacheNode[el] = find(el)))
}
return el

View File

@ -1,24 +1,21 @@
const scriptReg = /<script[^>]*src=["|'](.*)["|']>[^\w]*<\/script>/
const asyncReg = /<script[^>]*\s+async/
const deferReg = /<script[^>]*\s+defer/
function handleExternalScript () {
const container = Docsify.dom.getNode('#main')
const script = Docsify.dom.find(container, 'script')
function handleExternalScript (html) {
const scriptMatch = html.match(scriptReg)
if (script && script.src) {
const newScript = document.createElement('script')
if (scriptMatch && scriptMatch.length > 1) {
const script = document.createElement('script')
script.src = scriptMatch[1]
;['src', 'async', 'defer'].forEach(attribute => {
newScript[attribute] = script[attribute]
})
if (asyncReg.test(scriptMatch[0])) script.setAttribute('async', '')
if (deferReg.test(scriptMatch[0])) script.setAttribute('defer', '')
const target = document.querySelector('#main')
target.appendChild(script)
script.parentNode.removeChild(script)
container.appendChild(newScript)
}
}
const install = function (hook) {
hook.afterEach(handleExternalScript)
hook.doneEach(handleExternalScript)
}
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)