Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 1e97f6c

Browse files
authored
Dokka JSON: support for paragraphs, unordered lists, and links (#219)
* Support for paragrpahs and unordered lists in Dokka JSON * Support for links in Dokka JSON
1 parent bd943d8 commit 1e97f6c

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

spring-auto-restdocs-dokka-json/src/main/kotlin/capital/scalable/dokka/json/JsonFormatService.kt

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ open class JsonOutputBuilder(
8181
val parameterComments = node.content.sections
8282
.filter { it.subjectName != null }
8383
.map {
84-
Pair(it.subjectName!!, extractContent(it))
84+
Pair(it.subjectName!!, extractContent(it, topLevel = true))
8585
}.toMap()
8686
return Pair(node.name, MethodDocumentation(
8787
comment = extractContent(node),
@@ -107,19 +107,49 @@ open class JsonOutputBuilder(
107107
}
108108

109109
private fun extractContent(content: List<ContentNode>): String {
110-
return content.joinToString("") { extractContent(it) }
110+
return content.mapIndexed { index, it -> extractContent(it, topLevel = index == 0) }.joinToString("")
111111
}
112112

113-
private fun extractContent(content: ContentNode): String {
113+
private fun extractContent(content: ContentNode, topLevel: Boolean): String {
114114
when (content) {
115115
is ContentText -> return content.text
116-
is ContentBlock -> return content.children.joinToString("") { extractContent(it) }
117-
is ContentNodeLink -> return content.node?.let { extractContent(it) } ?: ""
116+
is ContentUnorderedList -> return wrap("<ul>", "</ul>", joinChildren(content))
117+
is ContentListItem -> return listItem(content)
118+
is ContentParagraph -> return paragraph(content, topLevel)
119+
is ContentExternalLink -> return "<a href=\"${content.href}\">${joinChildren(content)}</a>"
120+
// Ignore href of references to other code parts and just show the link text, e.g. class name.
121+
is ContentNodeLink -> return joinChildren(content)
122+
// Fallback. Some of the content types above are derived from ContentBlock and
123+
// thus this one has to be after them.
124+
is ContentBlock -> return joinChildren(content)
118125
is ContentEmpty -> return ""
119126
else -> logger.warn("Unhandled content node: $content")
120127
}
121128
return ""
122129
}
130+
131+
private fun paragraph(paragraph: ContentParagraph, topLevel: Boolean): String {
132+
return if (topLevel) {
133+
// Ignore paragraphs on the top level
134+
joinChildren(paragraph)
135+
} else {
136+
wrap("<p>", "</p>", joinChildren(paragraph))
137+
}
138+
}
139+
140+
private fun listItem(item: ContentListItem): String {
141+
val child = item.children.singleOrNull()
142+
return if (child is ContentParagraph) {
143+
// Ignore paragraph if item is nested underneath an item
144+
wrap("<li>", "</li>", joinChildren(child))
145+
} else {
146+
wrap("<li>", "</li>", joinChildren(item))
147+
}
148+
}
149+
150+
private fun wrap(prefix: String, suffix: String, body: String): String = "$prefix$body$suffix"
151+
152+
private fun joinChildren(block: ContentBlock): String = block.children.joinToString("") { extractContent(it, topLevel = false) }
123153
}
124154

125155
open class JsonFormatService @Inject constructor(private val logger: DokkaLogger) : FormatService {

spring-auto-restdocs-dokka-json/testdata/JavaClass.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
/**
44
* Javadoc on a Java class
5+
* <p>
6+
* Next paragraph
7+
*
8+
* <ul>
9+
* <li>Item 1</li>
10+
* <li>Item 2</li>
11+
* </ul>
512
*/
613
public class JavaClass {
714

@@ -19,7 +26,7 @@ public class JavaClass {
1926
*
2027
* @param a First param a
2128
* @param b Second param b
22-
* @see JavaClass
29+
* @see <a href="http://some-url.com">some link</a>
2330
* @deprecated Use something else
2431
* @title Custom tag
2532
*/

spring-auto-restdocs-dokka-json/testdata/JavaClass.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"comment" : "Javadoc on a Java class",
2+
"comment" : "Javadoc on a Java class <p> Next paragraph </p><ul> <li>Item 1</li> <li>Item 2</li> </ul>",
33
"fields" : {
44
"someField" : {
55
"comment" : "A Java field",
@@ -18,7 +18,7 @@
1818
},
1919
"tags" : {
2020
"parameters" : "Second param b",
21-
"see" : "JavaClass",
21+
"see" : "<a href=\"http://some-url.com\">some link</a>",
2222
"title" : "Custom tag"
2323
}
2424
}

spring-auto-restdocs-dokka-json/testdata/KotlinDataClass.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"comment" : "Class documentation",
2+
"comment" : "Class documentation<p>Next paragraph</p><ul><li>Item 1</li><li>Item 2</li></ul>",
33
"fields" : {
44
"text" : {
55
"comment" : "Documentation for text property",
@@ -16,7 +16,7 @@
1616
},
1717
"methods" : {
1818
"add" : {
19-
"comment" : "Function add",
19+
"comment" : "Function add<p><a href=\"http://some-url.com\">some link</a></p>",
2020
"parameters" : {
2121
"a" : "First param a",
2222
"b" : "Second param b"

spring-auto-restdocs-dokka-json/testdata/KotlinDataClass.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import java.math.BigDecimal
44

55
/**
66
* Class documentation
7+
*
8+
* Next paragraph
9+
*
10+
* * Item 1
11+
* * Item 2
712
*/
813
data class KotlinDataClass(
914
/**
@@ -22,6 +27,8 @@ data class KotlinDataClass(
2227
/**
2328
* Function add
2429
*
30+
* [some link](http://some-url.com)
31+
*
2532
* @param a First param a
2633
* @param b Second param b
2734
* @see KotlinDataClass

0 commit comments

Comments
 (0)