Skip to content

Commit

Permalink
Add setting for path in preview (#147)
Browse files Browse the repository at this point in the history
Add setting for path in preview
  • Loading branch information
2m authored May 21, 2019
2 parents 86a69df + 0f73e4b commit f94ee59
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/paradox/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ To disable browser auto-open, use the key `previewLaunchBrowser`:
previewLaunchBrowser := false
```

In case the page to start preview from isn't the site root, set `previewPath` to the desired path:

```sbt
previewPath := "docs/index.html"
```

## Packaging and Publishing

To create a zip package of the site run `package-site`.
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/com/typesafe/sbt/site/SitePreviewPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object SitePreviewPlugin extends AutoPlugin {
val previewAuto = TaskKey[Unit]("previewAuto", "Launches an automatic jetty server that serves your generated site from the target directory")
val previewFixedPort = SettingKey[Option[Int]]("previewFixedPort") in previewSite
val previewLaunchBrowser = SettingKey[Boolean]("previewLaunchBrowser") in previewSite
val previewPath = SettingKey[String]("previewPath", "path to open on `previewSite` and `previewAuto`") in previewSite
}
import SitePlugin.autoImport._
import autoImport._
Expand All @@ -22,6 +23,7 @@ object SitePreviewPlugin extends AutoPlugin {
val file = makeSite.value
val portOption = previewFixedPort.value
val browser = previewLaunchBrowser.value
val path = previewPath.value

val port = portOption getOrElse Port.any
val server = createServer(file, port) start()
Expand All @@ -34,22 +36,24 @@ object SitePreviewPlugin extends AutoPlugin {
waitForKey()
}
if(browser)
Browser open ("http://localhost:%d/" format port)
Browser open ("http://localhost:%d/%s".format(port, path))
waitForKey()
server stop()
server destroy()
},
previewAuto := {
val port = previewFixedPort.value getOrElse Port.any
val browser = previewLaunchBrowser.value
val path = previewPath.value

Preview(port, (target in previewAuto).value, makeSite, Compat.genSources, state.value) run { server =>
if (browser)
Browser open(server.portBindings.head.url)
Browser open(server.portBindings.head.url + "/" + path)
}
},
previewFixedPort := Some(4000),
previewLaunchBrowser := true,
previewPath := "",
target in previewAuto := siteDirectory.value
)

Expand Down

0 comments on commit f94ee59

Please sign in to comment.