aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/translators/documentables/briefFromContentNodes.kt
blob: ca66c1a8a055fd313e5b02cb39aef53c5570fafa (plain)
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
package org.jetbrains.dokka.base.translators.documentables

import org.jetbrains.dokka.pages.ContentGroup
import org.jetbrains.dokka.pages.ContentNode
import org.jetbrains.dokka.pages.ContentText
import org.jetbrains.dokka.pages.TextStyle

fun briefFromContentNodes(description: List<ContentNode>): List<ContentNode> {
    val firstSentenceRegex = """^((?:[^.?!]|[.!?](?!\s))*[.!?])""".toRegex()

    var sentenceFound = false
    fun lookthrough(node: ContentNode): ContentNode =
        if (node is ContentText && firstSentenceRegex.containsMatchIn(node.text)) {
            sentenceFound = true
            node.copy(text = firstSentenceRegex.find(node.text)?.value.orEmpty())
        } else if (node is ContentGroup) {
            node.copy(children = node.children.mapNotNull {
                if (!sentenceFound) lookthrough(it) else null
            }, style = node.style - TextStyle.Paragraph)
        } else {
            node
        }
    return description.mapNotNull {
        if (!sentenceFound) lookthrough(it) else null
    }
}