Skip to content

Commit

Permalink
fix: add missing new lines for <br> in compact mode by ensuring the e…
Browse files Browse the repository at this point in the history
…xtra new line is only consumed once at each paragraph style boundary
  • Loading branch information
cbeyls committed Oct 19, 2024
1 parent 370feea commit e264b73
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ internal class AnnotatedStringHtmlHandler(
private val linkInteractionListener: LinkInteractionListener?
) : HtmlHandler {
private val textWriter = HtmlTextWriter(builder, object : HtmlTextWriter.Callbacks {
private var consumedNewLineIndex = -1

override fun onWriteNewLines(newLineCount: Int): Int {
val currentIndex = builder.length
val paragraphIndex = paragraphStartIndex
// Paragraph style will automatically add one new line at its start and its end
return if (currentIndex == paragraphIndex || currentIndex == paragraphIndex.inv())
newLineCount - 1
else newLineCount
if (currentIndex != consumedNewLineIndex) {
val paragraphIndex = paragraphStartIndex
if (currentIndex == paragraphIndex || currentIndex == paragraphIndex.inv()) {
// Paragraph style will automatically add a single new line at each boundary
consumedNewLineIndex = currentIndex
return newLineCount - 1
}
}
return newLineCount
}

override fun onWriteContentStart() {
Expand Down Expand Up @@ -279,11 +285,9 @@ internal class AnnotatedStringHtmlHandler(
if (indent) {
blockIndentLevel--
}
paragraphStartIndex = if (level == 0) {
if (hasTrailingParagraph) {
// Encode the end position of the trailing paragraph as a negative value using bit inversion
if (hasTrailingParagraph) currentIndex.inv() else -1
} else {
currentIndex
paragraphStartIndex = if (level == 0) currentIndex.inv() else currentIndex
}
}
textWriter.markBlockBoundary(if (compactMode) 1 else suffixNewLineCount, 0)
Expand Down

0 comments on commit e264b73

Please sign in to comment.