mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-05 04:56:02 +01:00
Fix links
This commit is contained in:
@@ -42,22 +42,15 @@ object Markdown {
|
||||
} else s
|
||||
|
||||
val options = new Options()
|
||||
Marked.marked(source, options, new GitBucketMarkedRenderer(options))
|
||||
|
||||
//
|
||||
// val rootNode = new PegDownProcessor(
|
||||
// Extensions.AUTOLINKS | Extensions.WIKILINKS | Extensions.FENCED_CODE_BLOCKS |
|
||||
// Extensions.TABLES | Extensions.HARDWRAPS | Extensions.SUPPRESS_ALL_HTML | Extensions.STRIKETHROUGH
|
||||
// ).parseMarkdown(source.toCharArray)
|
||||
//
|
||||
// new GitBucketHtmlSerializer(
|
||||
// markdown, repository, enableWikiLink, enableRefsLink, enableAnchor, enableTaskList,
|
||||
// hasWritePermission, pages
|
||||
// ).toHtml(rootNode)
|
||||
val renderer = new GitBucketMarkedRenderer(options, repository, enableWikiLink, enableRefsLink, enableTaskList, hasWritePermission, pages)
|
||||
Marked.marked(source, options, renderer)
|
||||
}
|
||||
}
|
||||
|
||||
class GitBucketMarkedRenderer(options: Options) extends Renderer(options) {
|
||||
class GitBucketMarkedRenderer(options: Options, repository: RepositoryService.RepositoryInfo,
|
||||
enableWikiLink: Boolean, enableRefsLink: Boolean, enableTaskList: Boolean, hasWritePermission: Boolean,
|
||||
pages: List[String])
|
||||
(implicit val context: Context) extends Renderer(options) with LinkConverter with RequestCache {
|
||||
|
||||
override def code(code: String, lang: Optional[String], escaped: Boolean): String = {
|
||||
"<pre class=\"prettyprint" + (if(lang.isPresent) s" ${options.getLangPrefix}${lang.get}" else "" )+ "\">" +
|
||||
@@ -65,7 +58,61 @@ class GitBucketMarkedRenderer(options: Options) extends Renderer(options) {
|
||||
}
|
||||
|
||||
override def text(text: String): String = {
|
||||
text
|
||||
// convert commit id and username to link.
|
||||
val t1 = if(enableRefsLink) convertRefsLinks(text, repository, "issue:") else text
|
||||
|
||||
// convert task list to checkbox.
|
||||
val t2 = if(enableTaskList) GitBucketHtmlSerializer.convertCheckBox(t1, hasWritePermission) else t1
|
||||
|
||||
t2
|
||||
}
|
||||
|
||||
override def image(href: String, title: Optional[String], text: String): String = {
|
||||
super.image(fixUrl(href, true), title, text);
|
||||
}
|
||||
|
||||
override def nolink(text: String): String = {
|
||||
if(enableWikiLink && text.startsWith("[") && text.endsWith("]")){
|
||||
val link = text.replaceAll("(^\\[\\[|\\]\\]$)", "")
|
||||
|
||||
val (label, page) = if(link.contains('|')){
|
||||
val i = link.indexOf('|')
|
||||
(link.substring(0, i), link.substring(i + 1))
|
||||
} else {
|
||||
(link, link)
|
||||
}
|
||||
|
||||
val url = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page)
|
||||
if(pages.contains(page)){
|
||||
"<a href=\"" + url + "\">" + escape(page) + "</a>"
|
||||
} else {
|
||||
"<a href=\"" + url + "\" class=\"absent\">" + escape(page) + "</a>"
|
||||
}
|
||||
} else {
|
||||
escape(text)
|
||||
}
|
||||
}
|
||||
|
||||
private def fixUrl(url: String, isImage: Boolean = false): String = {
|
||||
if(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")){
|
||||
url
|
||||
} else if(url.startsWith("#")){
|
||||
("#" + GitBucketHtmlSerializer.generateAnchorName(url.substring(1)))
|
||||
} else if(!enableWikiLink){
|
||||
if(context.currentPath.contains("/blob/")){
|
||||
url + (if(isImage) "?raw=true" else "")
|
||||
} else if(context.currentPath.contains("/tree/")){
|
||||
val paths = context.currentPath.split("/")
|
||||
val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch
|
||||
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
||||
} else {
|
||||
val paths = context.currentPath.split("/")
|
||||
val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch
|
||||
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
||||
}
|
||||
} else {
|
||||
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -76,50 +123,6 @@ class GitBucketMarkedRenderer(options: Options) extends Renderer(options) {
|
||||
// enableWikiLink: Boolean,
|
||||
// pages: List[String]) extends LinkRenderer with WikiService {
|
||||
//
|
||||
// override def render(node: WikiLinkNode): Rendering = {
|
||||
// if(enableWikiLink){
|
||||
// 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 = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page)
|
||||
//
|
||||
// if(pages.contains(page)){
|
||||
// new Rendering(url, label)
|
||||
// } else {
|
||||
// new Rendering(url, label).withAttribute("class", "absent")
|
||||
// }
|
||||
// } catch {
|
||||
// case e: java.io.UnsupportedEncodingException => throw new IllegalStateException
|
||||
// }
|
||||
// } else {
|
||||
// super.render(node)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//class GitBucketVerbatimSerializer extends VerbatimSerializer {
|
||||
// def serialize(node: VerbatimNode, printer: Printer): Unit = {
|
||||
// printer.println.print("<pre")
|
||||
// if (!StringUtils.isEmpty(node.getType)) {
|
||||
// printer.print(" class=").print('"').print("prettyprint ").print(node.getType).print('"')
|
||||
// }
|
||||
// printer.print(">")
|
||||
// var text: String = node.getText
|
||||
// while (text.charAt(0) == '\n') {
|
||||
// printer.print("<br/>")
|
||||
// text = text.substring(1)
|
||||
// }
|
||||
// printer.printEncoded(text)
|
||||
// printer.print("</pre>")
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//class GitBucketHtmlSerializer(
|
||||
// markdown: String,
|
||||
// repository: RepositoryService.RepositoryInfo,
|
||||
@@ -134,11 +137,6 @@ class GitBucketMarkedRenderer(options: Options) extends Renderer(options) {
|
||||
// Map[String, VerbatimSerializer](VerbatimSerializer.DEFAULT -> new GitBucketVerbatimSerializer).asJava
|
||||
// ) with LinkConverter with RequestCache {
|
||||
//
|
||||
// override protected def printImageTag(rendering: LinkRenderer.Rendering): Unit = {
|
||||
// printer.print("<a target=\"_blank\" href=\"").print(fixUrl(rendering.href, true)).print("\">")
|
||||
// .print("<img src=\"").print(fixUrl(rendering.href, true)).print("\" alt=\"").printEncoded(rendering.text).print("\"/></a>")
|
||||
// }
|
||||
//
|
||||
// override protected def printLink(rendering: LinkRenderer.Rendering): Unit = {
|
||||
// printer.print('<').print('a')
|
||||
// printAttribute("href", fixUrl(rendering.href))
|
||||
@@ -148,28 +146,6 @@ class GitBucketMarkedRenderer(options: Options) extends Renderer(options) {
|
||||
// printer.print('>').print(rendering.text).print("</a>")
|
||||
// }
|
||||
//
|
||||
// private def fixUrl(url: String, isImage: Boolean = false): String = {
|
||||
// if(url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")){
|
||||
// url
|
||||
// } else if(url.startsWith("#")){
|
||||
// ("#" + GitBucketHtmlSerializer.generateAnchorName(url.substring(1)))
|
||||
// } else if(!enableWikiLink){
|
||||
// if(context.currentPath.contains("/blob/")){
|
||||
// url + (if(isImage) "?raw=true" else "")
|
||||
// } else if(context.currentPath.contains("/tree/")){
|
||||
// val paths = context.currentPath.split("/")
|
||||
// val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch
|
||||
// repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
||||
// } else {
|
||||
// val paths = context.currentPath.split("/")
|
||||
// val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch
|
||||
// repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
|
||||
// }
|
||||
// } else {
|
||||
// repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private def printAttribute(name: String, value: String): Unit = {
|
||||
// printer.print(' ').print(name).print('=').print('"').print(value).print('"')
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user