mirror of
				https://github.com/gitbucket/gitbucket.git
				synced 2025-10-31 18:46:28 +01:00 
			
		
		
		
	Rewrite relative links to reflect the base url of the repo.
This commit is contained in:
		| @@ -1,17 +1,14 @@ | ||||
| package view | ||||
|  | ||||
| import util.StringUtil | ||||
| import util.ControlUtil._ | ||||
| import util.Directory._ | ||||
| import org.parboiled.common.StringUtils | ||||
| import org.pegdown._ | ||||
| import org.pegdown.ast._ | ||||
| import org.pegdown.LinkRenderer.Rendering | ||||
| import java.text.Normalizer | ||||
| import java.util.Locale | ||||
| import scala.collection.JavaConverters._ | ||||
| import service.{ RequestCache, WikiService } | ||||
| import org.asciidoctor.{ Asciidoctor, Attributes, AttributesBuilder, OptionsBuilder, SafeMode } | ||||
| import org.asciidoctor.Asciidoctor | ||||
| import org.asciidoctor.AttributesBuilder | ||||
| import org.asciidoctor.OptionsBuilder | ||||
| import org.asciidoctor.SafeMode | ||||
| import org.htmlcleaner.HtmlCleaner | ||||
| import org.htmlcleaner.HtmlNode | ||||
| import org.htmlcleaner.SimpleHtmlSerializer | ||||
| import org.htmlcleaner.TagNode | ||||
| import org.htmlcleaner.TagNodeVisitor | ||||
|  | ||||
| object Asciidoc { | ||||
|  | ||||
| @@ -20,14 +17,41 @@ object Asciidoc { | ||||
|   /** | ||||
|    * Converts Markdown of Wiki pages to HTML. | ||||
|    */ | ||||
|   def toHtml(asciidoc: String, repository: service.RepositoryService.RepositoryInfo, | ||||
|   def toHtml(asciidoc: String, branch: String, repository: service.RepositoryService.RepositoryInfo, | ||||
|              enableWikiLink: Boolean, enableRefsLink: Boolean)(implicit context: app.Context): String = { | ||||
|  | ||||
|     val options = OptionsBuilder.options() | ||||
|     options.safe(SafeMode.SECURE) | ||||
|     val attributes = AttributesBuilder.attributes() | ||||
|     attributes.showTitle(true) | ||||
|     options.attributes(attributes.get()) | ||||
|     asciidoctor.render(asciidoc, options) | ||||
|     val rendered = asciidoctor.render(asciidoc, options) | ||||
|  | ||||
|     // this is always relative to the base dir of the repo, as we currently only render README files. | ||||
|     val relativeUrlPrefix = s"${helpers.url(repository)}/blob/${branch}/" | ||||
|     prefixRelativeUrls(rendered, relativeUrlPrefix) | ||||
|   } | ||||
|  | ||||
|   def prefixRelativeUrls(html: String, urlPrefix: String): String = { | ||||
|     val cleaner = new HtmlCleaner() | ||||
|     val node = cleaner.clean(html) | ||||
|     node.traverse(new TagNodeVisitor() { | ||||
|       override def visit(tagNode: TagNode, htmlNode: HtmlNode): Boolean = { | ||||
|         htmlNode match { | ||||
|           case tag: TagNode if tag.getName == "a" => | ||||
|             Option(tag.getAttributeByName("href")) foreach { href => | ||||
|               if (!href.startsWith("/") && !href.startsWith("http://") && !href.startsWith("https://")) { | ||||
|                 tag.addAttribute("href", s"${urlPrefix}${href}") | ||||
|               } | ||||
|             } | ||||
|           case _ => | ||||
|         } | ||||
|         // continue traversal | ||||
|         true | ||||
|       } | ||||
|     }) | ||||
|     new SimpleHtmlSerializer(cleaner.getProperties()).getAsString(node) | ||||
|   } | ||||
|  | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user