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

@ -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' }
]

View file

@ -75,3 +75,10 @@ The default is to support parsing emoji. For example `:100:` will be parsed to :
<script src="//unpkg.com/docsify/lib/plugins/emoji.js"></script>
```
## 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
<script src="//unpkg.com/docsify/lib/plugins/external-script.js"></script>
```

View file

@ -71,3 +71,11 @@
```html
<script src="//unpkg.com/docsify/lib/plugins/emoji.js"></script>
```
## 外链脚本 - External Script
如果文档里的 script 是内联脚本,可以直接执行;而如果是外链脚本(即 js 文件内容由 `src` 属性引入),则需要使用此插件。
```html
<script src="//unpkg.com/docsify/lib/plugins/external-script.js"></script>
```

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)}();

View file

@ -0,0 +1,24 @@
const scriptReg = /<script[^>]*src=["|'](.*)["|']>[^\w]*<\/script>/
const asyncReg = /<script[^>]*\s+async/
const deferReg = /<script[^>]*\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)