Skip to content

Commit 071ea2a

Browse files
marcospereiraoctonato
authored andcommitted
Better HTML title for documentation pages (#268)
So that we can have better SEO for these pages.
1 parent 8845971 commit 071ea2a

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

app/controllers/documentation/DocumentationController.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controllers.documentation
22

33
import actors.DocumentationActor
4-
import actors.DocumentationActor.{ NotFound => DocsNotFound, NotModified => DocsNotModified, _ }
4+
import actors.DocumentationActor.{NotFound => DocsNotFound, NotModified => DocsNotModified, _}
55
import akka.actor.ActorRef
66
import akka.pattern.ask
77
import akka.util.Timeout
@@ -18,10 +18,11 @@ import play.api.http.HttpEntity
1818
import play.api.i18n.MessagesApi
1919
import play.api.i18n.Lang
2020
import play.api.mvc._
21+
import utils.HtmlHelpers
22+
2123
import scala.concurrent.ExecutionContext
2224
import scala.concurrent.Future
2325
import scala.concurrent.duration._
24-
2526
import scala.reflect.ClassTag
2627

2728
@Singleton
@@ -181,9 +182,10 @@ class DocumentationController @Inject()(
181182
val linkFuture = canonicalLinkHeader(page)
182183
val resultFuture = actorRequest(actor, page, RenderPage(lang, version, etag(req), page)) {
183184
case RenderedPage(html, sidebarHtml, breadcrumbsHtml, source, context, cacheId) =>
185+
val pageTitle = HtmlHelpers.friendlyTitle(page)
184186
val result = Ok(
185187
views.html.documentation
186-
.v2(messages, context, page, Some(html), sidebarHtml, source, breadcrumbs = breadcrumbsHtml),
188+
.v2(messages, context, pageTitle, Some(html), sidebarHtml, source, breadcrumbs = breadcrumbsHtml),
187189
)
188190
cacheable(withLangHeaders(result, page, context), cacheId)
189191
}.flatMap { result =>

app/utils/HtmlHelpers.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
object HtmlHelpers {
4+
5+
// See https://stackoverflow.com/a/7594052/4600
6+
private val splitRegex = "(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])"
7+
8+
def friendlyTitle(page: String): String = page.split(splitRegex).mkString(" ").capitalize
9+
}

test/utils/HtmlHelpersSpec.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package utils
2+
3+
import org.specs2.mutable.Specification
4+
5+
class HtmlHelpersSpec extends Specification {
6+
7+
"HtmlHelpers" should {
8+
"when rendering title" in {
9+
"split the camel case title" in {
10+
val input = "ScalaTemplates"
11+
HtmlHelpers.friendlyTitle(input) must_== "Scala Templates"
12+
}
13+
14+
"split the camel case title when there is an acronym" in {
15+
"at the end of the page name" in {
16+
val input = "JavaJPA"
17+
HtmlHelpers.friendlyTitle(input) must_== "Java JPA"
18+
}
19+
20+
"at the begging of the page name" in {
21+
val input = "CSPFilter"
22+
HtmlHelpers.friendlyTitle(input) must_== "CSP Filter"
23+
}
24+
25+
"in the middle of the page name" in {
26+
val input = "ScalaSIRDRouter"
27+
HtmlHelpers.friendlyTitle(input) must_== "Scala SIRD Router"
28+
}
29+
}
30+
31+
"capitalize title's first word" in {
32+
val input = "somePageTitle"
33+
HtmlHelpers.friendlyTitle(input) must_== "Some Page Title"
34+
}
35+
36+
"do not split when it is a single work" in {
37+
val input = "Scala"
38+
HtmlHelpers.friendlyTitle(input) must_== "Scala"
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)