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

Bugfix/#4 slugification #5

Merged
merged 2 commits into from
May 30, 2017
Merged
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
4 changes: 3 additions & 1 deletion app/readme/TableOfContentHelper.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package readme

import util.TextUtil

object TableOfContentHelper {

val prefix = "#"
Expand All @@ -24,7 +26,7 @@ object TableOfContentHelper {
private def getLindex(line: String): LineIndex = {
val count = line.count(_ == '#')
val text = line.substring(count, line.length).trim
val title = s"- [$text](#${text.toLowerCase.split(' ').mkString("-")})"
val title = s"- [$text](#${TextUtil.slugify(text)})"
LineIndex(count, title)
}

Expand Down
16 changes: 16 additions & 0 deletions app/util/TextUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

//https://gist.github.com/sam/5213151

object TextUtil {

def slugify(input: String): String = {
import java.text.Normalizer
Normalizer.normalize(input, Normalizer.Form.NFD)
.replaceAll("[^\\w\\s-]", "") // Remove all non-word, non-space or non-dash characters
.replace('-', ' ') // Replace dashes with spaces
.trim // Trim leading/trailing whitespace (including what used to be leading/trailing dashes)
.replaceAll("\\s+", "-") // Replace whitespace (including newlines and repetitions) with single dashes
.toLowerCase // Lowercase the final results
}
}