Skip to content

Commit 1e6bccd

Browse files
committed
Merge branch 'master' of https://github.com/azerupi/mdBook
2 parents 6d77b7f + f9ea613 commit 1e6bccd

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

book-example/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [init](cli/init.md)
66
- [build](cli/build.md)
77
- [watch](cli/watch.md)
8+
- [serve](cli/serve.md)
89
- [test](cli/test.md)
910
- [Format](format/format.md)
1011
- [SUMMARY.md](format/summary.md)

book-example/src/cli/serve.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# The serve command
2+
3+
The `serve` command is useful when you want to preview your book. It also does hot reloading of the webpage whenever a file changes.
4+
It achieves this by serving the books content over `localhost:3000` (unless otherwise configured, see below) and runs a websocket server on `localhost:3001` which triggers the reloads.
5+
This preferred by many for writing books with mdbook because it allows for you to see the result of your work instantly after every file change.
6+
7+
#### Specify a directory
8+
9+
Like `watch`, `serve` can take a directory as argument to use instead of the
10+
current working directory.
11+
12+
```bash
13+
mdbook serve path/to/book
14+
```
15+
16+
17+
#### Server options
18+
19+
`serve` has four options: the http port, the websocket port, the interface to serve on, and the public address of the server so that the browser may reach the websocket server.
20+
21+
For example: suppose you had an nginx server for SSL termination which has a public address of 192.168.1.100 on port 80 and proxied that to 127.0.0.1 on port 8000. To run use the nginx proxy do:
22+
23+
```bash
24+
mdbook server path/to/book -p 8000 -i 127.0.0.1 -a 192.168.1.100
25+
```
26+
27+
If you were to want live reloading for this you would need to proxy the websocket calls through nginx as well from `192.168.1.100:<WS_PORT>` to `127.0.0.1:<WS_PORT>`. The `-w` flag allows for the websocket port to be configured.
28+
29+
-----
30+
31+
***note:*** *the `serve` command has not gotten a lot of testing yet, there could be some rough edges. If you discover a problem, please report it [on Github](https://github.com/azerupi/mdBook/issues)*
32+
33+
***note***:

src/bin/mdbook.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ fn main() {
6262
.about("Serve the book at http://localhost:3000. Rebuild and reload on change.")
6363
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when ommitted)'")
6464
.arg_from_usage("-p, --port=[port] 'Use another port{n}(Defaults to 3000)'")
65-
.arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'"))
65+
.arg_from_usage("-w, --websocket-port=[ws-port] 'Use another port for the websocket connection (livereload){n}(Defaults to 3001)'")
66+
.arg_from_usage("-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'")
67+
.arg_from_usage("-a, --address=[address] 'Address that the browser can reach the websocket server from{n}(Defaults to the interface addres)'"))
6668
.subcommand(SubCommand::with_name("test")
6769
.about("Test that code samples compile"))
6870
.get_matches();
@@ -189,13 +191,15 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
189191
let mut book = MDBook::new(&book_dir).read_config();
190192
let port = args.value_of("port").unwrap_or("3000");
191193
let ws_port = args.value_of("ws-port").unwrap_or("3001");
194+
let interface = args.value_of("interface").unwrap_or("localhost");
195+
let public_address = args.value_of("address").unwrap_or(interface);
192196

193-
let address = format!("localhost:{}", port);
194-
let ws_address = format!("localhost:{}", ws_port);
197+
let address = format!("{}:{}", interface, port);
198+
let ws_address = format!("{}:{}", interface, ws_port);
195199

196200
book.set_livereload(format!(r#"
197201
<script type="text/javascript">
198-
var socket = new WebSocket("ws://localhost:{}");
202+
var socket = new WebSocket("ws://{}:{}");
199203
socket.onmessage = function (event) {{
200204
if (event.data === "{}") {{
201205
socket.close();
@@ -207,7 +211,7 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
207211
socket.close();
208212
}}
209213
</script>
210-
"#, ws_port, RELOAD_COMMAND).to_owned());
214+
"#, public_address, ws_port, RELOAD_COMMAND).to_owned());
211215

212216
try!(book.build());
213217

0 commit comments

Comments
 (0)