mirror of
https://github.com/zhigang1992/docsify.git
synced 2026-04-23 21:00:06 +08:00
add external-script plugin
This commit is contained in:
24
src/plugins/external-script.js
Normal file
24
src/plugins/external-script.js
Normal 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)
|
||||
Reference in New Issue
Block a user