diff --git a/build/build.js b/build/build.js index b01ee22..6834167 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 3142694..4621d34 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 5fa1530..5b35d9d 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 0000000..3c38e8f --- /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 0000000..a7fd791 --- /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 0000000..8391079 --- /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)