Support extensions on external classes correctly
diff --git a/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormat.kt b/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormat.kt
index cfa3e63..494e05f 100644
--- a/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormat.kt
+++ b/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormat.kt
@@ -52,10 +52,6 @@
     fun mainUri(node: DocumentationNode): URI = tryGetMainUri(node) ?: error("Unsupported ${node.kind}")
 
     fun linkTo(to: DocumentationNode, from: URI): String {
-        to.links(NodeKind.ExternalLink).firstOrNull()?.let {
-            return it.name
-        }
-
         return mainUri(to).relativeTo(from).toString()
     }
 
diff --git a/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormatOutputBuilder.kt b/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormatOutputBuilder.kt
index 51ac323..257ae49 100644
--- a/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormatOutputBuilder.kt
+++ b/core/src/main/kotlin/Formats/JavaLayoutHtml/JavaLayoutHtmlFormatOutputBuilder.kt
@@ -258,10 +258,12 @@
     }
 
     protected open fun FlowContent.a(href: DocumentationNode, classes: String? = null, block: A.() -> Unit) {
-        val hrefText = if (href.kind == NodeKind.ExternalLink)
-            href.name
-        else
-            uriProvider.linkTo(href, uri)
+        val hrefText =
+            href.name.takeIf { href.kind == NodeKind.ExternalLink }
+                    ?: href.links.firstOrNull { it.kind == NodeKind.ExternalLink }?.name
+                    ?: "#".takeIf { href.kind == NodeKind.ExternalClass } // When external class unresolved
+                    ?: uriProvider.linkTo(href, uri)
+
         a(href = hrefText, classes = classes, block = block)
     }
 
@@ -730,8 +732,10 @@
             val annotations = node.members(NodeKind.AnnotationClass)
             val enums = node.members(NodeKind.Enum)
 
-            val functions = node.members(NodeKind.Function)
-            val properties = node.members(NodeKind.Property)
+            val functions = node.members(NodeKind.Function) +
+                    node.members(NodeKind.ExternalClass).flatMap { it.members(NodeKind.Function) }
+            val properties = node.members(NodeKind.Property) +
+                    node.members(NodeKind.ExternalClass).flatMap { it.members(NodeKind.Property) }
         }
     }
 }
diff --git a/core/src/test/kotlin/format/JavaLayoutHtmlFormatTest.kt b/core/src/test/kotlin/format/JavaLayoutHtmlFormatTest.kt
index d32ce5e..7a16df5 100644
--- a/core/src/test/kotlin/format/JavaLayoutHtmlFormatTest.kt
+++ b/core/src/test/kotlin/format/JavaLayoutHtmlFormatTest.kt
@@ -22,4 +22,16 @@
             listOf(model.members.single().members.single { it.name == "foo" })
         }
     }
+
+    @Test
+    fun externalClassExtension() {
+        verifyPackageNode("externalClassExtension.kt")
+    }
+
+    @Test
+    fun unresolvedExternalClass() {
+        verifyNode("unresolvedExternalClass.kt", noStdlibLink = true) { model ->
+            listOf(model.members.single().members.single { it.name == "MyException" })
+        }
+    }
 }
\ No newline at end of file
diff --git a/core/src/test/kotlin/format/JavaLayoutHtmlFormatTestCase.kt b/core/src/test/kotlin/format/JavaLayoutHtmlFormatTestCase.kt
index dcdd612..260ca99 100644
--- a/core/src/test/kotlin/format/JavaLayoutHtmlFormatTestCase.kt
+++ b/core/src/test/kotlin/format/JavaLayoutHtmlFormatTestCase.kt
@@ -70,13 +70,13 @@
             }
         }
 
-    protected fun verifyNode(fileName: String, select: (model: DocumentationNode) -> List<DocumentationNode>) {
+    protected fun verifyNode(fileName: String, noStdlibLink: Boolean = false, select: (model: DocumentationNode) -> List<DocumentationNode>) {
         verifyOutput(
             "testdata/format/java-layout-html/$fileName",
             ".html",
             format = "java-layout-html",
             withKotlinRuntime = true,
-            noStdlibLink = false,
+            noStdlibLink = noStdlibLink,
             collectInheritedExtensionsFromLibraries = true
         ) { model, output ->
             buildPagesAndReadInto(
@@ -88,14 +88,16 @@
     }
 
     protected fun verifyNode(fileName: String) {
-        verifyNode(fileName, { model -> listOf(model.members.single().members.single()) })
+        verifyNode(fileName) { model -> listOf(model.members.single().members.single()) }
     }
 
-    protected fun verifyPackageNode(fileName: String) {
+    protected fun verifyPackageNode(fileName: String, noStdlibLink: Boolean = false) {
         verifyOutput(
             "testdata/format/java-layout-html/$fileName",
             ".package-summary.html",
-            format = "java-layout-html"
+            format = "java-layout-html",
+            withKotlinRuntime = true,
+            noStdlibLink = noStdlibLink
         ) { model, output ->
             buildPagesAndReadInto(
                 model,
diff --git a/core/testdata/format/java-layout-html/externalClassExtension.kt b/core/testdata/format/java-layout-html/externalClassExtension.kt
new file mode 100644
index 0000000..04415cc
--- /dev/null
+++ b/core/testdata/format/java-layout-html/externalClassExtension.kt
@@ -0,0 +1,5 @@
+package p
+
+fun String.ext() {
+    println(this)
+}
\ No newline at end of file
diff --git a/core/testdata/format/java-layout-html/externalClassExtension.package-summary.html b/core/testdata/format/java-layout-html/externalClassExtension.package-summary.html
new file mode 100644
index 0000000..39db6c8
--- /dev/null
+++ b/core/testdata/format/java-layout-html/externalClassExtension.package-summary.html
@@ -0,0 +1,25 @@
+<!-- File: /test/p/package-summary.html -->
+<html>
+  <head>
+    <meta charset="UTF-8">
+  </head>
+  <body>
+    <h1>p</h1>
+    <h2>Top-level functions summary</h2>
+    <table>
+      <tbody>
+        <tr>
+          <td><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></td>
+          <td>
+            <div><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a>.<a href="#%28kotlin.String%29.ext%28%29">ext</a>()</code></div>
+          </td>
+        </tr>
+      </tbody>
+    </table>
+    <h2>Top-level functions</h2>
+    <div id="(kotlin.String).ext()">
+      <h3>ext</h3>
+      <pre><span class="keyword">fun </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a><span class="symbol">.</span><span class="identifier">ext</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></pre>
+    </div>
+  </body>
+</html>
diff --git a/core/testdata/format/java-layout-html/topLevel.package-summary.html b/core/testdata/format/java-layout-html/topLevel.package-summary.html
index 8c34f97..791c371 100644
--- a/core/testdata/format/java-layout-html/topLevel.package-summary.html
+++ b/core/testdata/format/java-layout-html/topLevel.package-summary.html
@@ -18,13 +18,13 @@
     <table>
       <tbody>
         <tr>
-          <td><span class="identifier">Unit</span></td>
+          <td><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></td>
           <td>
             <div><code><a href="#topLevelFun%28%29">topLevelFun</a>()</code></div>
           </td>
         </tr>
         <tr>
-          <td><span class="identifier">Unit</span></td>
+          <td><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></td>
           <td>
             <div><code><a href="Some.html#"><span class="identifier">Some</span></a>.<a href="#%28p.Some%29.topLevelExtFun%28%29">topLevelExtFun</a>()</code></div>
           </td>
@@ -35,25 +35,25 @@
     <table>
       <tbody>
         <tr>
-          <td><span class="identifier">String</span></td>
+          <td><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></td>
           <td>
             <div><code><a href="#topLevelVal%3Akotlin.String">topLevelVal</a></code></div>
           </td>
         </tr>
         <tr>
-          <td><span class="keyword">const</span> <span class="identifier">String</span></td>
+          <td><span class="keyword">const</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></td>
           <td>
             <div><code><a href="#topLevelConst%3Akotlin.String">topLevelConst</a></code></div>
           </td>
         </tr>
         <tr>
-          <td><span class="identifier">String</span></td>
+          <td><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></td>
           <td>
             <div><code><a href="#topLevelGetVal%3Akotlin.String">topLevelGetVal</a></code></div>
           </td>
         </tr>
         <tr>
-          <td><span class="identifier">String</span></td>
+          <td><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></td>
           <td>
             <div><code><a href="#%28p.Some%29.topLevelExtVal%3Akotlin.String">topLevelExtVal</a></code></div>
           </td>
@@ -63,28 +63,28 @@
     <h2>Top-level functions</h2>
     <div id="topLevelFun()">
       <h3>topLevelFun</h3>
-      <pre><span class="keyword">fun </span><span class="identifier">topLevelFun</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><span class="identifier">Unit</span></pre>
+      <pre><span class="keyword">fun </span><span class="identifier">topLevelFun</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></pre>
     </div>
     <div id="(p.Some).topLevelExtFun()">
       <h3>topLevelExtFun</h3>
-      <pre><span class="keyword">fun </span><a href="Some.html#"><span class="identifier">Some</span></a><span class="symbol">.</span><span class="identifier">topLevelExtFun</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><span class="identifier">Unit</span></pre>
+      <pre><span class="keyword">fun </span><a href="Some.html#"><span class="identifier">Some</span></a><span class="symbol">.</span><span class="identifier">topLevelExtFun</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></pre>
     </div>
     <h2>Top-level properties</h2>
     <div id="topLevelVal:kotlin.String">
       <h3>topLevelVal</h3>
-      <pre><span class="keyword">val </span><span class="identifier">topLevelVal</span><span class="symbol">: </span><span class="identifier">String</span></pre>
+      <pre><span class="keyword">val </span><span class="identifier">topLevelVal</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></pre>
     </div>
     <div id="topLevelConst:kotlin.String">
       <h3>topLevelConst</h3>
-      <pre><span class="keyword">const</span> <span class="keyword">val </span><span class="identifier">topLevelConst</span><span class="symbol">: </span><span class="identifier">String</span></pre>
+      <pre><span class="keyword">const</span> <span class="keyword">val </span><span class="identifier">topLevelConst</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></pre>
     </div>
     <div id="topLevelGetVal:kotlin.String">
       <h3>topLevelGetVal</h3>
-      <pre><span class="keyword">val </span><span class="identifier">topLevelGetVal</span><span class="symbol">: </span><span class="identifier">String</span></pre>
+      <pre><span class="keyword">val </span><span class="identifier">topLevelGetVal</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></pre>
     </div>
     <div id="(p.Some).topLevelExtVal:kotlin.String">
       <h3>topLevelExtVal</h3>
-      <pre><span class="keyword">val </span><a href="Some.html#"><span class="identifier">Some</span></a><span class="symbol">.</span><span class="identifier">topLevelExtVal</span><span class="symbol">: </span><span class="identifier">String</span></pre>
+      <pre><span class="keyword">val </span><a href="Some.html#"><span class="identifier">Some</span></a><span class="symbol">.</span><span class="identifier">topLevelExtVal</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html"><span class="identifier">String</span></a></pre>
     </div>
   </body>
 </html>
diff --git a/core/testdata/format/java-layout-html/unresolvedExternalClass.html b/core/testdata/format/java-layout-html/unresolvedExternalClass.html
new file mode 100644
index 0000000..eca242c
--- /dev/null
+++ b/core/testdata/format/java-layout-html/unresolvedExternalClass.html
@@ -0,0 +1,63 @@
+<!-- File: /test/p/MyException.html# -->
+<html>
+  <head>
+    <meta charset="UTF-8">
+  </head>
+  <body>
+    <h1>MyException</h1>
+    <pre><span class="keyword">class </span><span class="identifier">MyException</span>&nbsp;<span class="symbol">:</span>&nbsp;<span class="identifier">Exception</span></pre>
+    <table>
+      <tr>
+        <td><a href="#">p.MyException</a></td>
+      </tr>
+    </table>
+    <h2>Summary</h2>
+    <table>
+      <thead>
+        <tr>
+          <td>
+            <h3>Constructors</h3>
+          </td>
+        </tr>
+      </thead>
+      <tbody>
+        <tr>
+          <td>
+            <div><code><a href="#%3Cinit%3E%28%29">&lt;init&gt;</a>()</code></div>
+          </td>
+        </tr>
+      </tbody>
+    </table>
+    <table>
+      <thead>
+        <tr>
+          <td>
+            <h3>Inherited extension functions</h3>
+          </td>
+        </tr>
+      </thead>
+      <tbody>
+        <tr>
+          <td>From <a href="#">p.java.lang.Exception</a>
+            <table>
+              <tbody>
+                <tr>
+                  <td><span class="identifier">Unit</span></td>
+                  <td>
+                    <div><code><span class="identifier">Exception</span>.<a href="package-summary.html#%28kotlin.Exception%29.ext%28%29">ext</a>()</code></div>
+                    <p>Some docs...</p>
+                  </td>
+                </tr>
+              </tbody>
+            </table>
+          </td>
+        </tr>
+      </tbody>
+    </table>
+    <h2>Constructors</h2>
+    <div id="&lt;init&gt;()">
+      <h3>&lt;init&gt;</h3>
+      <pre><span class="identifier">MyException</span><span class="symbol">(</span><span class="symbol">)</span></pre>
+    </div>
+  </body>
+</html>
diff --git a/core/testdata/format/java-layout-html/unresolvedExternalClass.kt b/core/testdata/format/java-layout-html/unresolvedExternalClass.kt
new file mode 100644
index 0000000..0831dac
--- /dev/null
+++ b/core/testdata/format/java-layout-html/unresolvedExternalClass.kt
@@ -0,0 +1,13 @@
+package p
+
+// noStdlibLink set to true for that test
+
+/**
+ * Some docs...
+ */
+fun Exception.ext() {
+
+}
+
+
+class MyException: Exception()
\ No newline at end of file