Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support docsify init --plugins #99

Merged
merged 32 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ee76d17
feat: adding plugins support
tool3 Mar 12, 2020
8b9fccb
fix: fixing url
tool3 Mar 12, 2020
21a097f
fix: removing temp docs
tool3 Mar 12, 2020
a078e08
docs: adding documentation for init --plugins
tool3 Mar 12, 2020
b8b89c7
fix: removing unused files
tool3 Mar 13, 2020
64b32e0
fix: removing .nojekyll top-level
tool3 Mar 13, 2020
f522d07
chore: adding tests
tool3 Mar 13, 2020
e532c1e
fix: fixing docs links
tool3 Mar 13, 2020
6558cee
refactor: cleaner matchSnapshot
tool3 Mar 13, 2020
5431de3
fix: removing redundant spread
tool3 Mar 20, 2020
1083bec
fix(init): adding jsdelivr and major version to plugins init
tool3 Jun 18, 2020
54dd557
fix: realigning with remote
tool3 Jun 18, 2020
de9bcce
fix(e2e): recreating index snap with latest docsify version
tool3 Jun 18, 2020
3c547f9
Merge branch 'master' into master
tool3 Jun 21, 2020
067bb2a
Update docs/README.md
tool3 Apr 28, 2021
c1f92b4
Update README.md
tool3 Apr 28, 2021
5ec56bb
Update lib/template/index.html
tool3 Apr 28, 2021
0059e7f
Update lib/commands/init.js
tool3 Apr 28, 2021
272e310
chore: Optimize code to ensure equal indentation
sy-records Apr 29, 2021
c83fa11
Merge branch 'master' into tool3/master
sy-records Apr 29, 2021
f3e717c
chore: update snap
sy-records Apr 29, 2021
e48d7a8
chore: update snap
sy-records Apr 29, 2021
f96f6d5
chore: update zh.json
sy-records Apr 29, 2021
2debff8
fix: the exception caused by the plugin name being a number
sy-records Apr 29, 2021
6f26f05
Use MultiSelect
sy-records May 19, 2021
1b7f8f2
Update README
sy-records May 19, 2021
f2943a1
Update lib/commands/init.js
sy-records May 27, 2021
e1118d5
Update msg
sy-records May 27, 2021
63e9d95
Fix missing msg
sy-records May 27, 2021
790e96c
refactor: simplify code with async/await
jamesgeorge007 May 27, 2021
149bd81
chore: handle user interruptions
jamesgeorge007 May 28, 2021
828b672
Update README
sy-records Jun 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ docsify init <path> [--local false] [--theme vue]
- Type: string
- Default: `vue`
- Description: Choose a theme, defaults to `vue`, other choices are `buble`, `dark` and `pure`.
- `--plugins` option:
- Shorthand: `-p`
- Type: boolean
- Default: `false`
- Description: Provide a list of plugins to insert as `<script>` tags to `index.html`.

### `serve` command

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

## Contributing

Please see the [Contributing Guidelines](./CONTRIBUTING.md)

## Contribution
Expand Down
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ docsify init <path> [--local false] [--theme vue]
* Type: string
* Default: `vue`
* Description: Choose a theme, defaults to `vue`, other choices are `buble`, `dark` and `pure`.
* `--plugins` option:
* Shorthand: `-p`
* Type: array
* Default: `[]`
* Description: Provide a list of plugins to insert as `<script>` tags to `index.html`. If a plugin name is provided the script will have a url of `https://cdn.jsdelivr.net/npm/docsify@4/lib/plugins/<plugin_name>`, otherwise provide the url to the plugin. Example: `docsify init docs/ -p search gitalk https://cdn.jsdelivr.net/npm/docsify-copy-code@2`

### `serve` command

Expand Down
10 changes: 9 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@ require('yargs')
nargs: 1,
requiresArg: true,
type: 'string'
},
plugins: {
alias: 'p',
default: false,
desc: chalk.gray(y18n.__('init.plugins')),
nargs: 0,
requiresArg: false,
type: 'boolean'
}
}),
handler: argv => run.init(argv.path, argv.local, argv.theme)
handler: argv => run.init(argv.path, argv.local, argv.theme, argv.plugins)
})
.command({
command: 'serve [path]',
Expand Down
77 changes: 62 additions & 15 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
const fs = require('fs')
const cp = require('cp-file').sync
const chalk = require('chalk')
const {version} = require('../../package.json')
const logger = require('../util/logger')
const {prompt} = require('enquirer')
const {prompt, MultiSelect} = require('enquirer')
const {cwd, exists, pkg, pwd, read, resolve} = require('../util')
const colors = require('ansi-colors')

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

// eslint-disable-next-line
module.exports = function (path = '', local, theme) {
module.exports = async function (path = '', local, theme, plugins) {
const msg =
'\n' +
chalk.green('Initialization succeeded!') +
Expand All @@ -25,32 +27,25 @@ module.exports = function (path = '', local, theme) {
if (exists(cwdPath)) {
logger.error(`${path || '.'} already exists.`)

prompt({
const {rewrite} = await prompt({
type: 'confirm',
name: 'rewrite',
symbols: {
separator: ''
},
message: 'Are you sure you want to rewrite it?'
})
.then(answers => {
if (answers.rewrite === false) {
return process.exit(0)
}

createFile(cwdPath, local, theme)
console.log(msg)
})
.catch(console.error)

return false
if (!rewrite) {
return
}
}

createFile(cwdPath, local, theme)
await createFile(cwdPath, local, theme, plugins)
console.log(msg)
}

function createFile(path, local, theme) {
async function createFile(path, local, theme, plugins) {
const target = file => resolve(path, file)
const readme = exists(cwd('README.md')) || pwd('template/README.md')
let main = pwd('template/index.html')
Expand Down Expand Up @@ -95,4 +90,56 @@ function createFile(path, local, theme) {
.replace(/^git\+/g, '')
replace(target(filename), 'repo: \'\'', `repo: '${repo}'`)
}

// Return early if not opted for plugins
if (!plugins) {
return replace(target(filename), '\n _plugins_', '')
}

const officialPlugins = [
'front-matter',
'search',
'disqus',
'emoji',
'external-script',
'ga',
'gitalk',
'matomo',
'zoom-image'
]

const choices = officialPlugins.map(name => ({name, value: name}))
const prompt = new MultiSelect({
name: 'plugins',
message: 'Select plugins to be used',
hint: '(Use <space> to select, <return> to submit)',
default: '',
choices,
indicator(state, choice) {
if (choice.enabled) {
return colors.cyan(state.symbols.radio.on)
}

return colors.gray(state.symbols.radio.off)
}
})

prompt.on('cancel', () => replace(target(filename), '\n _plugins_', ''))

let answers = []
try {
answers = await prompt.run()
} catch (err) {
logger.error(err)
process.exit(1)
}

replace(target(filename), ' _plugins_', '_plugin'.repeat(answers.length + 1))

answers.forEach(plugin => {
const url = `//cdn.jsdelivr.net/npm/docsify@${version[0]}/lib/plugins/${plugin}.min.js`
replace(target(filename), '_plugin', ` <script src="${url}"></script>\n`)
})

replace(target(filename), '\n_plugin', '')
}
1 change: 1 addition & 0 deletions lib/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
</script>
<!-- Docsify v4 -->
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
_plugins_
</body>
</html>
1 change: 1 addition & 0 deletions tools/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"start": "Server for SSR",
"init.local": "Copy docsify files to local. To explicitly set --local to false you may use --no-local.",
"init.theme": "Theme file to be used.",
"init.plugins": "A list of plugins to be used.",
"serve": "Run local server to preview site.",
"serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.",
"serve.port": "Listen port.",
Expand Down
7 changes: 4 additions & 3 deletions tools/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"init": "创建 docs",
"init.local": "拷贝 docsify 到本地",
"init.theme": "选择主题",
"init.plugins": "选择插件",
"serve": "本地预览",
"serve.open": "自动打开浏览器",
"serve.port": "设置端口",
"serve.indexname": "Custom filename instead of index.html to serve by default",
"generate": "Docsify的生成器",
"serve.indexname": "自定义入口文件名,代替默认的 index.html",
"generate": "docsify 的生成器",
"generate.sidebar": "生成侧边栏文件",
"livereload.port": "设置livereload端口",
"livereload.port": "设置 livereload 端口",
"usage": "例子",
"version": "当前版本号"
}