From 1aa6636ba82e117dd7812356c48ddc39619ad930 Mon Sep 17 00:00:00 2001 From: Leopoldthecoder Date: Mon, 6 Mar 2017 23:57:28 +0800 Subject: [PATCH 1/2] add external-script plugin --- build/build.js | 3 ++- docs/plugins.md | 7 +++++++ docs/zh-cn/plugins.md | 8 ++++++++ lib/plugins/external-script.js | 30 ++++++++++++++++++++++++++++++ lib/plugins/external-script.min.js | 1 + src/plugins/external-script.js | 24 ++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 lib/plugins/external-script.js create mode 100644 lib/plugins/external-script.min.js create mode 100644 src/plugins/external-script.js diff --git a/build/build.js b/build/build.js index b01ee22b..6834167c 100644 --- a/build/build.js +++ b/build/build.js @@ -34,7 +34,8 @@ build({ var plugins = [ { name: 'search', entry: 'search/index.js', moduleName: 'Search' }, { name: 'ga', entry: 'ga.js', moduleName: 'GA' }, - { name: 'emoji', entry: 'emoji.js', moduleName: 'Emoji' } + { name: 'emoji', entry: 'emoji.js', moduleName: 'Emoji' }, + { name: 'external-script', entry: 'external-script.js', moduleName: 'ExternalScript' } // { name: 'front-matter', entry: 'front-matter/index.js', moduleName: 'FrontMatter' } ] diff --git a/docs/plugins.md b/docs/plugins.md index 3142694c..4621d34c 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -75,3 +75,10 @@ The default is to support parsing emoji. For example `:100:` will be parsed to : ``` +## External Script + +If the script on the page is an external one (imports a js file via `src` attribute), you'll need this plugin to make it work. + +```html + +``` diff --git a/docs/zh-cn/plugins.md b/docs/zh-cn/plugins.md index 5fa1530a..5b35d9d7 100644 --- a/docs/zh-cn/plugins.md +++ b/docs/zh-cn/plugins.md @@ -71,3 +71,11 @@ ```html ``` + +## 外链脚本 - External Script + +如果文档里的 script 是内联脚本,可以直接执行;而如果是外链脚本(即 js 文件内容由 `src` 属性引入),则需要使用此插件。 + +```html + +``` diff --git a/lib/plugins/external-script.js b/lib/plugins/external-script.js new file mode 100644 index 00000000..3c38e8f2 --- /dev/null +++ b/lib/plugins/external-script.js @@ -0,0 +1,30 @@ +this.D = this.D || {}; +(function () { +'use strict'; + +var scriptReg = /]*src=["|'](.*)["|']>[^\w]*<\/script>/; +var asyncReg = /]*\s+async/; +var deferReg = /]*\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); + +}()); diff --git a/lib/plugins/external-script.min.js b/lib/plugins/external-script.min.js new file mode 100644 index 00000000..a7fd791e --- /dev/null +++ b/lib/plugins/external-script.min.js @@ -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=/]*src=["|'](.*)["|']>[^\w]*<\/script>/,e=/]*\s+async/,s=/]*\s+defer/,i=function(c){c.afterEach(t)};window.$docsify.plugins=[].concat(i,window.$docsify.plugins)}(); diff --git a/src/plugins/external-script.js b/src/plugins/external-script.js new file mode 100644 index 00000000..8391079c --- /dev/null +++ b/src/plugins/external-script.js @@ -0,0 +1,24 @@ +const scriptReg = /]*src=["|'](.*)["|']>[^\w]*<\/script>/ +const asyncReg = /]*\s+async/ +const deferReg = /]*\s+defer/ + +function handleExternalScript (html) { + const scriptMatch = html.match(scriptReg) + + if (scriptMatch && scriptMatch.length > 1) { + const script = document.createElement('script') + script.src = scriptMatch[1] + + if (asyncReg.test(scriptMatch[0])) script.setAttribute('async', '') + if (deferReg.test(scriptMatch[0])) script.setAttribute('defer', '') + + const target = document.querySelector('#main') + target.appendChild(script) + } +} + +const install = function (hook) { + hook.afterEach(handleExternalScript) +} + +window.$docsify.plugins = [].concat(install, window.$docsify.plugins) From 9c54bafae48c54ed412940bdcfd2390889e46849 Mon Sep 17 00:00:00 2001 From: Leopoldthecoder Date: Tue, 7 Mar 2017 11:30:09 +0800 Subject: [PATCH 2/2] switch to doneEach and cache #main dom --- docs/configuration.md | 1 + docs/zh-cn/configuration.md | 1 + lib/docsify.js | 2 +- lib/docsify.min.js | 2 +- lib/plugins/external-script.js | 25 ++++++++++--------------- lib/plugins/external-script.min.js | 2 +- src/core/util/dom.js | 2 +- src/plugins/external-script.js | 25 +++++++++++-------------- 8 files changed, 27 insertions(+), 33 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 6b2c770a..c418aaf6 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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. diff --git a/docs/zh-cn/configuration.md b/docs/zh-cn/configuration.md index a474f7d3..103e8843 100644 --- a/docs/zh-cn/configuration.md +++ b/docs/zh-cn/configuration.md @@ -304,3 +304,4 @@ window.$docsify = { ``` +注意如果执行的是一个外链脚本,比如 jsfiddle 的内嵌 demo,请确保引入 [external-script](zh-cn/plugins?id=外链脚本-external-script) 插件。 diff --git a/lib/docsify.js b/lib/docsify.js index 776c9acc..ea391830 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -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 diff --git a/lib/docsify.min.js b/lib/docsify.min.js index 1837ca31..7062af93 100644 --- a/lib/docsify.min.js +++ b/lib/docsify.min.js @@ -1,2 +1,2 @@ -!function(){"use strict";function e(e){var t=Object.create(null);return function(n){var r=t[n];return r||(t[n]=e(n))}}function t(e){return"string"==typeof e||"number"==typeof e}function n(){}function r(e){return"function"==typeof e}function i(e){var t=["init","mounted","beforeEach","afterEach","doneEach","ready"];e._hooks={},e._lifecycle={},t.forEach(function(t){var n=e._hooks[t]=[];e._lifecycle[t]=function(e){return n.push(e)}})}function a(e,t,r,i){void 0===i&&(i=n);var a=r,o=e._hooks[t],s=function(e){var t=o[e];if(e>=o.length)i(a);else if("function"==typeof t)if(2===t.length)t(r,function(t){a=t,s(e+1)});else{var n=t(r);a=void 0!==n?n:a,s(e+1)}else s(e+1)};s(0)}function o(e,t){return void 0===t&&(t=!1),"string"==typeof e&&(e=t?s(e):be[e]||s(e)),e}function s(e,t){return t?e.querySelector(t):ye.querySelector(e)}function l(e,t){return[].slice.call(t?e.querySelectorAll(t):ye.querySelectorAll(e))}function u(e,t){return e=ye.createElement(e),t&&(e.innerHTML=t),e}function c(e,t){return e.appendChild(t)}function p(e,t){return e.insertBefore(t,e.children[0])}function h(e,t,n){r(t)?window.addEventListener(e,t):e.addEventListener(t,n)}function d(e,t,n){r(t)?window.removeEventListener(e,t):e.removeEventListener(t,n)}function g(e,t,n){e&&e.classList[n?t:"toggle"](n||t)}function f(e){var t={};return(e=e.trim().replace(/^(\?|#|&)/,""))?(e.split("&").forEach(function(e){var n=e.replace(/\+/g," ").split("=");t[n[0]]=Ce(n[1])}),t):t}function m(e){var t=[];for(var n in e)t.push((Ee(n)+"="+Ee(e[n])).toLowerCase());return t.length?"?"+t.join("&"):""}function v(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return je(e.join("/"))}function b(e){var t=window.location.href.indexOf("#");window.location.replace(window.location.href.slice(0,t>=0?t:0)+"#"+e)}function y(){var e=k();return e=Oe(e),"/"===e.charAt(0)?b(e):void b("/"+e)}function k(){var e=window.location.href,t=e.indexOf("#");return t===-1?"":e.slice(t+1)}function w(e){void 0===e&&(e=window.location.href);var t="",n=e.indexOf("?");n>=0&&(t=e.slice(n+1),e=e.slice(0,n));var r=e.indexOf("#");return r&&(e=e.slice(r+1)),{path:e,query:f(t)}}function x(e,t){var n=w(Oe(e));return n.query=pe({},n.query,t),e=n.path+m(n.query),je("#/"+e)}function _(e){var t=function(){return ke.classList.toggle("close")};e=o(e),h(e,"click",t);var n=o(".sidebar");h(n,"click",function(){Se&&t(),setTimeout(function(){return S(n,!0,!0)},0)})}function L(){var e=o("section.cover");if(e){var t=e.getBoundingClientRect().height;window.pageYOffset>=t||e.classList.contains("hidden")?g(ke,"add","sticky"):g(ke,"remove","sticky")}}function S(e,t,n){e=o(e);var r,i=l(e,"a"),a="#"+k();return i.sort(function(e,t){return t.href.length-e.href.length}).forEach(function(e){var n=e.getAttribute("href"),i=t?e.parentNode:e;0!==a.indexOf(n)||r?g(i,"remove","active"):(r=e,g(i,"add","active"))}),n&&(ye.title=r?r.innerText+" - "+Pe:Pe),r}function C(){for(var e,t=o(".sidebar"),n=l(".anchor"),r=s(t,".sidebar-nav"),i=s(t,"li.active"),a=ke.scrollTop,u=0,c=n.length;ua){e||(e=p);break}e=p}if(e){var h=qe[e.getAttribute("data-id")];if(h&&h!==i&&(i&&i.classList.remove("active"),h.classList.add("active"),i=h,!Ne&&ke.classList.contains("sticky"))){var d=t.clientHeight,g=0,f=i.offsetTop+i.clientHeight+40,m=i.offsetTop>=r.scrollTop&&f<=r.scrollTop+d,v=f-g=400?a(n):(ze[e]=n.response,r(n.response))})},abort:function(e){return 4!==r.readyState&&r.abort()}})}function O(e,t){e.innerHTML=e.innerHTML.replace(/var\(\s*--theme-color.*?\)/g,t)}function M(e){return e?(/\/\//.test(e)||(e="https://github.com/"+e),e=e.replace(/^git\+/,""),''):""}function P(e){var t='';return(Se?t+"
":"
"+t)+'
'}function q(){var e=", 100%, 85%",t="linear-gradient(to left bottom, hsl("+(Math.floor(255*Math.random())+e)+") 0%,hsl("+(Math.floor(255*Math.random())+e)+") 100%)";return'
'}function N(e,t){return void 0===t&&(t=""),e&&e.length?(e.forEach(function(e){t+='
  • '+e.title+"
  • ",e.children&&(t+='
    • '+N(e.children)+"
    ")}),t):""}function F(e,t){return'

    '+t.slice(5).trim()+"

    "}function I(e){return""}function z(e,t){return t={exports:{}},e(t,t.exports),t.exports}function H(e,t){var n=[],r={};return e.forEach(function(e){var i=e.level||1,a=i-1;i>t||(r[a]?r[a].children=(r[a].children||[]).concat(e):n.push(e),r[i]=e)}),n}function R(e){if("string"!=typeof e)return"";var t=e.toLowerCase().trim().replace(/<[^>\d]+>/g,"").replace(Ue,"").replace(/\s/g,"-").replace(/-+/g,"-").replace(/^(\d)/,"_$1"),n=De[t];return n=De.hasOwnProperty(t)?n+1:0,De[t]=n,n&&(t=t+"-"+n),t}function W(e,t){return''+t+''}function B(e){return e.replace(/<(pre|template|code)[^>]*?>[\s\S]+?<\/(pre|template|code)>/g,function(e){return e.replace(/:/g,"__colon__")}).replace(/:(\w+?):/gi,window.emojify||W).replace(/__colon__/g,":")}function D(e,t){var n="";if(e)n=Qe(e),n=n.match(/]*>([\s\S]+)<\/ul>/g)[0];else{var r=Ye[Ze]||H(Je,t);n=N(r,"