Skip to content

Commit 3e7d6ab

Browse files
committed
docs: add executeScript demo
1 parent 4b64386 commit 3e7d6ab

File tree

6 files changed

+125
-4
lines changed

6 files changed

+125
-4
lines changed

docs/configuration.md

+22
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,25 @@ window.$docsify = {
267267
autoHeader: true
268268
}
269269
```
270+
271+
## execute-script
272+
273+
- type: `Boolean`
274+
275+
Execute the script on the page. Only parse the first script tag([demo](themes)). If Vue is present, it is turned on by default.
276+
277+
```js
278+
window.$docsify = {
279+
executeScript: true
280+
}
281+
```
282+
283+
```markdown
284+
## This is test
285+
286+
<script>
287+
console.log(2333)
288+
</script>
289+
290+
```
291+

docs/themes.md

+34
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,37 @@ There are currently three themes available. Copy [Vue](//vuejs.org) and [buble](
1212
!> This compressed files in `/lib/themes/`.
1313

1414
If you have any ideas or would like to develop new theme, welcome submit [PR](https://github.com/QingWei-Li/docsify/pulls).
15+
16+
#### Click to preview
17+
18+
19+
<div class="demo-theme-preview">
20+
<a data-theme="vue">vue.css</a>
21+
<a data-theme="buble">buble.css</a>
22+
<a data-theme="dark">dark.css</a>
23+
</div>
24+
25+
26+
<style>
27+
.demo-theme-preview a {
28+
padding-right: 10px;
29+
}
30+
31+
.demo-theme-preview a:hover {
32+
text-decoration: underline;
33+
cursor: pointer;
34+
}
35+
</style>
36+
37+
<script>
38+
var preview = Docsify.dom.find('.demo-theme-preview');
39+
var themes = Docsify.dom.findAll('[rel="stylesheet"]');
40+
41+
preview.onclick = function (e) {
42+
var title = e.target.getAttribute('data-theme')
43+
44+
themes.forEach(function (theme) {
45+
theme.disabled = theme.title !== title
46+
});
47+
};
48+
</script>

docs/zh-cn/configuration.md

+22
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,25 @@ window.$docsify = {
265265
autoHeader: true
266266
}
267267
```
268+
269+
## execute-script
270+
271+
- 类型:`Boolean`
272+
273+
执行文档里的 script 标签里的脚本,只执行第一个 script ([demo](zh-cn/themes))。 如果 Vue 存在,则自动开启。
274+
275+
```js
276+
window.$docsify = {
277+
executeScript: true
278+
}
279+
```
280+
281+
```markdown
282+
## This is test
283+
284+
<script>
285+
console.log(2333)
286+
</script>
287+
288+
```
289+

docs/zh-cn/themes.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@
88
<link rel="stylesheet" href="//unpkg.com/docsify/themes/dark.css">
99
```
1010

11+
<a id="demo-theme-vue">vue.css</a>
12+
<a id="demo-theme-buble">buble.css</a>
13+
<a id="demo-theme-dark">dark.css</a>
14+
1115
!> CSS 的压缩文件位于 `/lib/themes/`
1216

13-
如果你有其他想法或者想开发别的主题,欢迎提 [PR](https://github.com/QingWei-Li/docsify/pulls)
17+
如果你有其他想法或者想开发别的主题,欢迎提 [PR](https://github.com/QingWei-Li/docsify/pulls)
18+
19+
#### 点击切换主题
20+
21+
22+
<div class="demo-theme-preview">
23+
<a data-theme="vue">vue.css</a>
24+
<a data-theme="buble">buble.css</a>
25+
<a data-theme="dark">dark.css</a>
26+
</div>
27+
28+
29+
<style>
30+
.demo-theme-preview a {
31+
padding-right: 10px;
32+
}
33+
34+
.demo-theme-preview a:hover {
35+
text-decoration: underline;
36+
cursor: pointer;
37+
}
38+
</style>
39+
40+
<script>
41+
var preview = Docsify.dom.find('.demo-theme-preview');
42+
var themes = Docsify.dom.findAll('[rel="stylesheet"]');
43+
44+
preview.onclick = function (e) {
45+
var title = e.target.getAttribute('data-theme')
46+
47+
themes.forEach(function (theme) {
48+
theme.disabled = theme.title !== title
49+
});
50+
};
51+
</script>

src/core/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const config = merge({
1515
themeColor: '',
1616
nameLink: window.location.pathname,
1717
autoHeader: false,
18+
executeScript: false,
1819
ga: ''
1920
}, window.$docsify)
2021

src/core/render/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ function executeScript () {
1414
const code = script.innerText.trim()
1515
if (!code) return false
1616

17-
window.__EXECUTE_RESULT__ = new Function('return ' + code)()
17+
setTimeout(_ => {
18+
window.__EXECUTE_RESULT__ = new Function(code)()
19+
}, 0)
1820
}
1921

2022
function renderMain (html) {
@@ -31,11 +33,13 @@ function renderMain (html) {
3133
if (!this.config.executeScript &&
3234
typeof window.Vue !== 'undefined' &&
3335
!executeScript()) {
34-
window.__EXECUTE_RESULT__ = new window.Vue().$mount('#main')
36+
setTimeout(_ => {
37+
window.__EXECUTE_RESULT__ = new window.Vue().$mount('#main')
38+
}, 0)
3539
}
3640

3741
if (this.config.auto2top) {
38-
setTimeout(() => scroll2Top(this.config.auto2top), 0)
42+
scroll2Top(this.config.auto2top)
3943
}
4044
}
4145

0 commit comments

Comments
 (0)