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

feat(serve): --base-path serves docs from a subpath to match GitHub pages #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ docsify init <path> [--local false] [--theme vue]
Run a server on `localhost` with livereload.

```shell
docsify serve <path> [--open false] [--port 3000]
docsify serve <path> [--open false] [--port 3000] [--base-path .]

# docsify s <path> [--open false] [--port 3000]
# docsify s <path> [--open false] [--port 3000] [--base-path .]
```

* `--open` option:
Expand All @@ -73,6 +73,10 @@ docsify serve <path> [--open false] [--port 3000]
* Type: number
* Default: `3000`
* Description: Choose a listen port, defaults to `3000`.
* `--base-path` option:
* Type: string
* Default: `.`
* Description: Serves your docs under a subpath to match the behaviour of GitHub pages.

## License

Expand Down
9 changes: 8 additions & 1 deletion bin/docsify
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ var yargs = require('yargs')
nargs: 1,
requiresArg: true,
type: 'number'
},
'base-path': {
default: '.',
desc: chalk.grey(y18n.__('serve.base-path')),
nargs: 1,
requiresArg: true,
type: 'string'
}
}),
handler: (argv) => run.serve(argv.path, argv.open, argv.port, argv.P)
handler: (argv) => run.serve(argv.path, argv.open, argv.port, argv.P, argv['base-path'])
})
.command({
command: 'start <path>',
Expand Down
8 changes: 6 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ docsify init <path> [--local false] [--theme vue]
Run a server on `localhost` with livereload.

```shell
docsify serve <path> [--open false] [--port 3000]
docsify serve <path> [--open false] [--port 3000] [--base-path .]

# docsify s <path> [--open false] [--port 3000]
# docsify s <path> [--open false] [--port 3000] [--base-path .]
```

* `--open` option:
Expand All @@ -73,6 +73,10 @@ docsify serve <path> [--open false] [--port 3000]
* Type: number
* Default: `3000`
* Description: Choose a listen port, defaults to `3000`.
* `--base-path` option:
* Type: string
* Default: `.`
* Description: Serves your docs under a subpath to match the behaviour of GitHub pages.

## License

Expand Down
18 changes: 14 additions & 4 deletions lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const util = require('../util/index')
var exists = util.exists
var resolve = util.resolve

module.exports = function (path, openInBrowser, port, livereloadPort) {
module.exports = function (path, openInBrowser, port, livereloadPort, basePath) {
path = resolve(path || '.')
var indexFile = resolve(path, 'index.html')
const subPath = normalizePath(basePath)

if (!exists(indexFile)) {
const msg = '\nNo docs found, please run ' + chalk.green('docsify init') + ' first.\n'
Expand All @@ -25,19 +26,28 @@ module.exports = function (path, openInBrowser, port, livereloadPort) {
server.use(livereload({
port: livereloadPort
}))
server.use(serveStatic(path))
server.use(subPath, serveStatic(path))
server.listen(port)
lrserver.createServer({
extraExts: ['md'],
exclusions: ['node_modules/'],
port: livereloadPort
}).watch(path)

const url = `http://localhost:${port}${subPath}`
if (openInBrowser) {
opn(`http://localhost:${port}`)
opn(url)
}

const msg = '\nServing ' + chalk.green(`${path}`) + ' now.\n' +
'Listening at ' + chalk.green(`http://localhost:${port}`) + '\n'
'Listening at ' + chalk.green(url) + '\n'
console.log(msg)
}

function normalizePath (basePath) {
if (basePath === '.' || basePath === '') {
return ''
}

return basePath[0] === '/' ? basePath : '/' + basePath
}
1 change: 1 addition & 0 deletions tools/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"serve.open": "Dokumentation im Standardbrowser öffnen. Um explizit --open auf false zu setzen, kannst du --no-open verwenden.",
"serve.port": "Listen port.",
"livereload.port": "livereload Listen port.",
"serve.base-path": "Erzeugt Dokumentation in einem Unterpfad, analog zur GitHub Pages Pfadstruktur.",
"usage": "Anwendung",
"version": "Zeige Versionsnummer an"
}
1 change: 1 addition & 0 deletions tools/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.",
"serve.port": "Listen port.",
"livereload.port": "livereload Listen port.",
"serve.base-path": "Serves your docs under a subpath to match the behaviour of GitHub pages.",
"usage": "Usage",
"version": "Show version number"
}
1 change: 1 addition & 0 deletions tools/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"serve.open": "自动打开浏览器",
"serve.port": "设置端口",
"livereload.port": "设置livereload端口",
"serve.base-path": "Serves your docs under a subpath to match the behaviour of GitHub pages.",
"usage": "例子",
"version": "当前版本号"
}