Skip to content

Commit

Permalink
fix: improve HTML to BBCode back-conversion (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkesiSeli authored Dec 15, 2024
1 parent 8ab2772 commit 4824ef1
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ internal class DefaultBBCodeConverter : BBCodeConverter {
.Builder()
.onOpenTag { name, attributes, _ ->
when (name) {
"h1" -> builder.append("[h1]")
"h2" -> builder.append("[h2]")
"h3" -> builder.append("[h3]")
"h4" -> builder.append("[h4]")
"h5" -> builder.append("[h5]")
"br" -> builder.appendLine()
"b", "strong" -> builder.append("[b]")
"u" -> builder.append("[u]")
Expand All @@ -68,6 +73,7 @@ internal class DefaultBBCodeConverter : BBCodeConverter {
"li" -> builder.append("[li]")
"code" -> builder.append("[code]")
"blockquote" -> builder.append("[quote]")
"q" -> builder.append("[quote]")
"a" -> {
val url = attributes["href"] ?: ""
builder.append("[url=$url]")
Expand All @@ -83,7 +89,13 @@ internal class DefaultBBCodeConverter : BBCodeConverter {
}
}.onCloseTag { name, _ ->
when (name) {
"p", "span", "br" -> Unit
"h1" -> builder.append("[/h1]")
"h2" -> builder.append("[/h2]")
"h3" -> builder.append("[/h3]")
"h4" -> builder.append("[/h4]")
"h5" -> builder.append("[/h5]")
"span" -> Unit
"p", "br" -> builder.append("\n")
"img" -> builder.append("[/img]")
"b", "strong" -> builder.append("[/b]")
"u" -> builder.append("[/u]")
Expand All @@ -95,6 +107,7 @@ internal class DefaultBBCodeConverter : BBCodeConverter {
"ol" -> builder.append("[/ol]")
"li" -> builder.append("[/li]")
"blockquote" -> builder.append("[/quote]")
"q" -> builder.append("[/quote]")
else -> Unit
}
}.onText { text ->
Expand Down

0 comments on commit 4824ef1

Please # to comment.