Skip to content

Commit

Permalink
Merge branch 'keta1-develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz committed Oct 13, 2024
2 parents 4c2fedb + 124e9cf commit afe4d2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mikepenz.markdown.utils

import org.intellij.markdown.html.entities.Entities

/**
* Based on: https://github.com/JetBrains/markdown/blob/master/src/commonMain/kotlin/org/intellij/markdown/html/entities/EntityConverter.kt
* Removed HTML focused escaping by https://github.com/mikepenz/multiplatform-markdown-renderer/pull/222
*/
object EntityConverter {
private const val escapeAllowedString = """!"#\$%&'\(\)\*\+,\-.\/:;<=>\?@\[\\\]\^_`{\|}~"""
private val REGEX = Regex("""&(?:([a-zA-Z0-9]+)|#([0-9]{1,8})|#[xX]([a-fA-F0-9]{1,8}));|(["&<>])""")
private val REGEX_ESCAPES = Regex("${REGEX.pattern}|\\\\([$escapeAllowedString])")

fun replaceEntities(
text: CharSequence,
processEntities: Boolean,
processEscapes: Boolean
): String {
val regex = if (processEscapes) REGEX_ESCAPES else REGEX
return regex.replace(text) { match ->
val g = match.groups
when {
g.size > 5 && g[5] != null -> g[5]!!.value[0].toString()
g[4] != null -> match.value
else -> {
val code = when {
!processEntities -> null
g[1] != null -> Entities.map[match.value]
g[2] != null -> g[2]!!.value.toInt()
g[3] != null -> g[3]!!.value.toInt(16)
else -> null
}
code?.toChar()?.toString() ?: "&${match.value.substring(1)}"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mikepenz.markdown.utils
import org.intellij.markdown.IElementType
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.getTextInNode
import org.intellij.markdown.html.entities.EntityConverter

/**
* Tag used to indicate an url for inline content. Required for click handling.
Expand Down

0 comments on commit afe4d2a

Please sign in to comment.