Skip to content

Commit 46ac4c3

Browse files
committed
feat(plugin): add codesponsor plugin
1 parent 44dc68c commit 46ac4c3

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

docs/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
mergeNavbar: true,
3535
maxLevel: 4,
3636
subMaxLevel: 2,
37+
codesponsor: '7c9Ms7xRs-j_y_8abU03DA',
3738
ga: 'UA-106147152-1',
3839
name: 'docsify',
3940
search: {
@@ -68,6 +69,7 @@
6869
</script>
6970
<script src="//cdn.jsdelivr.net/npm/docsify@latest/lib/docsify.min.js"></script>
7071
<script src="//cdn.jsdelivr.net/npm/docsify@latest/lib/plugins/search.min.js"></script>
72+
<script src="//cdn.jsdelivr.net/npm/docsify@latest/lib/plugins/codesponsor.min.js"></script>
7173
<script src="//cdn.jsdelivr.net/npm/docsify@latest/lib/plugins/ga.min.js"></script>
7274
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-bash.min.js"></script>
7375
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-markdown.min.js"></script>

src/core/util/dom.js

+5
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ export function off (el, type, handler) {
8181
export function toggleClass (el, type, val) {
8282
el && el.classList[val ? type : 'toggle'](val || type)
8383
}
84+
85+
export function style (content) {
86+
appendTo(head, create('style', content))
87+
}
88+

src/plugins/codesponsor.js

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

src/plugins/search/component.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ function style () {
6464
.search p.empty {
6565
text-align: center;
6666
}`
67-
const style = Docsify.dom.create('style', code)
68-
Docsify.dom.appendTo(Docsify.dom.head, style)
67+
68+
Docsify.dom.style(code)
6969
}
7070

7171
function tpl (opts, defaultValue = '') {

src/themes/basic/_layout.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ body.sticky {
325325

326326
/* main content */
327327
.content {
328-
padding-top: 20px;
328+
padding-top: 60px;
329329
position: absolute 0 0 0 $sidebar-width;
330330
transition: left 250ms ease;
331331
}
@@ -471,6 +471,7 @@ body.close {
471471
left: 0;
472472
max-width: 100vw;
473473
position: static;
474+
padding-top: 20px;
474475
transition: transform 250ms ease;
475476
}
476477

0 commit comments

Comments
 (0)