aboutsummaryrefslogtreecommitdiff
path: root/doc-tool/src/dotty/tools/dottydoc/staticsite/MarkdownLinkVisitor.scala
blob: 5216ee950675d297626a74702280c83fa23d7b99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package dotty.tools
package dottydoc
package staticsite

import com.vladsch.flexmark.ast._
import com.vladsch.flexmark.util.sequence.CharSubSequence
import model.{ Package, NonEntity, Val, Def, TypeAlias }
import dottydoc.util.MemberLookup

object MarkdownLinkVisitor {
  private val EntityLink = """([^\.]+)(\.[^\.]+)*""".r
  def apply(node: Node, docs: Map[String, Package], params: Map[String, AnyRef]): Unit =
    (new NodeVisitor(
      new VisitHandler(classOf[Link], new Visitor[Link] with MemberLookup {
        override def visit(node: Link): Unit = {
          val url = node.getUrl
          if (url.endsWith(".md")) node.setUrl {
            url.subSequence(0, url.lastIndexOf('.')).append(".html")
          }
          else if (EntityLink.unapplySeq(url.toString).isDefined) {
            lookup(NonEntity, docs, url.toString).foreach { ent =>
              val (path, suffix) = ent match {
                case ent: Val => (ent.path.dropRight(1), ".html#" + ent.signature)
                case ent: Def => (ent.path.dropRight(1), ".html#" + ent.signature)
                case ent: TypeAlias => (ent.path.dropRight(1), ".html#" + ent.signature)
                case ent: Package => (ent.path, "/index.html")
                case ent => (ent.path, ".html")
              }

              params("site") match {
                case map: java.util.Map[String, String] @unchecked => node.setUrl {
                  CharSubSequence.of(path.mkString(map.get("baseurl") + "/api/", "/", suffix))
                }
                case _ => ()
              }
            }
          }
        }
      })
    ))
    .visit(node)
}