-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from jp7677/section-title
Update structurizr libraries
- Loading branch information
Showing
14 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# Documentation | ||
# History | ||
|
||
## History | ||
|
||
Some notes how we got to the current state. | ||
Some notes how we got to the current state. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# Documentation | ||
|
||
## Usage | ||
# Usage | ||
|
||
This is how we use this thing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/nl/avisi/structurizr/site/generatr/site/model/SectionTitle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package nl.avisi.structurizr.site.generatr.site.model | ||
|
||
import com.structurizr.documentation.Format | ||
import com.structurizr.documentation.Section | ||
import com.vladsch.flexmark.ast.Heading | ||
import com.vladsch.flexmark.ast.Paragraph | ||
import com.vladsch.flexmark.parser.Parser | ||
|
||
private val parser = Parser.builder().build() | ||
private const val MAX_TITLE_LENGTH = 50 | ||
|
||
fun Section.contentTitle(): String { | ||
if (format != Format.Markdown) | ||
return "unsupported document" | ||
|
||
val document = parser.parse(content) | ||
|
||
if (!document.hasChildren()) | ||
return "untitled document" | ||
|
||
val header = document.children.firstOrNull { it is Heading }?.let { it as Heading } | ||
if (header != null) | ||
return header.text.toString() | ||
|
||
val paragraph = document.children.firstOrNull { it is Paragraph }?.let { it as Paragraph }?.chars?.toString() | ||
if (paragraph != null) | ||
return if (paragraph.length > MAX_TITLE_LENGTH) { | ||
val whitespacePosition = paragraph.withIndex() | ||
.filter { it.value.isWhitespace() } | ||
.lastOrNull { it.index < MAX_TITLE_LENGTH } | ||
?.index | ||
paragraph.take(whitespacePosition ?: MAX_TITLE_LENGTH) | ||
} else paragraph | ||
|
||
return "unknown document" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/kotlin/nl/avisi/structurizr/site/generatr/site/model/SectionTitleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package nl.avisi.structurizr.site.generatr.site.model | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import com.structurizr.documentation.Format | ||
import com.structurizr.documentation.Section | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
class SectionTitleTest { | ||
|
||
@Test | ||
fun `no content`() { | ||
val section = Section(Format.Markdown, "") | ||
assertThat(section.contentTitle()).isEqualTo("untitled document") | ||
} | ||
|
||
@Test | ||
fun `only whitespaces`() { | ||
val section = Section(Format.Markdown, " \n ") | ||
assertThat(section.contentTitle()).isEqualTo("untitled document") | ||
} | ||
|
||
@Test | ||
fun `no markdown`() { | ||
val section = Section(Format.AsciiDoc, "== title") | ||
assertThat(section.contentTitle()).isEqualTo("unsupported document") | ||
} | ||
|
||
@Test | ||
fun `short paragraph`() { | ||
val section = Section(Format.Markdown, "some content") | ||
assertThat(section.contentTitle()).isEqualTo("some content") | ||
} | ||
|
||
@Test | ||
fun `long paragraph`() { | ||
val section = Section( | ||
Format.Markdown, | ||
"some very very long content we really need to truncate since no one wants to read such an exhausting title" | ||
) | ||
assertThat(section.contentTitle()).isEqualTo("some very very long content we really need to") | ||
} | ||
|
||
@Test | ||
fun `long paragraph without whitespaces`() { | ||
val section = Section( | ||
Format.Markdown, | ||
"some-very-very-long-content-we-really-need-to-truncate-since-no-one-wants-to-read-such-an-exhausting-title" | ||
) | ||
assertThat(section.contentTitle()).isEqualTo("some-very-very-long-content-we-really-need-to-trun") | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["# header", "## header", "### header"]) | ||
fun `with heading`(content: String) { | ||
val section = Section(Format.Markdown, content) | ||
assertThat(section.contentTitle()).isEqualTo("header") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters