diff --git a/src/main/scala/app/WikiController.scala b/src/main/scala/app/WikiController.scala index c929ea1..c23c41a 100644 --- a/src/main/scala/app/WikiController.scala +++ b/src/main/scala/app/WikiController.scala @@ -146,9 +146,11 @@ } post("/:owner/:repository/wiki/_preview"){ - val content = params("content") + val owner = params("owner") + val repository = params("repository") + val content = params("content") contentType = "text/html" - view.helpers.markdown(content) + view.helpers.markdown(content, JGitUtil.getRepositoryInfo(owner, repository, servletContext)) } /** diff --git a/src/main/scala/view/helpers.scala b/src/main/scala/view/helpers.scala index 6a3b1b3..4756da9 100644 --- a/src/main/scala/view/helpers.scala +++ b/src/main/scala/view/helpers.scala @@ -2,6 +2,10 @@ import java.util.Date import java.text.SimpleDateFormat +import org.pegdown._ +import org.pegdown.LinkRenderer.Rendering +import org.pegdown.ast.WikiLinkNode + object helpers { /** @@ -18,9 +22,38 @@ def format(value: String): twirl.api.Html = twirl.api.Html( value.replaceAll(" ", " ").replaceAll("\t", "    ").replaceAll("\n", "
")) - def markdown(value: String): twirl.api.Html = { + /** + * Converts Markdown of Wiki pages to HTML. This method supports Wiki links. + */ + def markdown(value: String, repository: app.RepositoryInfo)(implicit context: app.Context): twirl.api.Html = { import org.pegdown._ - val html = new PegDownProcessor().markdownToHtml(value) + val html = new PegDownProcessor(Extensions.AUTOLINKS|Extensions.WIKILINKS) + .markdownToHtml(value, new LinkRenderer(){ + override def render(node: WikiLinkNode): Rendering = { + try { + val text = node.getText + val (label, page) = if(text.contains('|')){ + val i = text.indexOf('|') + (text.substring(0, i), text.substring(i + 1)) + } else { + (text, text) + } + val url = "%s/%s/%s/wiki/%s".format(context.path, repository.owner, repository.name, + java.net.URLEncoder.encode(page.replace(' ', '-'), "UTF-8")) + new Rendering(url, label); + } catch { + case e: java.io.UnsupportedEncodingException => throw new IllegalStateException(); + } + } + }) + twirl.api.Html(html) + } + + /** + * Converts Markdown to HTML. This method does not support Wiki links. + */ + def markdown(value: String): twirl.api.Html = { + val html = new PegDownProcessor().markdownToHtml(value, new LinkRenderer()) twirl.api.Html(html) } diff --git a/src/main/twirl/wiki/wiki.scala.html b/src/main/twirl/wiki/wiki.scala.html index 7c12a7d..2fff768 100644 --- a/src/main/twirl/wiki/wiki.scala.html +++ b/src/main/twirl/wiki/wiki.scala.html @@ -18,7 +18,7 @@
- @helpers.markdown(page.content) + @helpers.markdown(page.content, repository)
Last edited by @page.committer at @helpers.datetime(page.time)