From 944f086d5498f0ff9fde965b8dc4776252eb5df4 Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Thu, 25 Jul 2024 01:50:59 +0200 Subject: [PATCH] Fix issues with duplicate RenderWrapper conditionals --- .../rendering/wrapper/RenderWrapper.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/wrapper/RenderWrapper.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/wrapper/RenderWrapper.kt index c4e42e29..0aa6afb2 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/wrapper/RenderWrapper.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/wrapper/RenderWrapper.kt @@ -79,14 +79,16 @@ class RenderWrapper(private val code: String) { "\\[\\[if:$placeholder]]((.|\\R)+?)\\[\\[endif:$placeholder]]\\R?".toRegex(RegexOption.MULTILINE) // If there is a match: // Keep the inner content (without the delimiters) if the conditional value is true, remove it otherwise. - regex.findAll(this).forEach { match -> - replace( - match.range.first, - match.range.last + 1, - // First group is the whole match, second group is the inner content without the delimiters. - match.groups[1]?.value?.takeIf { value } ?: "", - ) - } + regex.findAll(this) + .sortedByDescending { it.range.first } // Iterate backwards to avoid index mismatches after replacing. + .forEach { match -> + replace( + match.range.first, + match.range.last + 1, + // First group is the whole match, second group is the inner content without the delimiters. + match.groups[1]?.value?.takeIf { value } ?: "", + ) + } } // Replace placeholders (defined as [[NAME]] in the template file) with their corresponding value.