Skip to content

Commit 8bb295c

Browse files
tool3sy-recordsjamesgeorge007
authored
feat: support docsify init --plugins (#99)
* feat: adding plugins support * fix: fixing url * fix: removing temp docs * docs: adding documentation for init --plugins * fix: removing unused files * fix: removing .nojekyll top-level * chore: adding tests * fix: fixing docs links * refactor: cleaner matchSnapshot * fix: removing redundant spread * fix(init): adding jsdelivr and major version to plugins init * fix(e2e): recreating index snap with latest docsify version * Update docs/README.md Co-authored-by: 沈唁 <52o@qq52o.cn> * Update README.md Co-authored-by: 沈唁 <52o@qq52o.cn> * Update lib/template/index.html Co-authored-by: 沈唁 <52o@qq52o.cn> * Update lib/commands/init.js Co-authored-by: 沈唁 <52o@qq52o.cn> * chore: Optimize code to ensure equal indentation * chore: update snap * chore: update snap * chore: update zh.json * fix: the exception caused by the plugin name being a number #99 (comment) * Use MultiSelect * Update README * Update lib/commands/init.js Co-authored-by: James George <jamesgeorge998001@gmail.com> * Update msg * Fix missing msg * refactor: simplify code with async/await * chore: handle user interruptions * Update README Co-authored-by: 沈唁 <52o@qq52o.cn> Co-authored-by: James George <jamesgeorge998001@gmail.com>
1 parent 2728725 commit 8bb295c

File tree

7 files changed

+108
-31
lines changed

7 files changed

+108
-31
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ npm i docsify-cli -g
5050
Use `init` to generate your docs.
5151

5252
```shell
53-
docsify init <path> [--local false] [--theme vue]
53+
docsify init <path> [--local false] [--theme vue] [--plugins false]
5454

55-
# docsify i <path> [-l false] [-t vue]
55+
# docsify i <path> [-l false] [-t vue] [--plugins false]
5656
```
5757

5858
`<path>` defaults to the current directory. Use relative paths like `./docs` (or `docs`).
@@ -67,6 +67,11 @@ docsify init <path> [--local false] [--theme vue]
6767
- Type: string
6868
- Default: `vue`
6969
- Description: Choose a theme, defaults to `vue`, other choices are `buble`, `dark` and `pure`.
70+
- `--plugins` option:
71+
- Shorthand: `-p`
72+
- Type: boolean
73+
- Default: `false`
74+
- Description: Provide a list of plugins to insert as `<script>` tags to `index.html`.
7075

7176
### `serve` command
7277

@@ -106,7 +111,6 @@ docsify generate <path> [--sidebar _sidebar.md]
106111
- Description: Generate sidebar file, defaults to `_sidebar.md`.
107112

108113
## Contributing
109-
110114
Please see the [Contributing Guidelines](./CONTRIBUTING.md)
111115

112116
## Contribution

docs/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ npm i docsify-cli -g
3535
Use `init` to generate your docs.
3636

3737
```shell
38-
docsify init <path> [--local false] [--theme vue]
38+
docsify init <path> [--local false] [--theme vue] [--plugins false]
3939

40-
# docsify i <path> [--local false] [--theme vue]
40+
# docsify i <path> [--local false] [--theme vue] [--plugins false]
4141
```
4242

4343
`<path>` defaults to the current directory. Use relative paths like `./docs` (or `docs`).
@@ -52,6 +52,11 @@ docsify init <path> [--local false] [--theme vue]
5252
* Type: string
5353
* Default: `vue`
5454
* Description: Choose a theme, defaults to `vue`, other choices are `buble`, `dark` and `pure`.
55+
* `--plugins` option:
56+
* Shorthand: `-p`
57+
* Type: array
58+
* Default: `[]`
59+
* Description: Provide a list of plugins to insert as `<script>` tags to `index.html`.
5560

5661
### `serve` command
5762

lib/cli.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ require('yargs')
4141
nargs: 1,
4242
requiresArg: true,
4343
type: 'string'
44+
},
45+
plugins: {
46+
alias: 'p',
47+
default: false,
48+
desc: chalk.gray(y18n.__('init.plugins')),
49+
nargs: 0,
50+
requiresArg: false,
51+
type: 'boolean'
4452
}
4553
}),
46-
handler: argv => run.init(argv.path, argv.local, argv.theme)
54+
handler: argv => run.init(argv.path, argv.local, argv.theme, argv.plugins)
4755
})
4856
.command({
4957
command: 'serve [path]',

lib/commands/init.js

+79-22
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
const fs = require('fs')
44
const cp = require('cp-file').sync
55
const chalk = require('chalk')
6+
const {version} = require('../../package.json')
67
const logger = require('../util/logger')
7-
const {prompt} = require('enquirer')
8+
const {prompt, MultiSelect} = require('enquirer')
89
const {cwd, exists, pkg, pwd, read, resolve} = require('../util')
10+
const colors = require('ansi-colors')
911

1012
const replace = function (file, tpl, replace) {
1113
fs.writeFileSync(file, read(file).replace(tpl, replace), 'utf-8')
1214
}
1315

1416
// eslint-disable-next-line
15-
module.exports = function (path = '', local, theme) {
17+
module.exports = async function (path = '', local, theme, plugins) {
1618
const msg =
1719
'\n' +
1820
chalk.green('Initialization succeeded!') +
@@ -25,32 +27,31 @@ module.exports = function (path = '', local, theme) {
2527
if (exists(cwdPath)) {
2628
logger.error(`${path || '.'} already exists.`)
2729

28-
prompt({
29-
type: 'confirm',
30-
name: 'rewrite',
31-
symbols: {
32-
separator: ''
33-
},
34-
message: 'Are you sure you want to rewrite it?'
35-
})
36-
.then(answers => {
37-
if (answers.rewrite === false) {
38-
return process.exit(0)
39-
}
40-
41-
createFile(cwdPath, local, theme)
42-
console.log(msg)
30+
let answer = {}
31+
try {
32+
answer = await prompt({
33+
type: 'confirm',
34+
name: 'rewrite',
35+
symbols: {
36+
separator: ''
37+
},
38+
message: 'Are you sure you want to rewrite it?'
4339
})
44-
.catch(console.error)
45-
46-
return false
40+
} catch (err) {
41+
err && logger.error(err)
42+
process.exit(1)
43+
}
44+
45+
if (!answer.rewrite) {
46+
return
47+
}
4748
}
4849

49-
createFile(cwdPath, local, theme)
50+
await createFile(cwdPath, local, theme, plugins)
5051
console.log(msg)
5152
}
5253

53-
function createFile(path, local, theme) {
54+
async function createFile(path, local, theme, plugins) {
5455
const target = file => resolve(path, file)
5556
const readme = exists(cwd('README.md')) || pwd('template/README.md')
5657
let main = pwd('template/index.html')
@@ -95,4 +96,60 @@ function createFile(path, local, theme) {
9596
.replace(/^git\+/g, '')
9697
replace(target(filename), 'repo: \'\'', `repo: '${repo}'`)
9798
}
99+
100+
// Return early if not opted for plugins
101+
if (!plugins) {
102+
return replace(target(filename), '\n _plugins_', '')
103+
}
104+
105+
const officialPlugins = [
106+
'front-matter',
107+
'search',
108+
'disqus',
109+
'emoji',
110+
'external-script',
111+
'ga',
112+
'gitalk',
113+
'matomo',
114+
'zoom-image'
115+
]
116+
117+
const choices = officialPlugins.map(name => ({name, value: name}))
118+
const prompt = new MultiSelect({
119+
name: 'plugins',
120+
message: 'Select plugins to be used',
121+
hint: '(Use <space> to select, <return> to submit)',
122+
default: '',
123+
choices,
124+
indicator(state, choice) {
125+
if (choice.enabled) {
126+
return colors.cyan(state.symbols.radio.on)
127+
}
128+
129+
return colors.gray(state.symbols.radio.off)
130+
}
131+
})
132+
133+
prompt.on('cancel', () => replace(target(filename), '\n _plugins_', ''))
134+
135+
let answers = []
136+
try {
137+
answers = await prompt.run()
138+
} catch (err) {
139+
if (err) {
140+
logger.error(err)
141+
process.exitCode = 1
142+
}
143+
144+
return
145+
}
146+
147+
replace(target(filename), ' _plugins_', '_plugin'.repeat(answers.length + 1))
148+
149+
answers.forEach(plugin => {
150+
const url = `//cdn.jsdelivr.net/npm/docsify@${version[0]}/lib/plugins/${plugin}.min.js`
151+
replace(target(filename), '_plugin', ` <script src="${url}"></script>\n`)
152+
})
153+
154+
replace(target(filename), '\n_plugin', '')
98155
}

lib/template/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
</script>
1919
<!-- Docsify v4 -->
2020
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
21+
_plugins_
2122
</body>
2223
</html>

tools/locales/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"start": "Server for SSR",
77
"init.local": "Copy docsify files to local. To explicitly set --local to false you may use --no-local.",
88
"init.theme": "Theme file to be used.",
9+
"init.plugins": "A list of plugins to be used.",
910
"serve": "Run local server to preview site.",
1011
"serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.",
1112
"serve.port": "Listen port.",

tools/locales/zh.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
"init": "创建 docs",
77
"init.local": "拷贝 docsify 到本地",
88
"init.theme": "选择主题",
9+
"init.plugins": "选择插件",
910
"serve": "本地预览",
1011
"serve.open": "自动打开浏览器",
1112
"serve.port": "设置端口",
12-
"serve.indexname": "Custom filename instead of index.html to serve by default",
13-
"generate": "Docsify的生成器",
13+
"serve.indexname": "自定义入口文件名,代替默认的 index.html",
14+
"generate": "docsify 的生成器",
1415
"generate.sidebar": "生成侧边栏文件",
15-
"livereload.port": "设置livereload端口",
16+
"livereload.port": "设置 livereload 端口",
1617
"usage": "例子",
1718
"version": "当前版本号"
1819
}

0 commit comments

Comments
 (0)