Merge pull request #115 from Leopoldthecoder/master

add external-script plugin
This commit is contained in:
cinwell.li 2017-03-07 11:57:13 +08:00 committed by GitHub
commit 9e9ad57540
11 changed files with 69 additions and 4 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

@ -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

@ -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

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

@ -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

@ -0,0 +1,25 @@
this.D = this.D || {};
(function () {
'use strict';
function handleExternalScript () {
var container = Docsify.dom.getNode('#main');
var script = Docsify.dom.find(container, 'script');
if (script && script.src) {
var newScript = document.createElement('script');['src', 'async', 'defer'].forEach(function (attribute) {
newScript[attribute] = script[attribute];
});
script.parentNode.removeChild(script);
container.appendChild(newScript);
}
}
var install = function (hook) {
hook.doneEach(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 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

@ -0,0 +1,21 @@
function handleExternalScript () {
const container = Docsify.dom.getNode('#main')
const script = Docsify.dom.find(container, 'script')
if (script && script.src) {
const newScript = document.createElement('script')
;['src', 'async', 'defer'].forEach(attribute => {
newScript[attribute] = script[attribute]
})
script.parentNode.removeChild(script)
container.appendChild(newScript)
}
}
const install = function (hook) {
hook.doneEach(handleExternalScript)
}
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)