chore: various markdown fixes

This commit is contained in:
Diego Beraldin 2024-02-21 14:04:21 +01:00
parent bd486855f8
commit 242c329d33

View File

@ -19,8 +19,8 @@ internal fun ASTNode.findChildOfTypeRecursive(type: IElementType): ASTNode? {
internal fun String.sanitize(): String = this internal fun String.sanitize(): String = this
.removeEntities() .removeEntities()
.spoilerFixup() .spoilerFixUp()
.quoteFixup() .quoteFixUp()
.expandLemmyHandles() .expandLemmyHandles()
private fun String.removeEntities(): String = private fun String.removeEntities(): String =
@ -29,11 +29,12 @@ private fun String.removeEntities(): String =
.replace("…", "") .replace("…", "")
private fun String.spoilerFixup(): String = run { private fun String.spoilerFixUp(): String = run {
val finalLines = mutableListOf<String>() val finalLines = mutableListOf<String>()
var finalLinesSizeAtLastSpoiler = 0 var finalLinesSizeAtLastSpoiler = 0
lines().forEach { line -> lines().forEach { line ->
val isSpoilerOnTopOfStack = finalLinesSizeAtLastSpoiler == finalLines.size val isSpoilerOnTopOfStack =
finalLines.isNotEmpty() && finalLinesSizeAtLastSpoiler == finalLines.size
if (line.contains(SpoilerRegex.spoilerOpening)) { if (line.contains(SpoilerRegex.spoilerOpening)) {
if (finalLines.lastOrNull()?.isEmpty() == false) { if (finalLines.lastOrNull()?.isEmpty() == false) {
finalLines += "" finalLines += ""
@ -57,18 +58,15 @@ private fun String.spoilerFixup(): String = run {
finalLines.joinToString("\n") finalLines.joinToString("\n")
} }
private fun String.quoteFixup(): String = run { private fun String.quoteFixUp(): String = run {
val finalLines = mutableListOf<String>() val finalLines = mutableListOf<String>()
lines().forEach { line -> lines().forEach { line ->
// removes list inside quotes // removes list inside quotes
val quoteAndList = Regex("^>\\s*?-") val quoteAndList = Regex("^>-")
if (quoteAndList.matches(line)) { val cleanLine = line.replace(quoteAndList, "-")
val cleanLine = line.replace(quoteAndList, ">") val isLastEmpty = finalLines.isNotEmpty() && finalLines.last().isEmpty()
if (cleanLine.isNotEmpty()) { if (!isLastEmpty || cleanLine.isNotEmpty()) {
finalLines += cleanLine finalLines += cleanLine
}
} else {
finalLines += line
} }
} }
finalLines.joinToString("\n") finalLines.joinToString("\n")