mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 05:25:50 +01:00
Support Wiki links.
This commit is contained in:
@@ -2,6 +2,10 @@ package view
|
||||
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 @@ object helpers {
|
||||
def format(value: String): twirl.api.Html = twirl.api.Html(
|
||||
value.replaceAll(" ", " ").replaceAll("\t", " ").replaceAll("\n", "<br>"))
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user