Add image tags for commenting
authorLanius Trolling <lanius@laniustrolling.dev>
Thu, 24 Aug 2023 18:54:18 +0000 (14:54 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Thu, 24 Aug 2023 18:54:18 +0000 (14:54 -0400)
src/main/kotlin/info/mechyrdia/data/views_comment.kt
src/main/kotlin/info/mechyrdia/lore/parser_tags.kt

index 9ca1be55bc1960378a3f0ea572c91456cfe00bb2..ed64c34fa112f1dcced4d934085ff7aa8dcad9ab 100644 (file)
@@ -459,6 +459,29 @@ suspend fun ApplicationCall.commentHelpPage(): HTML.() -> Unit = page("Commentin
                                                a(href = "https://google.com/") { +"HTML link" }
                                        }
                                }
+                               tr {
+                                       td { +"[imgbb=256x256]Lns12z1/robert-sparr.png[/imgbb]" }
+                                       td {
+                                               p {
+                                                       +"Creates an embedded image:"
+                                                       br
+                                                       unsafe {
+                                                               raw("<script>window.appendImageThumb('https://i.ibb.co/Lns12z1/robert-sparr.png', '${getImageSizeStyleValue(256, 256)}');</script>")
+                                                       }
+                                                       br
+                                                       +"The tag param controls the width and height, much like a table cell. The size unit is viewport-responsive and has no correlation with pixels."
+                                               }
+                                               p {
+                                                       +"A similar tag is used to embed images that are hosted on Imgur, e.g. the image at https://i.imgur.com/dd0mmQ1.png"
+                                                       br
+                                                       unsafe {
+                                                               raw("<script>window.appendImageThumb('https://i.imgur.com/dd0mmQ1.png', '${getImageSizeStyleValue(250, 323)}');</script>")
+                                                       }
+                                                       br
+                                                       +"can be embedded using [imgur=250x323]dd0mmQ1.png[/imgur]"
+                                               }
+                                       }
+                               }
                                tr {
                                        td { +"[reply](comment id)[/reply]" }
                                        td { +"Creates a reply link to a comment" }
index 5765f7f1d1a9395a69eabf3a42521424db602da6..70bd963fd1a323224a5555c97f20b4575cd330e2 100644 (file)
@@ -356,6 +356,23 @@ enum class TextParserCommentTags(val type: TextParserTagType<Unit>) {
        
        LANG(TextParserFormattingTag.LANG.type),
        
+       IMGUR(
+               TextParserTagType.Indirect { tagParam, content, _ ->
+                       val imageUrl = sanitizeExtLink(content)
+                       val (width, height) = getSizeParam(tagParam)
+                       
+                       "<script>window.appendImageThumb('https://i.imgur.com/$imageUrl', '${getImageSizeStyleValue(width, height)}');</script>"
+               }
+       ),
+       IMGBB(
+               TextParserTagType.Indirect { tagParam, content, _ ->
+                       val imageUrl = sanitizeExtLink(content)
+                       val (width, height) = getSizeParam(tagParam)
+                       
+                       "<script>window.appendImageThumb('https://i.ibb.co/$imageUrl', '${getImageSizeStyleValue(width, height)}');</script>"
+               }
+       ),
+       
        REPLY(
                TextParserTagType.Indirect { _, content, _ ->
                        sanitizeId(content)?.let { id ->
@@ -383,6 +400,9 @@ val NON_LINK_CHAR = Regex("[^#a-zA-Z\\d\\-._]")
 val DOT_CHARS = Regex("\\.+")
 fun sanitizeLink(html: String) = html.replace(NON_LINK_CHAR, "").replace(DOT_CHARS, ".")
 
+val NON_EXT_LINK_CHAR = Regex("[^#a-zA-Z\\d\\-._/]")
+fun sanitizeExtLink(html: String) = html.replace(NON_EXT_LINK_CHAR, "").replace(DOT_CHARS, ".")
+
 val ID_REGEX = Regex("[A-IL-TVX-Z0-9]{24}")
 fun sanitizeId(html: String) = ID_REGEX.matchEntire(html)?.value