forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodesponsor.js
86 lines (69 loc) · 1.52 KB
/
codesponsor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const DEFAULT_OPTIONS = {
theme: 'light',
image: 'show'
}
function tpl (id, options) {
const qs = []
for (const key in options) {
qs.push(`${key}=${options[key]}`)
}
const div = Docsify.dom.create('div')
Docsify.dom.toggleClass(div, 'codesponsor')
div.innerHTML = `<iframe
scrolling=0
frameborder=0
width=250
height=250
id="code-sponsor-embed-iframe"
src="https://app.codesponsor.io/widgets/${id}?${qs.join('&')}">
</iframe>`
return div
}
function appIframe (id, opts) {
const html = tpl(id, opts)
Docsify.dom.before(Docsify.dom.find('section.content'), html)
}
function appendStyle () {
Docsify.dom.style(`
.codesponsor {
position: relative;
float: right;
right: 10px;
top: 10px;
}
@media screen and (min-width: 1600px) {
body.sticky .codesponsor {
position: fixed;
}
.codesponsor {
position: absolute;
bottom: 10px;
top: auto;
float: none;
}
}
`)
}
const install = function (hook, vm) {
let config = vm.config.codesponsor
let id
if (typeof config === 'string') {
id = config
config = {}
} else {
id = config.id
}
const opts = Docsify.util.merge(DEFAULT_OPTIONS, config)
if (!id) {
throw Error('codesponsor plugin need id')
}
if (Docsify.util.isMobile) {
return
}
// Append style
hook.ready(() => {
appendStyle()
appIframe(id, opts)
})
}
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)