Skip to content

Commit

Permalink
Fix handling of multiple nodes in list items
Browse files Browse the repository at this point in the history
  • Loading branch information
JojOatXGME committed Apr 24, 2023
1 parent b9aa6e8 commit f27903a
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

### Fixed
- No-longer discard all but the last paragraph in list items [#133](../../issues/133) [#147](../../issues/147)

## [2.0.0] - 2022-10-28

### Added
Expand Down
23 changes: 21 additions & 2 deletions src/main/kotlin/org/jetbrains/changelog/Changelog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,17 @@ data class Changelog(
HTML,
}

private fun ASTNode?.textAsIs(customContent: String? = null) = this
?.getTextInNode(customContent ?: content)
?.let {
when (type) {
EOL -> lineSeparator
else -> it
}
}
?.toString()
.orEmpty()

private fun ASTNode?.text(customContent: String? = null) = this
?.getTextInNode(customContent ?: content)
?.let {
Expand Down Expand Up @@ -431,7 +442,11 @@ data class Changelog(
.flatMap { list ->
list.children
.filter { it.type == org.intellij.markdown.MarkdownElementTypes.LIST_ITEM }
.map { it.children.last().text(content) }
.map { listItem ->
listItem.children
.drop(1) // MarkdownTokenTypes.LIST_BULLET or LIST_NUMBER
.joinToString("") { it.textAsIs(content) }
}
.toSet()
}
.toSet()
Expand All @@ -445,7 +460,11 @@ data class Changelog(
.associate { (left, right) ->
left.text(content) to right.children
.filter { it.type == org.intellij.markdown.MarkdownElementTypes.LIST_ITEM }
.map { it.children.last().text(content) }
.map { listItem ->
listItem.children
.drop(1) // MarkdownTokenTypes.LIST_BULLET or LIST_NUMBER
.joinToString("") { it.textAsIs(content) }
}
.toSet()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,108 @@ class PatchChangelogTaskTest : BaseTest() {
assertTrue(changelog.endsWith(lineSeparator))
}

@Test
fun `preserves complex list items`() {
changelog =
"""
# Changelog
## [Unreleased]
### Changed
- This item has two paragraphs.
All paragraphs should be preserved.
Separate paragraphs must not be merged.
- This item contains a nested list.
- Sub-Item 1
- Sub-Item 2
- ```
This item is a code block
```
## [0.0.1] - 2022-10-10
### Added
- ```
A code block could also be followed by a list
```
- Sub-Item 1
- Sub-Item 2
| Header |
|-----------------------------|
| There could also be a table |
### Fixed
- Following sections must stay unaffected.
""".trimIndent()

runTask(PATCH_CHANGELOG_TASK_NAME)

assertMarkdown(
"""
# Changelog
## [Unreleased]
### Added
### Changed
### Deprecated
### Removed
### Fixed
### Security
## [1.0.0] - $date
### Changed
- This item has two paragraphs.
All paragraphs should be preserved.
Separate paragraphs must not be merged.
- This item contains a nested list.
- Sub-Item 1
- Sub-Item 2
- ```
This item is a code block
```
## [0.0.1] - 2022-10-10
### Added
- ```
A code block could also be followed by a list
```
- Sub-Item 1
- Sub-Item 2
| Header |
|-----------------------------|
| There could also be a table |
### Fixed
- Following sections must stay unaffected.
[Unreleased]: https://github.com/JetBrains/gradle-changelog-plugin/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/JetBrains/gradle-changelog-plugin/compare/v0.0.1...v1.0.0
[0.0.1]: https://github.com/JetBrains/gradle-changelog-plugin/commits/v0.0.1
""".trimIndent(),
changelog
)
}

@Test
fun `removes empty groups`() {
changelog =
Expand Down

0 comments on commit f27903a

Please sign in to comment.